I have the following function put in "Module1" determine the maximum value of a text box in user form, named "frmAddRecord2", that can be input.
txtLV and txtMaxLV are textboxes under "frmAddRecord2".
And txtMaxLV_Pass is a boolean variable put under frmAddRecord2 as well. At this stage, this function works fine.
Public Function txtLV_Max() As Long
With frmAddRecord2
If .txtLV.Value >= 99 Or Not .txtMaxLV_Pass Then
txtLV_Max = 109
Else
txtLV_Max = .txtMaxLV.Value - 1
End If
End With
End Function
As I will have "frmAddRecord1", "frmAddRecord2", "frmAddRecord3" ... etc, so I would like to call the following sub when frmAddRecord1 or frmAddRecord2 is activated.
Public Sub SetActiveUserForm(Optional UserFormName As String)
If UserFormName = "frmAddRecord1" Then
Set ActiveUserForm = frmAddRecord1
UserFormShown = True
ElseIf UserFormName = "frmAddRecord2" Then
Set ActiveUserForm = frmAddRecord2
UserFormShown = True
Else
Set ActiveUserForm = Nothing
UserFormShown = False
End If
End Sub
And I would like to restructure the function as:
Public Function txtLV_Max() As Long
With activeuserform
If .txtLV.Value >= 99 Or Not .txtMaxLV_Pass Then
txtLV_Max = 109
Else
txtLV_Max = .txtMaxLV.Value - 1
End If
End With
End Function
However, error occur at the Line "If .txtLV.Value >= 99 Or Not .txtMaxLV_Pass Then" . After testing, the txtMaxLV_Pass is found failed to be call after I restructured the function. And it work fine again if I moved txtMaxLV_Pass to be public under Module1.
But I would like to ask, if I want to keep the txtMaxLV_Pass under the userform, what should I changed in declare the userform variable. Please advice, I have studied this in web site and books, but still not able to tackle. Thank you for all your help.
option explicit
at the top of your code modules? IsActiveUserForm
a global variable you're referencing? – MistellaActiveUserForm
as? I think it may need to beVariant
for the userform properties to be accessible. – MistellaDim ActiveUserForm as Userform
toDim ActiveUserForm as Object
. – Mistella