How to password protect an application

  • Thread starter Thread starter Nathan Gutman
  • Start date Start date
N

Nathan Gutman

I have an application for which I want the user to use a password
before being able to use it. I want the user to be able to make up to
three tries before terminating the application.
If I use an InputBox to prompt for the password the user can click
Cancel or Close and can get to the application worksheet without using
any password.
If I use a UserForm with a TextBox to enter the password how can I
display the password TextBox up to three times without unloading and
reloading the UserForm.
If wrong password the user would get a critical MsgBox which when
clicked OK would get, up to three times, immediately back to the
TextBox to re-enter the password.
Thanks for any help,
Nathan
 
Hi Nathan

One way among several. Userform1 code (with a Textbox1 for entry and a CommandButton1
saying "OK" + default=true):

Option Explicit

Public Pwd As String

Private Sub CommandButton1_Click()
Pwd = TextBox1.Text
Me.Hide
End Sub

In a standard module:

Option Explicit

Sub Auto_open()
Dim Count As Long
Dim GoodGuy As Boolean
GoodGuy = False
For Count = 1 To 3
With UserForm1
Select Case Count
Case 1
.Caption = _
"Password please"
Case 2
.Caption = _
"Try again please"
Case 3
.Caption = _
"Last chance:"
End Select
.Show
If .Pwd = _
"Nathan Rules" Then
GoodGuy = True
Exit For
End If
End With
Next
Unload UserForm1
If GoodGuy = True Then
Sheets(1).Visible = True
MsgBox "Yo da man"
Else
Sheets(1).Visible = xlVeryHidden
ThisWorkbook.Save
ThisWorkbook.Close
End If
End Sub



Please don't MsgBox at wrong entries, thay have to be OK'ed away and are all over
extremely annoying. Use a label on the form, it's caption, color or whatever. Anything but
msgboxes.
 
Thanks for the suggestions, I will look at them carefully.
There is another twist to that issue.
The worksheet will not require a password for the first 30 days from
the time that it was first opened. After that it will require a
password.
Thanks again,
Nathan
 
Back
Top