469,126 Members | 1,287 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,126 developers. It's quick & easy.

Run-time Error '13' : Type Mismatch

Hello Everyone,
I have successfully created a login page for my Order Processing System. When I enter my Username and Password correctly it logs in successfully and moved onto my next form called 'frmWelcome'. However if I enter the incorrect Username and Password I get Error:

Run-time Error '13' : Type Mismatch

I have tried everything, anyone that can help me?

Here is my code :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Private Sub Form_load()
  4. 'When the form loads the enter form will open
  5. DoCmd.OpenForm "frmLogin"
  6. End Sub
  7.  
  8. Private Sub cmdLogin_Click()
  9. 'The dlookup funtion allows the records to be searched within the required table
  10.     If txtPassword.Value = DLookup("[Password]", "tblUsers", "[UserName]='" & txtUserName.Value & "'") Then
  11.    'This is opening the main menu and then closing the form enter
  12.    DoCmd.OpenForm "frmWelcome"
  13.    DoCmd.Close acForm, "frmLogin"
  14.    'txtusername.Value = Null
  15.    'txtpassword.Value = Null
  16.  ElseIf (txtUserName.Value) Then
  17.     MsgBox "Enter Username and Password", vbCritical, "Error"
  18.     txtPassword.Value = ""
  19.     txtUserName.SetFocus
  20.     ElseIf (txtPassword.Value) Then
  21.     MsgBox "Enter password", vbCritical, "Error"
  22.     txtPassword.SetFocus
  23.  Else
  24.    'If incorrect details are entered into any of the fields and enter is selected this would display an error message
  25.    MsgBox "Log in details are incorrect, Please try again", vbCritical, "Error"
  26.    'Both the fields would clear
  27.    txtUserName.Value = Null
  28.    txtPassword.Value = Null
  29.    txtUserName.SetFocus
  30.  
  31.  End If
  32. End Sub
  33.  
  34. Private Sub txtUserName_LostFocus()
  35.     'after the user leaves the colour text box, convert the entered
  36.     'colour to proper case, e.g. all words start with upper case letters
  37.     txtUserName.Value = StrConv(txtUserName, vbProperCase)
  38. End Sub
  39.  
  40. Private Sub cmdExit_Click()
  41.  'This closes the forms down completely
  42.  DoCmd.Quit
  43. End Sub
Dec 5 '12 #1
5 4254
NeoPa
32,162 Expert Mod 16PB
Line #16 is checking a string as if it's a boolean (which it isn't). The code should probably compare the string with something but it isn't.

PS. Line #5 is entirely unnecessary.

Before I leave I will make the point that storing passwords in visible form in a table is quite insecure and always recommended against. It's also a bit more secure to use the name and password in the DLookup() call and ceck only that a record has been returned. That way the actual password isn't even seen in the code.
Dec 5 '12 #2
zmbd
5,400 Expert Mod 4TB
Please do the following:
I have an idea as to what is happening at line 11 in that your DLOOKUP is returning an invalid result; however, you need to do the following to confirm

- In the VBA editor: {Ribbon:Debug:Compile}
Fix any bugs that show up and save... Run {Ribbon:Debug:Compile} until no errors are returned.

- Change your code in lines 9 and 10 of the posted From:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdLogin_Click () 
  2. 'The dlookup funtion allows the 
to:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdLogin_Click()
  2. STOP
  3. 'The dlookup funtion allows the
Start your database... now [F8] thru the code and enter the wrong password... you'll see that you return a null.

You would have found this if you coded it like:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdLogin_Click()
  2.    'The dlookup funtion allows the records to be searched within the required table 
  3.    '
  4.    Dim z_str_userrecordlookup AS string
  5.    '
  6.    z_str_userrecordlookup = dLookup "[Password]", "tblUsers", "[UserName]='" & txtUserName.Value & "'")
  7.    '
  8.    If txtPassword.Value = z_str_userrecordlookup Then
  9.       'This is opening the main menu and then closing the form enter
  10.       DoCmd.OpenForm "frmWelcome"
  11.       '(... remaining code ...)
you would have returned a null value to the z_str_userrecordlookup and errored on that line: DLookup Function
DLookup function evaluates expr against the entire domain. Any field that is included in criteria must also be a field in domain; otherwise, the DLookup function returns a Null (Null: A value you can enter in a field or use in expressions or queries to indicate missing or unknown data. In Visual Basic, the Null keyword indicates a Null value. Some fields, such as primary key fields, can't contain a Null value.)
Change line 7 in my code to read:
z_str_userrecordlookup = NZ(DLookup("[Password]", "tblUsers", "[UserName]='" & txtUserName.Value & "'"), 0) and you don't get the error.

It used to be taught in CompSci courses that all functions should be returned to a variable - in fact, that had to be done at one point in time. It is a sad fact that programmers now take shortcuts by placing functions within logic blocks as doing so makes the code harder to troubleshoot.
Dec 5 '12 #3
NeoPa
32,162 Expert Mod 16PB
Z makes a good point about compiling the code. I shouldn't have overlooked that. Here are a few tips that should help - Before Posting (VBA or SQL) Code.
Dec 5 '12 #4
ADezii
8,800 Expert 8TB
There is another point that I feel is worth mentioning, and that is I strongly feel that Passwords should be Case Sensitive, namely:
Expand|Select|Wrap|Line Numbers
  1. Password <> PASSWORD
  2. Password <> PassworD
  3. PaSSword <> PaSsword
  4. etc.
  5. Password will only equal Password
The following Code will account for that if you wish to use it:
Expand|Select|Wrap|Line Numbers
  1. '***************************** Code intentionally omitted and modified *****************************
  2. Dim z_str_userrecordlookup As String
  3.  
  4. z_str_userrecordlookup = DLookup("[Password]", "tblUsers", "[UserName]='" & Me![txtUserName] & "'")
  5.  
  6. If StrComp(z_str_userrecordlookup, Me![txtPassword], vbBinaryCompare) = 0 Then
  7.   MsgBox "Passed Validation"
  8. Else
  9.   MsgBox "Does NOT Pass Validation"
  10. End If
  11. '***************************************************************************************************
Dec 5 '12 #5
NeoPa
32,162 Expert Mod 16PB
That's a nuisance ADezii. I'd not noticed that before. That means putting an encrypted version of the password entered as part of the criteria won't be a reliable test, as SQL seems to compare/filter by letter rather than by case/binary.

Bearing in mind the more secure way of testing for passwords is storing them as encrypted values and never holding the actual password as text in your module, doing it properly would require that one used the technique you suggest with some standard (Used to store the data as well as check it) function procedure to encrypt / obscure the actual password value :
Expand|Select|Wrap|Line Numbers
  1. Dim strEncPW As String, strWhere As String
  2.  
  3. strWhere = Replace("[UserName]='%N'", "%N", Me.txtUserName)
  4. strEncPW = DLookup("[Password]", "[tblUsers]", strWhere)
  5.  
  6. If StrComp(strEncPW, EncFunc(Me.txtPassword), vbBinaryCompare) = 0 Then
  7.     'Passed Validation
  8. Else
  9.     'Did NOT Pass Validation
  10. End If
Dec 6 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by James | last post: by
2 posts views Thread by Dica | last post: by
12 posts views Thread by Mactash | last post: by
13 posts views Thread by Bob Day | last post: by
9 posts views Thread by Brett Wesoloski | last post: by
8 posts views Thread by David Thielen | last post: by
3 posts views Thread by traceable1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.