0

I have a command button which launches an input box to allow input of a password.

Private Sub CommandButton1_Click()  
     Dim ThePW As String

     ThePW = InputBox("A password is required to run this procedure." & vbCrLf & _
        "Please enter the password:", "Password")

     If ThePW <> "123" Then MsgBox ("wrong pw")
End Sub

However, when I click exit(X) or cancel, the message box still displays wrong pw

How can I solve this? I don't want it to display wrong pw when I click exit(X) or select cancel.

Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
evabb
  • 405
  • 3
  • 21

1 Answers1

0

Use the code below, if he hits cancel, it just exits the Sub

Private Sub CommandButton1_Click()

Dim ThePW As String

ThePW = InputBox("A password is required to run this procedure." & vbCrLf & _
"Please enter the password:", "Password")

If ThePW = "" Then Exit Sub ' Exit if null input or cancel

If ThePW <> "123" Then
    MsgBox ("wrong pw")
Else
    ' do something if OK
End If        

End Sub
Shai Rado
  • 33,032
  • 6
  • 29
  • 51