2017-08-25 140 views
0

我目前有一个简单的登录窗体在Excel中(VBA)的问题,当有错误,继续和另一个错误,它仍然给我两个更多的MsgBoxes错误,但与“卸载我“和”Goto Ende“它应该完全关闭。Excel VBA窗体错误消息循环

任何猜测为什么这不起作用?我知道这是非常基本的,可能非常多余,但它应该仍然有效。

Public Name As Variant 
Public Password As Variant 

Private Sub Btn_Register_Cancel_Click() 
    Unload Me 
End Sub 

Private Sub Btn_Register_Register_Click() 

Start: 

Dim Error As Integer 

Error = 0 

Name = Tbx_Register_Name.Value 
Password = Tbx_Register_Password.Value 

'Check for Name, Password, Password2 if empty 
If Tbx_Register_Name.Value = "" Then 
    Error = MsgBox("Please enter a username.", _ 
      vbRetryCancel, "Error") 

     If Error = 2 Then 
      Unload Me 
      GoTo Ende 

     Else 
      Application.ScreenUpdating = False 
      Register.Hide 
      Register.Show 
      Application.ScreenUpdating = True 
      GoTo Start 

     End If 

ElseIf Tbx_Register_Password.Value = "" Then 
    Error = MsgBox("Please enter a password.", _ 
      vbRetryCancel, "Error") 

     If Error = 2 Then 
      Unload Me 
      GoTo Ende 

     Else 
      Application.ScreenUpdating = False 
      Register.Hide 
      Register.Show 
      Application.ScreenUpdating = True 
      GoTo Start 

     End If 

ElseIf Tbx_Register_Password2.Value = "" Then 
    Error = MsgBox("This field cannot be empty.", _ 
      vbRetryCancel, "Error") 

     If Error = 2 Then 
      Unload Me 
      GoTo Ende 

     Else 
      Application.ScreenUpdating = False 
      Register.Hide 
      Register.Show 
      Application.ScreenUpdating = True 
      GoTo Start 

     End If 

End If 

With Workbooks("General Makro.xlsx").Worksheets("User") 
'Check for Username match in registration list 
For i = 1 To 100 

    If .Cells(i, 1).Value = Name Then 

     Error = MsgBox("This username is already taken.", _ 
      vbRetryCancel, "Error") 

     If Error = 2 Then 
      Unload Me 
      i = 100 
      GoTo Ende 


     Else 
      Application.ScreenUpdating = False 
      Register.Hide 
      Register.Show 
      Application.ScreenUpdating = True 
      GoTo Start 

     End If 

    End If 

Next i 

End With 

'Check for the passwords to match 
If Tbx_Register_Password.Value = Tbx_Register_Password2.Value Then 

    With Workbooks("General Makro.xlsx").Worksheets("User") 

     For i = 1 To 100 

      If .Cells(i, 1) = "" Then 

       .Cells(i, 1).Value = Name 
       .Cells(i, 2).Value = Password 

       Tbx_Register_Password.Value = "" 
       Tbx_Register_Password2.Value = "" 


       Application.ScreenUpdating = False 
       Register.Hide 
       Login.Show 
       Tbx_Login_Name.Value = .Cells(i, 1).Value 
       Tbx_Login_Password.Value = .Cells(i, 2).Value 
       Application.ScreenUpdating = True 

       i = 100 
       GoTo Ende 

      End If 

     Next i 

    End With 

Else 
    Error = MsgBox("The passwords have to match!", vbRetryCancel, "Error") 

    If Error = 2 Then 
     Unload Me 
     GoTo Ende 

    Else 
     Application.ScreenUpdating = False 
     Register.Hide 
     Register.Show 
     Application.ScreenUpdating = True 
     GoTo Start 

    End If 

End If 

Ende: 

End Sub 

编辑:我真的试图做第2次为用户窗体的登录,而我又得到了同样的问题在那里。一切正常,直到我关闭整个程序,然后错误消息再次出现。我是否卸载用户表单不正确? Maby的登录用户表单表示打开并在所有内容关闭时继续。

编辑2:我可以关闭警报,但这将是一个丑陋的解决方案,绝对没有我想在程序中的每个关闭按钮上实现。

+0

谢谢!我去做。 – Dominik

回答

0

您可以验证在文本框空值与此:

If TextBox.Text = "" Then 
    MsgBox "Is blank!" 
    Unload Me 
    GoTo Ende 
End If 

'Your code 

Ende: Exit Sub 

要验证数据库中的用户名和密码,你可以这样做:

Dim sh As Worksheet 
Dim LastRow As Long 
Dim UserRange As Range 
Dim UserMatch As Range 

Set sh = ThisWorkbook.Sheets("database") 
LastRow = sh.Range("A" & Rows.Count).End(xlUp).Row 

Set UserRange = sh.Range("A1:A" & LastRow) 

Set UserMatch = UserRange.Find(What:=UserTextBox.Text, LookIn:=xlValues) 
If Not UserMatch Is Nothing Then 
    MsgBox "User exists!" 

    If PwdTextBox.Text = UserMatch.Offset(0, 1) Then 
     MsgBox "Pwd matched!" 
     'do something 
    Else 
     MsgBox "Wrong password!" 
     'do something 
    End If 

Else 
    MsgBox "User dont exists!" 
    'do something 
End If 

这将如果工作数据库用户名在A列,密码在B列。