2017-08-25 104 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列。