Combobox as a user login

brayleyboy

Board Regular
Joined
Nov 24, 2005
Messages
52
Hi, I am using a userform to create a user login and have what is probably a vey simple question.

I have a combobox that lists the usernames (DBS!V8:10) and have the following code to set a password depending on which username is selected.

Code:
Private Sub ComboBox1_Change()
Dim pword As String
If UserForm1.ComboBox1.Value = "DBS!V8" Then
pword = "password1"
End If
If UserForm1.ComboBox1.Value = "DBS!V9" Then
pword = "Password2"
End If
If UserForm1.ComboBox1.Value = "DBS!V10" Then
pword = "Password3"
End If
End Sub

I then have a text box (textbox1) to input the password in and a command button to check the password against the username and either allow access or inform the user of an incorrect password (see code below).

Code:
Private Sub CommandButton1_Click()
If UserForm1.TextBox2.Value <> "????????" Then
MsgBox "Password Incorrect"
Exit Sub
Else
MsgBox "Access Granted"
End If
Unload UserForm1
End Sub

Can you tell me if/how I can reference the password declared in the first bit of code in the second bit in place of the question marks?

Many Thanks
Dave[/code]
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Move:

Dim pword As String

to the top of the module, before any procedures. Then it becomes available to all the procedures and you can use:

If UserForm1.TextBox2.Value <> pword Then
 
Upvote 0
Apologies, I am very new to this. When you say to move it to the top of the module before any procedures I'm not sure exactly what you mean.

I have moved it above the "Private Sub ComboBox1_Change()" line as below:

Code:
Dim pword As String 
Private Sub ComboBox1_Change() 
If UserForm1.ComboBox1.Value = "DBS!V8" Then 
pword = "password1" 
End If 
If UserForm1.ComboBox1.Value = "DBS!V9" Then 
pword = "Password2" 
End If 
If UserForm1.ComboBox1.Value = "DBS!V10" Then 
pword = "Password3" 
End If 
End Sub

Private Sub CommandButton1_Click() 
If UserForm1.TextBox2.Value <> pword Then 
MsgBox "Password Incorrect" 
Exit Sub 
Else 
MsgBox "Access Granted" 
End If 
Unload UserForm1 
End Sub

But it doesnt seem to recognise the password and always tells me it is incorrect
 
Upvote 0
I expect that's because eg:

If UserForm1.ComboBox1.Value = "DBS!V8" Then

is testing for the string DBS!V8 rather than what's in the cell and the password isn't being set. Try changing that to:

If UserForm1.ComboBox1.Value = Worksheets("DBS").Range("V8").Value Then
 
Upvote 0
I have Ammended the code as you said (see below)

Code:
Dim pword As String
Private Sub ComboBox1_Change()
If UserForm1.ComboBox1.Value = Worksheets("DBS").Range("V8").Value Then
pword = "password1"
End If
If UserForm1.ComboBox1.Value = Worksheets("DBS").Range("V9").Value Then
pword = "Password2"
End If
If UserForm1.ComboBox1.Value = Worksheets("DBS").Range("V10").Value Then
pword = "Password3"
End If
End Sub

Private Sub CommandButton1_Click()
If UserForm1.TextBox2.Value <> pword Then
MsgBox "Password Incorrect"
Exit Sub
Else
MsgBox "Access Granted"
End If
Unload UserForm1
End Sub

It works fine if I select the username in DBS!V8 and use the password "password1". However it doesnt seem to work for the other user names at all, I get the message box that says "incorrect password". It is as though the password is not being defined when I change the combobox to the other usernames.
 
Upvote 0
It worked when I tried it. Are you sure you are entering the right password? Note that it's case sensitive.
 
Upvote 0
Thanks for your help, there was a problem with the combobox which I have found and sorted, It works perfectly.

Cheers
dave
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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