[VBA] Password on an input box -

Eawyne

New Member
Joined
Jun 28, 2021
Messages
43
Office Version
  1. 2013
Platform
  1. 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)

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
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
First, I would suggest that you use the InputBox method of the Application object, instead of the InputBox function. This way you can test whether the user has clicked on the Cancel button as apposed to the leaving it blank. Then your last macro can be amended as follows...

VBA Code:
Sub stuartk()

    Dim Pwrd As Variant
    Dim Proceed As Boolean
   
    Proceed = False
    Do
        Pwrd = Application.InputBox("Please enter the password")
        If Pwrd = False Then Exit Sub
        If Pwrd = "abc" Then
            Proceed = True
        Else
            MsgBox "You have entered the wrong password, try again!", vbExclamation
        End If
    Loop Until Proceed
   
    Range("G8:G30,I8:I30,K8:K30,M8:M30").ClearContents
   
End Sub

Hope this helps!
 
Upvote 0
Solution
Oh, cool about the Application object, I wasn't aware of that little difference. Thanks a lot, as this is working perfectly, as intended :biggrin:
 
Upvote 0
Oh, cool about the Application object, I wasn't aware of that little difference. Thanks a lot, as this is working perfectly, as intended :biggrin:

Just for interest, there is a way to test for Cancel button press when using the simple InputBox Function

VBA Code:
Sub stuartk()

    Dim Entry   As Variant
    Dim Prompt  As String, Title As String
    Dim try     As Long
    
    Const Pwrd As String = "abc"
    
    try = 1
    Title = "Enter Password"
    
    Do
        Prompt = "Please Enter Password To Continue"
        Prompt = Prompt & Chr(10) & Chr(10) & _
                  "attempt " & try & " of 3"

        Entry = InputBox(Prompt, Title)
        'cancel pressed
        If StrPtr(Entry) = 0 Then Exit Sub
        
        If Entry <> Pwrd Then
          If try = 3 Then
                MsgBox "Three Attempts Only Allowed", 16, "Three Attempts Only"
                Exit Sub
        Else
                MsgBox "Password invalid - Please try again", 48, "Invalid Password"
                try = try + 1
          End If
            
        Else
'Optional Success MsgBox
                'MsgBox "Password Correct - Click OK To Continue.", 64, "Password Correct"
            
                Exit Do
        End If
        
    Loop
   
    Range("G8:G30,I8:I30,K8:K30,M8:M30").ClearContents
   
End Sub

Hope Helpful

Dave
 
Upvote 0
Just for interest, there is a way to test for Cancel button press when using the simple InputBox Function

VBA Code:
Sub stuartk()

    Dim Entry   As Variant
    Dim Prompt  As String, Title As String
    Dim try     As Long
   
    Const Pwrd As String = "abc"
   
    try = 1
    Title = "Enter Password"
   
    Do
        Prompt = "Please Enter Password To Continue"
        Prompt = Prompt & Chr(10) & Chr(10) & _
                  "attempt " & try & " of 3"

        Entry = InputBox(Prompt, Title)
        'cancel pressed
        If StrPtr(Entry) = 0 Then Exit Sub
       
        If Entry <> Pwrd Then
          If try = 3 Then
                MsgBox "Three Attempts Only Allowed", 16, "Three Attempts Only"
                Exit Sub
        Else
                MsgBox "Password invalid - Please try again", 48, "Invalid Password"
                try = try + 1
          End If
           
        Else
'Optional Success MsgBox
                'MsgBox "Password Correct - Click OK To Continue.", 64, "Password Correct"
           
                Exit Do
        End If
       
    Loop
  
    Range("G8:G30,I8:I30,K8:K30,M8:M30").ClearContents
  
End Sub

Hope Helpful

Dave

Thanks for this ; I'm not sure I might use it, but it's still interesting as a tool to know about.
 
Upvote 0
Thanks for this ; I'm not sure I might use it, but it's still interesting as a tool to know about.

most welcome hope find helpful

StrPtr is an undocumented function that can tell whether an empty string was returned by the InputBox function (Visual Basic for Applications) if the user just click OK when there was nothing in it or an empty string exists because the user hit Cancel.

Dave
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,167
Members
448,870
Latest member
max_pedreira

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top