• Privacywetgeving
    Het is bij Helpmij.nl niet toegestaan om persoonsgegevens in een voorbeeld te plaatsen. Alle voorbeelden die persoonsgegevens bevatten zullen zonder opgaaf van reden verwijderd worden. In de vraag zal specifiek vermeld moeten worden dat het om fictieve namen gaat.

Fout passwoord ingeven moet code afgebroken worden

Status
Niet open voor verdere reacties.

bjornesto

Gebruiker
Lid geworden
16 apr 2012
Berichten
201
Ik heb een code gemaakt en zou graag hebben als het passwoord fout is dat dan de private sub afsluit zodat je niet verder kan gaan naar de volgende msgbox. Hoe moet ik dat dan aanpassen?

Code:
Private Sub CommandButton2_Click()
Dim mypassword
tryagain:
mypassword = InputBox("Enter Password", "Password")
If mypassword = "yourpassword" Then

'your code here
MsgBox ("Correct")
Else
If MsgBox("Password incorrect, try again?", vbYesNo, "Wrong password") = vbYes Then GoTo tryagain
End If
If MsgBox("Are you sure that you wish to clear the contents?", vbYesNo, "Confirm") = vbYes Then
    Range("A3").Select
    Selection.SpecialCells(xlCellTypeConstants, 1).Select
    Selection.ClearContents
End If
End Sub
 
Dat kan bijvoorbeeld als volgt. Er is daarbij ook een teller ingebouwd waarbij na 3 pogingen de procedure wordt afgebroken.

Code:
Private Sub CommandButton2_Click()
    Dim mypassword
    Dim teller As Integer
    teller = 1
tryagain:
    mypassword = InputBox("Enter Password", "Password")
    If mypassword = "yourpassword" Then
        'your code here
        MsgBox ("Correct")
    ElseIf teller = 3 Then
        MsgBox "Exit", vbOKOnly
        Exit Sub
    Else
        If MsgBox("Password incorrect, try again?", vbYesNo, "Wrong password") = vbYes Then
            teller = teller + 1
            GoTo tryagain
        End If
    End If
    If MsgBox("Are you sure that you wish to clear the contents?", vbYesNo, "Confirm") = vbYes Then
        Range("A3").Select
        Selection.SpecialCells(xlCellTypeConstants, 1).Select
        Selection.ClearContents
    End If
End Sub

Rebmog
 
Het probleem is nog steeds hetzelfde

Als ik een juist antwood intik zou ik naar de volgende messagebox moeten gaan die in de code staat

Code:
If MsgBox("Are you sure that you wish to clear the contents?", vbYesNo, "Confirm") = vbYes Then
    Range("A3").Select
    Selection.SpecialCells(xlCellTypeConstants, 1).Select
    Selection.ClearContents
End If

Als ik echter een fout paswoord geef en kies voor no dan moet die gewoon sluiten.
Momenteel gaat als je op no klikt nog altijd verder door naar de messagebox hierboven om cellen te deleten
 
De keuze van No bij een foutief password had ik over het hoofd gezien. Hierbij een aangepaste versie.

Code:
Private Sub CommandButton2_Click()
    Dim mypassword
    Dim teller As Integer
    teller = 1
tryagain:
    mypassword = InputBox("Enter Password", "Password")
    If mypassword = "yourpassword" Then
        'your code here
        MsgBox ("Correct")
    ElseIf teller = 3 Then
        MsgBox "Exit", vbOKOnly
        Exit Sub
    Else
        If MsgBox("Password incorrect, try again?", vbYesNo, "Wrong password") = vbYes Then
            teller = teller + 1
            GoTo tryagain
        Else
            Exit Sub
        End If
    End If
    If MsgBox("Are you sure that you wish to clear the contents?", vbYesNo, "Confirm") = vbYes Then
        Range("A3").Select
        Selection.SpecialCells(xlCellTypeConstants, 1).Select
        Selection.ClearContents
    End If
End Sub

Rebmog
 
Val je gebruiker niet lastig met een overdaad aan aan te klikken objecten.
Code:
Private Sub CommandButton2_Click()
    Dim mypassword As String, terller As Integer
    teller = 0
    Do
        mypassword = Application.InputBox("Enter Password - 3 wrong passwords closes this box.", "Password", "Attempt " & teller + 1, , , , , 2)
        teller = teller + 1
    Loop Until mypassword = "yourpassword" Or teller = 3
    If mypassword <> "yourpassword" Then Exit Sub
    If MsgBox("Are you sure that you want to clear the contents?", vbYesNo, "Confirm") = vbYes Then
        Cells(3, 1).SpecialCells(xlCellTypeConstants, 1).ClearContents
    End If
End Sub
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan