PDA

View Full Version : [SOLVED] Password before running macro



rama4672
02-12-2005, 07:12 AM
Can anyone help with this
What i want to do is create a password that when someone tries to run the macro it will ask for a password, if it is wrong it will not allow them to run any macros.
It will be attached to a userform, so when a user press a command button to run the form it will ask for a password.

Regards

Ian

Jacob Hilderbrand
02-12-2005, 07:18 AM
Try this.


Option Explicit

Private Sub CommandButton1_Click()
Dim Pass As String
Dim Prompt As String
Dim Title As String
Dim UserPass As String
Pass = "MyPassword"
Prompt = "Enter the password to continue"
Title = "Password Input"
UserPass = InputBox(Prompt, Title)
If UserPass <> Pass Then
Prompt = "You have entered an incorrect password"
Title = "Incorrect Passowd"
MsgBox Prompt, vbCritical, Title
Exit Sub
End If
End Sub

mdmackillop
02-12-2005, 07:26 AM
Hi Jake,
I'm sure I've come across some code to enter asterisks in the text box, which would be a useful addition, if I can find it again!
MD

mdmackillop
02-12-2005, 07:28 AM
http://support.microsoft.com/default.aspx?scid=kb%3BEN-US%3B829070

If you set the PasswordChar property of a TextBox control, it becomes a "masked-edit" control. Every character that is typed in the TextBox control is replaced visually by the character that you specify. To use a TextBox control to validate a password, follow these steps:

1.Start Excel, and then open a new blank workbook.
2.On the Tools menu, point to Macro, and then click Visual Basic Editor.
3.On the Insert menu, click UserForm to insert a UserForm in your workbook.
4.Add a TextBox control to the UserForm.
5.On the View menu, click Properties to make the Properties window visible.
6.In the PasswordChar property of the TextBox control, type *.
Note You are changing the value to an asterisk.
7.Add a CommandButton control to the UserForm.
8.Double-click the CommandButton control to open the Code window for the UserForm.
9.In the Code window, type the following code for the CommandButton1 Click event:


Private Sub CommandButton1_Click()
If TextBox1.Text <> "userform" Then
MsgBox "Password is Incorrect. Please reenter."
TextBox1.Text = ""
TextBox1.SetFocus
Else
MsgBox "Welcome!"
Unload Me
End If
End Sub

10.On the Run menu, click Run Sub/UserForm.
11.Type the password userform in the TextBox control.
12.Click the CommandButton control.

Jacob Hilderbrand
02-12-2005, 07:29 AM
Make a TextBox then set the PasswordChar Property to *. Then you could check the text in the TextBox instead of an InputBox.

rama4672
02-12-2005, 08:48 AM
Thank you Jake and MD for the reply.

I have set up a userform now for the password, I have put your code md on the ok button, so it checks the textbox1 but it still says password is wrong.
Do i also use the code from you Jake, if so where do i put that.

Regards

Ian

rama4672
02-12-2005, 09:04 AM
Ok I have got it working, I worked out what i was doing wrong(i never read your post properly md)

So it now loads the next user form if the password is correct.
Next is there a way of it checking if the next user form (which i have called main menu)is already open, and if it is just exit from itself.

Thanks

Regards

Ian

mdmackillop
02-12-2005, 09:36 AM
Somthing like this



Sub Shows()
If MainMenu.Visible = True Then
MsgBox "Main Menu is open"
Exit Sub
End If
UserForm2.Show False
End Sub
Sub ShowsMM()
MainMenu.Show False
End Sub

rama4672
02-12-2005, 09:45 AM
Thank you for that MD, i am going to mark this as solved as it is now doing everything that i wanted it to do


Regards

Ian

ETracker
02-12-2005, 03:41 PM
I was looking at this post and found it to be very interesting. I have one addtional question to ask, is it possible to have the code check for a list of passwords that is lets say on a hidden sheet1 and only open if one of the passwords are correct. If this could happen, we could have more than on password. Just wondering.

ETracker :dunno :dunno :dunno :cloud9:

Jacob Hilderbrand
02-12-2005, 04:19 PM
Check Here (http://www.vbaexpress.com/kb/getarticle.php?kb_id=33) for an example of how to use multiple user names and passwords.

ETracker
02-13-2005, 08:31 AM
Thanks Jake,

This sample is just what I was looking for.

ETracker :beerchug:

Jacob Hilderbrand
02-13-2005, 04:17 PM
You're Welcome :beerchug:

Take Care