User form to enter password

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
Ok I have created a user form with a textbox, enter button & cancel button and got it to show, and I can enter the password as asterisks, but I don’t know how do to get it do something. I would like the enter button to fire the code and the cancel button to exit the code. Can anybody please point me in the right direction?. I have absolutely no experience with user forms
Any help is as always very much appreciated
VBA Code:
Sub TEST()
ActiveSheet.Protect Password:="Password"
UserForm1.Show
If Textbox1.Value = "Password2" Then
'Press enter button
'Do Stuff


'Press cancel button
Unload UserForm1
ActiveSheet.Protect Password:="Password"

End If
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try the solution below:

1. Create two textboxes one with username as name and other textbox name to be password
2. Put a labels next to your username and password and name them to your liking
3. Name your command button something. I named mine Lgin
4.

VBA Code:
Private Sub Lgin_Click() 'Lgin is name of commandbutton or "enter" button
    If Me.UserName.Value = "Assign a username" Then ' Give a username
        If Me.Password.Value = "Password2" Then ' Give a password
            LoginFlag = True
            ''Do Stuff
            Exit Sub
        End If
    End If
    MsgBox "Sorry, Incorrect Login Details...Try Again", vbOKOnly, "Message from..."
End Sub
 
Upvote 0
Jimmypop

Thanks for assisting I have created the user form with 2 text boxes and one command button as picture

Pasted the code into a module, took the private off the sub (Sub Login_Click)so I could run it as a test and I am getting invalid use of ME keyword error
Capture.PNG

VBA Code:
Sub Login_Click() 'Login is name of commandbutton or "enter" button
    If Me.UserName.Value = "Ash" Then ' Give a username
        If Me.Password.Value = "Password2" Then ' Give a password
            LoginFlag = True
            Range("B4").Select
            Exit Sub
        End If
    End If
    MsgBox "Sorry, Incorrect Login Details...Try Again", vbOKOnly, "Message from..."
End Sub
 
Upvote 0
Did you paste the code into the user form's code module (right-click on user form > View Code) or in a separate module?

If it's a separate module (which it sounds like) then you will need to replace the Me keyword with UserForm1 (or whatever you have named your user form). For example...
VBA Code:
Sub Login_Click() 'Login is name of commandbutton or "enter" button
    If UserForm1.UserName.Value = "Ash" Then ' Give a username
        If UserForm1.Password.Value = "Password2" Then ' Give a password
            LoginFlag = True
            Range("B4").Select
            Exit Sub
        End If
    End If
    MsgBox "Sorry, Incorrect Login Details...Try Again", vbOKOnly, "Message from..."
End Sub

Alternatively paste the code directly into the login command buttons' click event in the user form's code module, as Jimmypop suggested. Then the Me keyword will refer to the user form and that error should go away.
 
Upvote 0
Thanks Sunjinsak

Apologies but this is all new to me and I am really struggling

When I run this code the user form comes up I enter the user name & password press the command button Login & nothing happens.

Also Do I have to have a user name can I just have a password
VBA Code:
Sub Login_Click() 'Login is name of commandbutton or "enter" button
UserForm1.Show
    If UserForm1.UserName.Value = "Ash" Then ' Give a username
        If UserForm1.Password.Value = "Password2" Then ' Give a password
            LoginFlag = True
            Range("B4").Select
            Exit Sub
        End If
    End If
    MsgBox "Sorry, Incorrect Login Details...Try Again", vbOKOnly, "Message from..."
End Sub
 
Upvote 0
Bagsy

Do you call the userform with a shape or activex control button on your worksheet?

If so then your sub should read something like

VBA Code:
Sub Login_Click()
 UserForm1.Show
End Sub

Then in the userform code (right click userform and view code) should look like

VBA Code:
Private Sub Enter_Click()
 If UserForm1.Username.Value = "Ash" Then ' Give a username
        If UserForm1.Password.Value = "Password2" Then ' Give a password
            LoginFlag = True
            Range("B4").Select
            Exit Sub
        End If
    End If
    MsgBox "Sorry, Incorrect Login Details...Try Again", vbOKOnly, "Message from..."
End Sub

I named the enter button on the userform Enter
 
Upvote 0
Thanks Sunjinsak

Appreciate you helping

Apologies for the late reply, but been working on two fronts

