Eawyne
Board Regular
- Joined
- Jun 28, 2021
- Messages
- 53
- Office Version
- 2021
- Platform
- Windows
Hi all,
I've got a little problem I can't seem to reconcile. I wanted a simple password input on a button in my file. I've got two options, both of which working - but with one little caveat each time.
First code : it repeats if I enter a wrong password, but I can't cancel the input box.
Second code : it puts a message saying I entered a wrong password; it doesn't repeat but I can cancel the input box.
The right code should : put a message saying I did wrong => run the inputbox again, and I can cancel
What could I do ? I've tried tinkering around with the Loop function, but to no avail, I can't seem to place it correctly (if it's even the proper fuction to use)
I'm aware the code might be wordy... there's this quite elegant code from another thread around :
which I could play with maybe ? I dunno ^^' Thanks anyway
I've got a little problem I can't seem to reconcile. I wanted a simple password input on a button in my file. I've got two options, both of which working - but with one little caveat each time.
First code : it repeats if I enter a wrong password, but I can't cancel the input box.
Second code : it puts a message saying I entered a wrong password; it doesn't repeat but I can cancel the input box.
The right code should : put a message saying I did wrong => run the inputbox again, and I can cancel
What could I do ? I've tried tinkering around with the Loop function, but to no avail, I can't seem to place it correctly (if it's even the proper fuction to use)
VBA Code:
Sub Boite_Codestest()
'Demande un mot de passe pour ouvrir le popup des codes.
Dim Ans As Boolean
Const Pword As String = "zebra"
On Error Resume Next
Ans = False
Do While Ans = False
If InputBox("Veuillez entrer le mot de passe :", "Mot de passe") = Pword Then
Ans = True
End If
'Relance la demande si le mot de passe entré est mauvais.
Loop
Popup_Codes.Show
If PassProtect = vbNullString Then Exit Sub
End Sub
VBA Code:
'Public pblnEnteredPassword As Boolean
Sub Boite_Codestest2()
Dim PassProtect As Variant
If pblnEnteredPassword Then GoTo DoStuff
PassProtect = InputBox(Prompt:="Veuillez entrer le mot de passe" & vbCrLf & "(Sensible à la casse)", Title:="Codes")
If PassProtect = vbNullString Then Exit Sub
If PassProtect = "zebra" Then
pblnEnteredPassword = True
GoTo DoStuff
Else
MsgBox Prompt:="Mot de passe incorrect. Veuillez réessayer.", Buttons:=vbOKOnly
Exit Sub
End If
DoStuff:
Popup_Codes.Show
End Sub
I'm aware the code might be wordy... there's this quite elegant code from another thread around :
VBA Code:
Sub stuartk()
Dim Pwrd As Variant
Pwrd = InputBox("Please enter the password")
If Pwrd <> "abc" Then Exit Sub
Range("G8:G30,I8:I30,K8:K30,M8:M30").ClearContents
End Sub
which I could play with maybe ? I dunno ^^' Thanks anyway