2017-08-25 3 views
0

J'ai actuellement un problème avec un formulaire de connexion simple dans Excel (VBA), en cas d'erreur, continue et avoir une autre erreur, il me donne encore deux MsgBoxes avec des erreurs mais avec le "Déchargez-moi "et" Goto Ende "il devrait se fermer complètement.Excel VBA Forms boucle de message d'erreur

Tout devine pourquoi cela ne fonctionne pas? Je sais que c'est très basique et probablement très redondant, mais ça devrait marcher.

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 

Edit: En fait, je essayé de faire la 2ème UserForm pour la connexion, et je suis d'arriver le même problème. Tout fonctionne bien, jusqu'à ce que je ferme tout le programme, puis le message d'erreur apparaît à nouveau. Est-ce que je décharge le userform incorrect? Maby l'utilisateur de connexion dit ouvert et continue quand tout se ferme.

Éditer 2: Je pourrais juste désactiver des alertes mais ce serait une solution laide et certainement rien que je veux implémenter sur chaque bouton de fermeture dans le programme.

+0

merci! Je le ferai. – Dominik

Répondre

0

Vous pouvez vérifier les valeurs vides dans les zones de texte avec ceci:

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

'Your code 

Ende: Exit Sub 

Pour vérifier le nom d'utilisateur et mot de passe dans une base de données, vous pouvez le faire:

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 

Cela fonctionnera si, dans la base de données les noms d'utilisateur sont dans la colonne A et les mots de passe dans la colonne B.