What I have is some code which brings up an input box to enter data, but I need the password to show as *****, so I am hoping to incorporate a user form in the code, instead of an input box.
VBA Code:
Sub AshSignature()
 Dim Password As String
 Dim Ws As Worksheet, wsPinkCopy As Worksheet
  Set wsPinkCopy = Worksheets("Pink Copy1")
  Sheets("Pink Copy1").Select
ActiveSheet.Unprotect Password:="password"
 Do Until Password = "Apple Crumble"
 Password = InputBox("Please enter password below", "Password", "????")
 If Password = "" Then
 Exit Sub
 End If
 Loop
Range("O8:P13") = "A.Sewell"
ActiveSheet.Protect Password:="password"
Call EnterAshSignature
End Sub
 
Upvote 0
Hi Bagsy,

So VBA is an event-driven language. At its most fundamental that means that code is executed when an event occurs that triggers that code. So for example when a user form is initialised, or when a button is clicked etc. So you want to call your code when the correct event to trigger it occurs, if that makes sense?

Taking your example code above and your description of what you want to achieve we can modify both the code and the structure of your project a little. Unfortunately I can't post screen shots or a sample workbook as I'm in work at the moment and my employer blocks access to all file upload/hosting facilities as we work with very sensitive data, so I'll try my best to describe what you need to do in words and sample code alone.

I've assumed a few things from your previous posts:
  • Your user form containing the password text box and the login button is called UserForm1
  • The text box to input the password is called Password
  • The login button is called Login
If any of those differ then change the references to them in the sample code accordingly. It is also worth noting that this is just one way of achieving what you want. As with anything like this there are multiple ways of achieving the same thing that may work better within the parameters of your project.

Now, first of all you will want a way to show UserForm1 when a user needs to enter their password. The code to show the user form is simply:
VBA Code:
    UserForm1.Show
As to where to put this code that will depend on how and when you want the user to input their password. For the sake of example let's say you have a command button embedded in the worksheet called EnterPassword that the user must click on. In that case you'd put the above code in the button's click event, like so:
VBA Code:
    Private Sub EnterPassword_Click()
        UserForm1.Show
    End Sub
To get to the button's click event right-click on it and select 'View Code'. If you want to show the user form in a different way then let us know what that is and we can help you with that.

Now, once UserForm1 is displayed the user will enter their password in the Password text box, then click the Login button. So you want the code that checks the password then takes the rest of the actions to fire when the Login button is clicked. You can either put the code directly in the Login button's click event, or you can put it in a separate code module (Insert > Module in the editor) and then call it from the button's click event. For this example we'll use the latter. So, insert a new code module and paste the following modification of your above code into the module:
VBA Code:
Public Sub AshSignature()

    Dim Password As String
    Password = "Apple Crumble"
    
    'Check if the password is correct
    If UserForm1.Password.Value <> Password Then
        'Display a message and exit the sub-routine if the password is not correct
        MsgBox "Sorry, incorrect password. Please try again.", vbOKOnly, "Incorrect Password"
        Exit Sub
    End If
    
    'The following code will only be executed if the password is correct...

    Dim wsPinkCopy As Worksheet
    Set wsPinkCopy = Worksheets("Pink Copy1")
    
    wsPinkCopy.Unprotect Password:="password"
    wsPinkCopy.Range("O8:P13") = "A.Sewell"
    wsPinkCopy.Protect Password:="password"
    
    'Close the password input user form now that we don't need it
    Unload UserForm1

    Call EnterAshSignature

End Sub

Then, in the click event of the Login button, simply do:
VBA Code:
Private Sub Login_Click()
    Call AshSignature
End Sub

I hope I've not over-complicated that for you but you seemed to be struggling with how and where to put your code so I thought a basic explanation of how 'events' and code relate to each other in VBA might help. Once again this is just one way of achieving what you want and not necessarily the best - but it should work.

Hope it helps :)
 
Upvote 0
Thanks Sunjinsak
This is really helpfull, I will have a go over the next few days, to see if I can sort it out.
Many thanks for giving me your time
Gary
 
Upvote 0
You're welcome :)

Let us know how you get on and if you have any more questions. Good luck!
 
Upvote 0

Forum statistics

Threads
1,216,192
Messages
6,129,440
Members
449,509
Latest member
ajbooisen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top