Referencing UserForm public variable in vba

30 Views Asked by At

There is a userform with the following code:

UserFormA


Public BdayResult As Birthdays

Private Sub cbOk_Click()
    Dim frm As UserFormBirthdays
    Set frm = cbOk.Parent
    frm.Hide
End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
    Debug.Print "Initialize"
    Dim frm As UserFormBirthdays
    Dim sEmployeesExcelFilePath As String
    sEmployeesExcelFilePath = "C:\Users\arun_jayapal\Documents\Employees.xlsx"
    Debug.Print (sEmployeesExcelFilePath)
    
    Set frm = lblRunDate.Parent
    
    
    lblRunDate.Caption = frm.BdayResult.RunDate
    '...other code stripped for brevity
End Sub

In some other routine I am initializing an instance of that form and calling Show()


'inside some sub
Dim frm as new UserFormA

set frm.BDayResult = b 'some object reference

frm.Show vbModal

Code enters the UserForm_Initalize routine. It proceeds to execute the line lblRunDate.Caption = frm.BdayResult.RunDate. I get an exception saying:

Run-time error '91':

Object variable or With block variable not set

And the debugger indicates line in the sub, i.e. set frm.BDayResult = b

Hoping someone can point where I messed up.

0

There are 0 best solutions below