Passing variable from Form to Module in VBA

后端 未结 2 906
我在风中等你
我在风中等你 2020-11-28 06:58

I have the following button on a Form:

Private Sub CommandButton1_Click()
 Dim pass As String
 pass = UserForm1.TextBox1
 Unload UserForm1
End Sub

2条回答
  •  清酒与你
    2020-11-28 07:50

    Don't declare the variable in the userform. Declare it as Public in the module.

    Public pass As String
    

    In the Userform

    Private Sub CommandButton1_Click()
        pass = UserForm1.TextBox1
        Unload UserForm1
    End Sub
    

    In the Module

    Public pass As String
    
    Public Sub Login()
        '
        '~~> Rest of the code
        '
        UserForm1.Show
        driver.findElementByName("PASSWORD").SendKeys pass
        '
        '~~> Rest of the code
        '
    End Sub
    

    You might want to also add an additional check just before calling the driver.find... line?

    If Len(Trim(pass)) <> 0 Then
    

    This will ensure that a blank string is not passed.

提交回复
热议问题