Calling a userform and returning a value

后端 未结 3 1893
北恋
北恋 2020-12-11 17:39

I have a vba code thats Auto_Open. It does some checks then prompts a userform that asks for username and password. I called this userform with userform_name.show

相关标签:
3条回答
  • 2020-12-11 18:28

    You can manage to do this without the use of public variables.

    There appears to be a difference between show/hide and load/unload.

    If you hide a form while it's still loaded it won't be cleared out, so you can reference the state of the controls on the form.

    For example I was using a date picker (called DTPicker1) on a form, my code in the module looks something like this:

    Dim NewDay As Date
    
    Load FrmDayPicker
    FrmDayPicker.Show
    
    NewDay = FrmDayPicker.DTPicker1.Value
    
    Unload FrmDayPicker
    
    Debug.Print NewDay
    

    On your form you can just use Me.Hide insteaded of Unload Me and this should work

    0 讨论(0)
  • 2020-12-11 18:30

    How about using a function instead of a sub?

    Function loginbutton()
      ' your code
    
      loginbutton = bool
    End Function
    

    Now in your calling code you can test for true/false

    if loginbutton() then
      'true responce
    else
      'false responce
    end if
    
    0 讨论(0)
  • 2020-12-11 18:35

    Remove Dim bool As Boolean from the userform code area and declare it in the module as shown below

    This is how your Code in the module would look like

    Public bool As Boolean
    
    Sub Auto_Open()
        '
        '~~> Rest of the code
        '
        UserForm1.Show
    
        If bool = True Then
            '~~> Do Something
        Else
            '~~> Do Something        
        End If
    
        '
        '~~> Rest of the code
        '
    End Sub
    
    0 讨论(0)
提交回复
热议问题