Userform as username and password login screen

ajmoore28

New Member
Joined
Mar 15, 2009
Messages
5
I am new to VBA programming and I am having difficulty with a userform that I have set-up. Essentially I have set-up the userform to use as a login in screen, it has a combo box for the username and a textbox for the password. I have a separate hidden worksheet that I am using to maintain my user list which has two columns; one for username and one for password. Currently I have 19 different users. I have set-up the combo box to pull in the usernames using rowsource in properties. I want the user to be able to pick their username from the drop down list then type in their password. The problem is I have no idea how to code this.:confused: I have no problem when we are only talking about one login and one password but I don't know how to set this up for mutiple sets. Any help would be very much appreciated....
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Suppose sheet3 contains user name and password
Add following code in login button click event
Code:
Private Sub cmdLogIN_Click()
    Dim row As Integer, col As Integer
    row = 1
    col = 1

    Dim status As Boolean
    status = False
    For row = 1 To 19
        If Sheet3.Cells(row, col).Value = cmbName.Value And Sheet3.Cells(row, col + 1).Value = txtPwd.Text Then
            status = True
            Exit For
        End If
    Next
    
    If Not status Then
        MsgBox "Log In failed", vbCritical
    Else
        Unload Me
    End If
End Sub
 
Upvote 0
This assumes that the list of user names is in a named range "UserRange" and that the password is one cell to the right of the cell with the username.

This code is for UserForm1 which has the controls ComboBox1, TextBox1, CommandButton1, and Label1.

This goes in the userform's code module.
Code:
Private Sub CommandButton1_Click()
    If 0 <= ComboBox1.ListIndex Then
        Rem case sensitive
        Me.Tag = CStr(Range("UsersRange").Find(ComboBox1.Text).Offset(0, 1).Value = TextBox1.Text)
    End If
    Me.Hide
End Sub

Private Sub UserForm_Initialize()
    Label1.Caption = "Select a User, enter a password and press Enter."
    CommandButton1.Caption = "Enter"
    TextBox1.PasswordChar = Chr(165)
    ComboBox1.List = Range("UsersRange").Value
End Sub

Private Sub ComboBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    KeyAscii = 0
End Sub
In a normal module, the function UserKnowsPassword returns True or False if the user entered the correct info. Demo shows how the function is used in a routine.
Code:
Function UserKnowsPassword() As Boolean
    UserForm1.Show
    UserKnowsPassword = (UserForm1.Tag = "True")
    Unload UserForm1
End Function

Sub demo()
    If UserKnowsPassword Then
        MsgBox "the password entered matches the username chosen."
    Else
        MsgBox "user did not match password"
    End If
End Sub
 
Last edited:
Upvote 0
Awesome...thank you both. It is working exactly as I wanted it to with some minor tweaking. This site is a life saver! :)
 
Upvote 0

Forum statistics

Threads
1,215,287
Messages
6,124,075
Members
449,140
Latest member
SheetalDixit

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