0
votes

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.

1
Do you have option explicit at the top of your code modules? Is ActiveUserForm a global variable you're referencing?Mistella
Yes, I put option explicit at the top of module1 and frmAddRecord2. ActiveUserForm is global variable in module1; txtMaxLv is a global variable in frmAddRecord2. And while the function is using, the userform is still showing on the screen.Ting Fung Li
What type of variable do you have ActiveUserForm as? I think it may need to be Variant for the userform properties to be accessible.Mistella
I set ActiveUserForm as userformTing Fung Li
Try changing Dim ActiveUserForm as Userform to Dim ActiveUserForm as Object.Mistella

1 Answers

0
votes

The Global variable ActiveUserForm was given the type of Userform. However, to access UserForm properties from a Userform variable, it actually needs to be of type Object or Variant.