Consulting

Results 1 to 13 of 13

Thread: Password before running macro

  1. #1
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location

    Password before running macro

    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

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    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

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    http://support.microsoft.com/default...EN-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.

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Make a TextBox then set the PasswordChar Property to *. Then you could check the text in the TextBox instead of an InputBox.

  6. #6
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    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

  7. #7
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    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

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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

  9. #9
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    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

  10. #10
    VBAX Regular
    Joined
    May 2004
    Location
    Louisiana, USA
    Posts
    33
    Location
    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

  11. #11
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Check Here for an example of how to use multiple user names and passwords.

  12. #12
    VBAX Regular
    Joined
    May 2004
    Location
    Louisiana, USA
    Posts
    33
    Location
    Thanks Jake,

    This sample is just what I was looking for.

    ETracker

  13. #13
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •