+ Reply to Thread
Results 1 to 5 of 5

userform button code needs attention to work correctly

  1. #1
    Registered User
    Join Date
    12-15-2012
    Location
    Pensacola, Florida
    MS-Off Ver
    Excel 2010
    Posts
    3

    userform button code needs attention to work correctly

    I have a userform that opens up, prior to my employees using a worksheet form. This userform has a combobox for their name & a text box for their password. A command button that enables them to enter the worksheet area. The code worked fine until i changed from a textbox to a combobox. At first they were typing their name into a textbox =username, but i added a combobox instead to quicken the process. Now I get "Please Try Again", which you will see in this code or my uploaded file.

    Thanks, Hope Someone Can Help!!


    Private Sub UserForm_Initialize()
    'Populate Employee combo box.
    Dim rngEmployee As Range
    Dim ws As Worksheet
    Set ws = Worksheets("Setup")
    For Each rngEmployee In ws.Range("Employee")
    UserForm1.ComboBox1.AddItem rngEmployee.Value
    Next rngEmployee
    End Sub

    Private Sub CommandButton1_Click()
    Dim username, password As String

    username = ComboBox1.Text
    password = TextBox2.Text

    If username = "Ramsey" And password = "admin" Then
    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    End If

    If username = "Ronnie" And password = "admin" Then
    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    End If

    If username = "Doug" And password = "admin" Then
    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    End If

    If username = "Fish" And password = "admin" Then
    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    End If

    If username = "Willie" And password = "admin" Then
    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    Else
    MsgBox "Please Try Again", vbCritical

    End If
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: userform button code needs attention to work correctly

    The problem is the last If statement.

    Unless the user has entered 'Willie' for username and 'admin' for password you will get the 'Please Try Again' message box.

    So if entered 'Doug' and 'admin' I would get both message boxes.

    Try this.
    Please Login or Register  to view this content.
    I've grouped all the users together because they have the same password.

    If that's not the case then the Select Case can be changed to have an individual case for each user.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-15-2012
    Location
    Pensacola, Florida
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: userform button code needs attention to work correctly

    Thanks for the quick reply, passwords will be different. I have used your code and split up cases, but i am getting an error. I am new to VB, so i have never used (CASE). The error is a compile error, Case without Select Case!!!!!! I appreciate your help

    MY NEW CODE


    Private Sub CommandButton1_Click()
    Dim username As String
    Dim password As String

    username = ComboBox1.Text
    password = TextBox2.Text
    Select Case username
    Case "Ramsey"

    If password = "5901" Then

    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    Case "Ronnie"

    If password = "admin" Then

    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    Case "Doug"

    If password = "admin" Then

    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    Case "Fish"

    If password = "elvis123" Then

    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form

    Case "Willie"

    If password = "admin" Then

    MsgBox "Thank You", vbInformation
    Unload Me ' unload this form


    Else
    MsgBox "You've entered an incorrect password."
    TextBox2.Text = vbNullString
    End If
    Case Else
    MsgBox "Username not recognised."
    End Select

    End Sub

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: userform button code needs attention to work correctly

    Can you add code tags to the code you posted - makes it a lot easier to read.

  5. #5
    Registered User
    Join Date
    12-15-2012
    Location
    Pensacola, Florida
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: userform button code needs attention to work correctly

    I am trying to grasp the code tags, i understand the point. Will reply afterwards, Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1