Search code examples
vbauserform

Using a Class Module public variable withevents


I have a userform1 with calendar. Each day is formed by a label. When clicking on a label, call a class module that identifies the clicked label and records its caption:

Public WithEvents Frme As MSForms.Label
Public frm As UserForm

Private Sub Frme_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
'here i want call a userform2
End Sub

I need call a userform2 to show the activities that have that day, but I can't link the Frme variable with the userform2. I ask for help

I don't have much knowledge in class module although I've researched a lot on the internet


Solution

  • You can pass the selected label's caption to Userform2 by declaring a public variable in the class module and setting its value before calling Userform2. In the Userform2 module, you can retrieve the value of this variable to show the activities of the selected day. Here's an example of how you can modify the code:

    In the class module:

    Public WithEvents Frme As MSForms.Label
    Public frm As UserForm
    Public SelectedDate as String
    
    Private Sub Frme_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
    ByVal X As Single, ByVal Y As Single)
    SelectedDate = Frme.Caption
    UserForm2.Show
    End Sub
    
    

    In the Userform2 module:

    
    Private Sub UserForm_Activate()
    'Use the SelectedDate variable to retrieve and show activities for that day
    End Sub
    
    

    By setting the value of the SelectedDate variable in the class module before calling Userform2, you can retrieve it in the Userform2 module to show the activities for that day. You can declare the variable in Main module but i think it's better in the class module.