我有一个在Auto_Open中运行的VBA代码。它进行了一些检查,然后提示一个用户窗体要求输入用户名和密码。我使用 userform_name.show
调用了这个用户窗体。
我的问题是如何从用户窗体代码向Auto_Open
子程序返回一个Boolean
值。
我将验证凭据是否正确的代码链接到了表单上的“登录”按钮。这是生成布尔值的代码。我需要将它返回给Auto_Open。
Private Sub loginbutton()
Dim bool As Boolean
Dim lrup
Dim r As Long
Dim pass As String
loginbox.Hide
'are fields empty
Do While True
If unBox.Text = "" Or pwBox.Text = "" Then
MsgBox ("You must enter a Username and Password")
Else
Exit Do
End If
loginbox.Show
Exit Sub
Loop
'find pw reated to username (if existant)
lrup = UserPass.Range("A1").Offset(UserPass.Rows.Count - 1, 0).End(xlUp).Row
If unBox = "b0541476" And pwBox = "theone" Then
bool = True
Else
MsgBox ("Invalid username or password. Please try again.")
loginbox.Show
Exit Sub
End If
For r = 2 To lrup
If unBox = Cells(r, 1) Then
pass = Cells(r, 2).Value
Exit For
End If
Next
If pass = "" Then
MsgBox ("Invalid username or password. Please try again.")
loginbox.Show
Exit Sub
Else
bool = True
End If
End Sub