Access JumpStart 2.0 | Blog

A Rapid Development Framework for Microsoft Access

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
Visual Basic

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.

Sign up For a Daily Email Adventure in Microsoft Access

Every business day (typically M-F), I'll send you an email with information about my ongoing journey as an advanced Access application developer. It will be loaded with my tips and musings.

    We won't send you spam. Unsubscribe at any time.