Password Protection

S

SEWarren

Hello All
I have a workbook that has 5 user forms. One of the user forms opens when
the workbook is opened. On the main user form i have 4 command buttons. Each
which opens a different form. I would like to password protect one of the
command buttons (user forms). Is this possible and if so could someone Help.
Thanks
 
D

Dave Peterson

Maybe you could just ask at the top of procedure

Option Explicit
sub macnamehere()
dim myPWD as string
dim UserPWD as string
myPWD = "top secret"

userpwd = inputbox(Prompt:="What's the password, Kenneth?")

if userpwd <> mypwd then
msgbox "nope"
exit sub
end if

'rest of code
End Sub
 
S

SEWarren

Dave, if i understand correctly, you can't hide or encrypt entries to a
inputbox. Is that correct? if so i wouldn't be able to keep the password
that a user typed from being seen. I think i need to use a separate excel
user form to accomplish this task, i'm just not sure how to do it.
 
D

Dave Peterson

Yep.

But if you create that other userform, you can add a textbox to it. Then change
the .passwordchar property to "*" to hide the response.

I put this in a general module:

Option Explicit
Public UserPWD As String
Sub macnamehere()
Dim myPWD As String

myPWD = "top secret"

UserForm1.Show
If UserPWD <> myPWD Then
MsgBox "nope"
Exit Sub
End If

UserForm2.Show

End Sub

I put this behind the new userform (a textbox and 2 commandbuttons) (userform1
for me):

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
UserPWD = Me.TextBox1.Text
Unload Me
End Sub
Private Sub UserForm_Initialize()
Me.CommandButton1.Caption = "Cancel"
Me.CommandButton2.Caption = "Ok"
Me.TextBox1.PasswordChar = "*"
End Sub

Dave, if i understand correctly, you can't hide or encrypt entries to a
inputbox. Is that correct? if so i wouldn't be able to keep the password
that a user typed from being seen. I think i need to use a separate excel
user form to accomplish this task, i'm just not sure how to do it.
 
Top