Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2004
    Posts
    5

    Unanswered: login dialog problems

    hi im doing a small project for school and im having problems gettin a login dialog to work. i am using a list box and a text box because i have multiple users on the system. but, when you enter a wrong password for one user, the msgbox pops up 7 times (there are 7 users). i will post the code below. can you see how to resolvee this problem?

    CODE:

    Private Sub command1_click()
    ' If the username and passwords match for the staff name selected then
    ' a message box will display showing a successfull login, followed by the main menu
    ' if an incorrect pass is entered a msgbox will show saying user/pass error and it
    ' will reload the login dialog

    ' *****LOGIN DETAILS FOR DAVID*****
    If List1.Text = "David" And Text2.Text = "david1" Then
    MsgBox "Welcome to the system!"
    Unload Me
    Load menu
    menu.Show
    Else: MsgBox "Incorrect Login Details!"
    Login.Show
    ' resets password field
    Text2.Text = ""
    End If

    ' *****LOGIN DETAILS FOR GARY*****
    If List1.Text = "Gary" And Text2.Text = "gary1" Then
    MsgBox "Welcome to the system!"
    Unload Me
    Load menu
    menu.Show
    Else: MsgBox "Incorrect Login Details!"
    Login.Show
    ' resets password field
    Text2.Text = ""
    End If

    ' *****LOGIN DETAILS FOR JONATHAN*****
    If List1.Text = "Jonathan" And Text2.Text = "jonathan1" Then
    MsgBox "Welcome to the system!"
    Unload Me
    Load menu
    menu.Show
    Else: MsgBox "Incorrect Login Details!"
    Login.Show
    ' resets password field
    Text2.Text = ""
    End If

    ' *****LOGIN DETAILS FOR PETE*****
    If List1.Text = "Pete" And Text2.Text = "pete1" Then
    MsgBox "Welcome to the system!"
    Unload Me
    Load menu
    menu.Show
    Else: MsgBox "Incorrect Login Details!"
    Login.Show
    ' resets password field
    Text2.Text = ""
    End If

    ' *****LOGIN DETAILS FOR ROSS*****
    If List1.Text = "Ross" And Text2.Text = "ross1" Then
    MsgBox "Welcome to the system!"
    Unload Me
    Load menu
    menu.Show
    Else: MsgBox "Incorrect Login Details!"
    Login.Show
    ' resets password field
    Text2.Text = ""
    End If

    ' *****LOGIN DETAILS FOR SCOTTIE*****
    If List1.Text = "Scottie" And Text2.Text = "scottie1" Then
    MsgBox "Welcome to the system!"
    Unload Me
    Load menu
    menu.Show
    Else: MsgBox "Incorrect Login Details!"
    Login.Show
    ' resets password field
    Text2.Text = ""
    End If

    '*****LOGIN DETAILS FOR WAYNE
    If List1.Text = "Wayne" And Text2.Text = "Wayne1" Then
    MsgBox "Welcome to the system!"
    Unload Me
    Load menu
    menu.Show
    Else: MsgBox "Incorrect Login Details!"
    Login.Show
    ' resets password field
    Text2.Text = ""
    End If
    End Sub

    Private Sub Command2_Click()
    ' Ends program
    End
    End Sub

    Private Sub Form_Load()
    'Declares items
    Password = Text2.Text
    User = List1.Text
    ' This loads the staff names on to the form
    List1.AddItem "David"
    List1.AddItem "Gary"
    List1.AddItem "Jonathan"
    List1.AddItem "Pete"
    List1.AddItem "Ross"
    List1.AddItem "Scottie"
    List1.AddItem "Wayne"
    End Sub

  2. #2
    Join Date
    Oct 2004
    Posts
    1
    It seems like you would have to use a Select statement. The reason why you are getting 7 messages back is because you have 7 different if statements. And each if statement is returning its results.

    Code:
     Private Sub Command1_Click() 
    ' If the username and passwords match for the staff name selected then
    ' a message box will display showing a successfull login, followed by the main menu
    ' if an incorrect pass is entered a msgbox will show saying user/pass error and it
    ' will reload the login dialog
     
    Select Case List1.Text
    Case "David"
    	If Text2.Text = "david1" Then
    		MsgBox "Welcome to the system!"
    		Unload Me
    		Load Menu
    		Menu.Show
    	Else: MsgBox "Incorrect Login Details!"
    		Login.Show
    		' resets password field
    		Text2.Text = ""
    	End If
    Case "Gary"
    	If Text2.Text = "gary1" Then
    		MsgBox "Welcome to the system!"
    		Unload Me
    		Load Menu
    		Menu.Show
    	 Else: MsgBox "Incorrect Login Details!"
    		Login.Show
    		' resets password field
    		Text2.Text = ""
    	End If
    Case "Jonathan"
    	If Text2.Text = "jonathan1" Then
    		MsgBox "Welcome to the system!"
    		Unload Me
    		Load Menu
    		Menu.Show
    	 Else: MsgBox "Incorrect Login Details!"
    		Login.Show
    		' resets password field
    		Text2.Text = ""
    	End If
    Case "Pete"
    	If Text2.Text = "pete1" Then
    		MsgBox "Welcome to the system!"
    		Unload Me
    		Load Menu
    		Menu.Show
    	 Else: MsgBox "Incorrect Login Details!"
    		Login.Show
    		' resets password field
    		Text2.Text = ""
    	End If
     
    Case "Ross"
    	If Text2.Text = "ross1" Then
    		MsgBox "Welcome to the system!"
    		Unload Me
    		Load Menu
    		Menu.Show
    	 Else: MsgBox "Incorrect Login Details!"
    		Login.Show
    		' resets password field
    		Text2.Text = ""
    	End If
    Case "Scottie"
    	If Text2.Text = "scottie1" Then
    		MsgBox "Welcome to the system!"
    		Unload Me
    		Load Menu
    		Menu.Show
    	 Else: MsgBox "Incorrect Login Details!"
    		Login.Show
    		' resets password field
    		Text2.Text = ""
    	End If
    Case "Wayne"
    	If Text2.Text = "wayne1" Then
    		MsgBox "Welcome to the system!"
    		Unload Me
    		Load Menu
    		Menu.Show
    	 Else: MsgBox "Incorrect Login Details!"
    		Login.Show
    		' resets password field
    		Text2.Text = ""
    	End If
    End Select
    End Sub
    Private Sub Command2_Click()
    ' Ends program
    End
    End Sub
    Private Sub Form_Load()
    'Declares items
    Password = Text2.Text
    User = List1.Text
    ' This loads the staff names on to the form
    List1.AddItem "David"
    List1.AddItem "Gary"
    List1.AddItem "Jonathan"
    List1.AddItem "Pete"
    List1.AddItem "Ross"
    List1.AddItem "Scottie"
    List1.AddItem "Wayne"
    End Sub

  3. #3
    Join Date
    Oct 2004
    Location
    South Houston Tx.
    Posts
    6
    Slam... is there somewhere online that can teach me select case? I can do if... then... else... in my sleep but something has caught me up on this.

  4. #4
    Join Date
    Sep 2004
    Posts
    15

    Smile

    Why don't you store username and password in a database?It will be easier for you from coding point of view since it will reduce the length of your code!

  5. #5
    Join Date
    Jul 2004
    Location
    Inverurie
    Posts
    628
    This is true. I the format im using is a users tabel called Employees, and it has the following code behind it (the login form itself uses a combobox for the username and a text box for the password. I also created two custom error messages for this).

    Code:
    Private Sub login_button_Click()
    
    Dim db As Database
    
    Set db = CurrentDb
    
    'Check to see if a username is selected
    
    If IsNull(Me.login_select) Or Me.login_select = "" Then
    DoCmd.Close
    DoCmd.OpenForm "error1"
    DoCmd.Close
    Exit Sub
    End If
    
    'Check to see if data is entered into the password box
    
    If IsNull(Me.password) Or Me.password = "" Then
    DoCmd.Close
    DoCmd.OpenForm "error2"
    Exit Sub
    End If
    
    'Check to see if the password entered matches
    'the password in the Employees table.
    
    If Me.password.Value = DLookup("Password", "Employees", "[EmployeeID]=" & Me.login_select.Value) Then
    
    'Close logon form and open splash screen
    
    DoCmd.Close
    DoCmd.OpenForm "frmsplash"
    
    Else
    
    DoCmd.Close
    DoCmd.OpenForm "error2"
    
    End If
    
    End Sub
    It works quite nicely to be honest, and you can then have a feature that allows the users to change their passwords for something that they'll find easier to remember because you are comparing two values (the text box and the database table record). that should get you a higher grade, it shows initiative and creativity.

  6. #6
    Join Date
    Jul 2004
    Location
    Inverurie
    Posts
    628
    Oh, if you do use this method, use the combo box wizard and set it to lookup two fields: the ID and the Username fields. in the properties for the combo box, set column 1 width to 0". This will hide the ID number and just show the username, just incase you decide to lookup the users information if you create a profile viewer.

  7. #7
    Join Date
    Sep 2004
    Posts
    15
    Yes ! This will deffinately work with MS-Access. If you are using VB6 you can also use the following Code.

    ****************************************
    Dim rs As New ADODB.Recordset
    Dim cn As New ADODB.Connection

    Private Sub Form_Load()
    'Open a connection to MS-Access Databse

    cn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
    "Dbq=c:\somepath\mydb.mdb;" & _
    "Uid=admin;" & _
    "Pwd="

    With rs
    .Activeconnection = cn
    .CursorLocation = adUseClient
    .CursorType = adOpenDynamic
    End With

    End Sub

    Private Sub cmdOK_Click()

    'check for correct password

    If Trim(txtUserName) <> "" And Not IsNull(txtUserName) Then

    If Trim(txtPassword) <> "" And Not IsNull(txtPassword) Then
    rs.Open "Select password from <tablename> where username = '" & txtUserName & "'"
    If rs.RecordCount > 0 Then
    If Trim(rs.Fields(0)) = Trim(txtPassword) Then
    'Some Code
    Me.Hide
    Else
    MsgBox "Invalid Password!Try Again", "Login"
    End If
    Else
    MsgBox "Invalid UserName!", "Login"
    txtPassword.SetFocus
    SendKeys "{Home}+{End}"
    End If
    End If

    End If
    End Sub

    Rohit Chitre

Posting Permissions

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