简体   繁体   中英

Excel VBA Forms Error Message loop

i currently have a problem with a simple login form in excel (VBA), when having an error, continuing and having another error it still gives me two more MsgBoxes with errors but with the "Unload Me" and "Goto Ende" it should close itself completely.

Any guesses why this isn't working? I know this is very basic and probably very redundant, but it should still work.

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: I Actually Tried to do the 2nd UserForm for the login, and i happen to get the same problem there. Everything works just fine, until i close the whole program, then the error-message appears again. Am i unloading the userform incorrect? Maby the login userform says open and continues when everything is getting closed.

Edit 2: I could just turn off alerts but that would be an ugly solution and definitely nothing i want to implement on every close button in the program.

You can verify blank values in textboxes with this:

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

'Your code

Ende:    Exit Sub

To verify the username and password in a database, you can do this:

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

This will work if in the database the usernames are in column A and the passwords in column B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM