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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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
 

brayleyboy

Board Regular
Joined
Nov 24, 2005
Messages
52
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
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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
 

brayleyboy

Board Regular
Joined
Nov 24, 2005
Messages
52

ADVERTISEMENT

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.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
It worked when I tried it. Are you sure you are entering the right password? Note that it's case sensitive.
 

brayleyboy

Board Regular
Joined
Nov 24, 2005
Messages
52
Thanks for your help, there was a problem with the combobox which I have found and sorted, It works perfectly.

Cheers
dave
 

Forum statistics

Threads
1,141,734
Messages
5,708,157
Members
421,549
Latest member
Dtcfire

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
Top