Search code examples
vbams-access

Keep object in memory while is form is open (access)


I am writing an access application, and I just spent time setting up a class that would make the application easy to understand and efficient.

However, when I need the object for the second click event, the object is no longer in memory.

I have a user click on a button that sets up the object and does some tests:

Public this_renewal As Renewal

Private Sub cmdMA_Click()
    
        Set this_renewal = Factory.CreateRenewal(cMA)
        
        Call BranchLabelVisibility(True)
        Me.lblBranchToAdd.Caption = this_renewal.Abb
        Call DateLabelVisibility(True)
        Me.lblYearToAdd.Caption = this_renewal.Year
        Me.lblMonthToAdd.Caption = this_renewal.Month
        Call TestMonth
    
End Sub

Here is the CreateRenewal Function that I have in a regular module called factory. I got this idea from another thread on how to initialize a class with attributes: VBA: Initialize object with values?

 Public Function CreateRenewal(strFileName As String) As Renewal

    Dim renewal_obj As Renewal
    Set renewal_obj = New Renewal
    
    Call renewal_obj.InitiateProperties(strFileName)
    Set CreateRenewal = renewal_obj

End Function

and that calls the InititateProperties within the renewal class:

Public Sub InitiateProperties(ByVal strFileName As String)

    strRenewalFile = strFileName
    strRenewalFullFileName = fnGetFullFileName()
    strRenewalFileAndPath = cPath & strRenewalFullFileName

    strBranchLetter = fnGetLetterFromFile(strRenewalFile)

    strAbb = DLookup("BranchAbb", "tblBranches", "BranchLetter = '" & strBranchLetter & "'")
    strBranchName = DLookup("Branch", "tblBranches", "BranchAbb = '" & strAbb & "'")
    
    If Len(Mid(strRenewalFullFileName, 10, 2)) = 1 Then
        strRenewalMonth = "0" & Mid(strRenewalFullFileName, 10, 2)
    Else
        strRenewalMonth = Mid(strRenewalFullFileName, 10, 2)
    End If
    strRenewal2DigitYear = Mid(strRenewalFullFileName, 12, 2)
    strRenewalYear = "20" & strRenewal2DigitYear
    
    strRenewalTable = strAbb & " " & strRenewalYear & " Renewals"

End Sub

then the user decides if they want to import this_renewal file (which is the object in memory) and clicks the import button if they do which runs this code:

Private Sub cmdImport_Click()

    DoCmd.SetWarnings False
    
    Call FixExcelFile
    
    Dim strTableName As String
    Dim strImportName As String
    strTableName = this_renewal.Table
    'strImportName = Left(fnGetFileName(Me.tbFileToImport), 8)

    Call ImportTable
    
    'Count and Display Values of the two Tables
    Call TableLabelVisibility(True)
    Call GetTableValues
    
    'Create Backup of Original
    Call CreateBackup
    
    'Run Queries to Update Data of Import
    Call AppendQuery
    Call UpdateMonth
    Call UpdateStatus
    Call UpdateUnderWriter
    
    Call ShowResults
    
    DoCmd.SetWarnings True

End Sub

If I step through the code during the cmdMA_Click event the object is created and those properties are initialized within the InitiateProperties sub in the class. After that click event finishes and I trigger the next click event (on the next button, on the same form) I receive "Object Variable or With Variable not set" for the object. Within the original click event I have no issue.


Solution

  • I figured it out; It is such a stupid thing that happens. So in the last procedure called during the first click event:

    Private Sub TestMonth()
    
        If this_renewal.IsNewMonth Then
            Me.cmdImport.Enabled = True
            Me.lblFileMistake.Visible = False
        Else
            GoTo WrongFile
        End If
        
    endSub:
        Exit Sub
        
    WrongFile:
        
        Me.cmdImport.Enabled = False
        Me.lblFileMistake.Visible = True
        Me.lblFileMistake.Caption = "Month " & this_renewal.Month & " was already added for branch " & this_renewal.Name & "!!"
        GoTo endSub
        
    End Sub
    

    Right where I have 'Exit Sub', it was 'End'. That clears all variables. Once I changed it to 'Exit Sub', it now works correctly.