Login Help

JBurlison

Registered User.
Local time
Today, 18:37
Joined
Mar 14, 2008
Messages
172
I have a login screen and i want to have it so when the user presses the enter button in the password box it logs in. i was trying to lookup the key press event but im not sure how it works.
 
The "On key press event" recieves an ascii code for any key pressed at any time while the control has focus.

IIRC the enter key has ASCII code 13 attached to it....
You could then, if enter is pressed, execute your login code.

Good luck....
 
The event passes on the ASCII code of the key that was pressed. You can use it to see if enter was pressed. The ascii code for Enter is 13, which you can also obtain by checking the ascii code of vbCr.

Example:

Code:
Private Sub txtYourTextBox_KeyPress(KeyAscii As Integer)
    If KeyAscii = Asc(vbCr) Then
        'log in
    End If
End Sub

edit: the mailman beat me to it :D
 
So:

Code:
Private Sub Password_KeyPress(KeyAscii As Integer)
    
        On Error GoTo ErrorRPT

    Static LogonAttempts As Integer
    Dim SaiCurrentUser As String


    SaiCurrentUser = ""
    
If KeyAscii = 13 Then


    If Me.Password.Value = DLookup("[Password]", "[User Name]", "[User Name] = '" & Me.User_name & "'") Then

        SaiCurrentUser = [User Name]
        Forms![infokeeper]![Loginname] = SaiCurrentUser
        DoCmd.Close acForm, "Login Screen", acSaveNo
        DoCmd.OpenForm "Switchboard"

    Else

        LogonAttempts = LogonAttempts + 1

        MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
               "Invalid Entry!"
        Me.Password.SetFocus
        Me.Password.Value = ""



    End If

    If LogonAttempts > 3 Then
        MsgBox "You do not have permission to access this database.Please contact your database administrator."
        Application.Quit

    End If
    End If
    GoTo Endsubtxt
    
ErrorRPT:
Call ErrorRPT1

Endsubtxt:
        

End Sub

This dose not work it just jumps down to the next tab index (The Login Button)
 
Oh I'm sorry, please try the keydown event rather than the keypress event :) (Note: It's KeyCode there not KeyAscii, but it's the same thing)
 
Yes keydown event, sorry my bad, to much Oracle, not enough Access lately :(
 
Its telling me that my password is wrong, i took off the input mask and its right. This works under my login button.

Code:
Private Sub Password_KeyDown(KeyCode As Integer, Shift As Integer)
   On Error GoTo ErrorRPT
        
    Static LogonAttempts As Integer
    Dim SaiCurrentUser As String
    SaiCurrentUser = ""
    
If KeyCode = 13 Then


    If Me.Password.Value = DLookup("[Password]", "[User Name]", "[User Name] = '" & Me.User_name & "'") Then
        
        SaiCurrentUser = [User Name]
        Forms![infokeeper]![Loginname] = SaiCurrentUser
        DoCmd.Close acForm, "Login Screen", acSaveNo
        DoCmd.OpenForm "Switchboard"

    Else

        LogonAttempts = LogonAttempts + 1

        MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
               "Invalid Entry!"
        Me.Password.SetFocus
        Me.Password.Value = ""


    End If

    If LogonAttempts > 3 Then
        MsgBox "You do not have permission to access this database.Please contact your database administrator."
        Application.Quit

    End If
    End If
    GoTo Endsubtxt
    
ErrorRPT:
Call ErrorRPT1

Endsubtxt:

End Sub
 
Its telling me that my password is wrong, i took off the input mask and its right.
Does that mean you entered the right password and got the error message?

Or did this solution work?

There seems to be a indescrepancy here:
If Me.Password.Value = DLookup("[Password]", "[User Name]", "[User Name] = '" & Me.User_name & "'") Then

SaiCurrentUser = [User Name]

These I think should be the same. Try adding your username/password to the messagebox if that is not the issue....
 
HA

Changed
If Me.Password.Value = DLookup("[Password]", "[User Name]", "[User Name] = '" & Me.User_name & "'") Then

SaiCurrentUser = [User Name]

To

Code:
If Me.Password.Value = DLookup("[Password]", "[User Name]", "[User Name] = '" & [User Name] & "'") Then
       
SaiCurrentUser = [User Name]

And it worked! thanks man!
 
If you are refering to a control on a form it is "good practice" to use Me. in front of it like you had previously.

Otherwise you or "your future replacement" may get confused and think you are using the table somehow.

While on the subject. Generaly it is not a good idea to use spaces in column names.
 
Why not any spaces? i have not run into any troubles with that yet
 
Well...
1) Not all databases systems support spaces
When exporting data or connecting or upgrading you could run into troubles
2) Just dont
3) VBA Coding
When doing VBA you must use [] around the fields to refer to them which can be a pain
4) Dont do it.
5) Good practice

Basicaly it is (while you stay within access) a bit of a bother rather than a reall problem, and the major point would be 5. Basicaly because in variables in VBA you cannot do it either. Good practice is to not do it.
Just like it is good practice to prefix your objects in access, tbl = Table, qry = Query etc.
Not because you get errors, but because it is good practice.
 

Users who are viewing this thread

Back
Top Bottom