Some things you do when designing forms inherently reset the VBA environment. This takes all your existing object variables in that form and resets them.
That’s why I often use a function to refer to an object to check to make sure it exists before I use it so I don’t get an error.
'Form_frmAccounts
Option Compare Database
Option Explicit
Private InternalAccount As AccountInterface
' ------------ Lazy Loading AccountInterface object
Private Function Acct() As AccountInterface
If InternalAccount Is Nothing Then Set InternalAccount = New AccountCreditCard
Set Acct = InternalAccount
End Function
' -------------
Private Sub Form_Current()
If Not IsNull(CurrentBalance) And Not IsNull(Me.MinimumPayment) And Not IsNull(Me.InterestRate) Then
Me.txtMonthsToPayOff = Acct.MonthsToPayoff(Me.CurrentBalance, Me.MinimumPayment, Me.InterestRate)
End If
End Sub
So you can see in the above code example, I have a function called Acct() that returns the InternalAccount object, but checks to make sure it is set first. This will also help in other instances if the user’s VBA session gets reset for some reason.
I use this technique a lot. I still get the autocomplete for the AccountInterface class when using the Acct function because that’s what it returns.