Share via

Query fails. Error 3464 "data type mismatch in criteria expression.

Anonymous
2013-04-09T22:42:35+00:00

can anyboby assist me on this errer. Please see code below. I am new to programming so amonly learning the basics. Thanks in advance.

Private Sub cmd_Login_Click()

On Error GoTo ErrorHandler:

'Check if data is entered in the Username textbox

If IsNull(txtUsername) Or Me.txtUsername = "" Then

MsgBox "Username is required", vbOKOnly, "Invalid Entry!"

txtUsername.SetFocus

Exit Sub

'Check if data is entered in the Password textbox

ElseIf IsNull(txtPassword) Or Me.txtPassword = "" Then

MsgBox "Password is required", vbOKOnly, "Invalid Entry!"

txtPassword.SetFocus

Exit Sub

Else

Dim response As String

Dim strPass As String

strPass = DLookup("Password", "tblUser", "[EmployeeID]='" & Me.txtUsername & "'") & ""

If strPass = "" Then

'No match was found for username

MsgBox "Username doesn't exist! Please try again.", vbOKOnly, "Invalid Username!"

intLogAttempt = intLogAttempt + 1

txtUsername.SetFocus

ElseIf strPass <> txtPassword Then

'Password does not match

MsgBox "Invalid Password!", vbOKOnly, "Invalid Password!"

intLogAttempt = intLogAttempt + 1

txtPassword.SetFocus

End If

'If the user enters incorrect password and username for 3 times database will shutdown

If intLogAttempt = 3 Then

MsgBox "You do not have access to this database.Please contact admin." & vbCrLf & vbCrLf & _

"Application will exit.", vbCritical, "Restricted Access!"

Application.Quit

End If

End If

'Username and password are correct, system will open the Main page

strUser = Me.txtUsername 'Set the value of the strUser declared as Global variable to be displayed in the main page

strRole = DLookup("Role", "tblUser", "[EmployeeID]='" & Me.txtUsername & "'") 'Set the value of the strRole declared as Global variable to be displayed in the main page

DoCmd.Close acForm, "frmUserLogin", acSaveNo

MsgBox "Welcome to MainPage! " & strUser, vbOKOnly, "Welcome!"

'Close login form and open Main page

DoCmd.OpenForm "frmMainPage", acNormal, "", "", , acNormal

ErrorHandler: MsgBox "An unexpected error has occured. Please contact an administrator" & vbNewLine & _

Err.Number & " - " & Err.Description, vbOKOnly + vbCritical, "Critical Error"

End Sub

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Tom van Stiphout 40,201 Reputation points MVP Volunteer Moderator
    2013-04-10T02:49:52+00:00

    I recreated your scenario and it ran fine.

    One important point: it looks like you did not turn on Option Explicit. Open a VBA window, select Tools > Options and check "Require Variable Declaration".

    Then in each code module at the top ensure that this line occurs:

    Option Explicit

    Then in the VBA editor select Debug > Compile and fix up any problems the compiler is pointing out. In your function I had to add:

    Dim intLogAttempt As Integer

    Dim strUser As String

    Dim strRole As String

    The other thing you can do to debug this is to set a breakpoint at the top of this function and run the form again. Step over each line until the error occurs. Which line was it? Inspect the variables.

    0 comments No comments