473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 4697
NeoPa
32,556 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,501 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,556 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,834 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,556 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

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

Similar topics

1
by: James | last post by:
Hi, I would like to run a custom script on a linux box via a button on a php page (php webpage hosted on the same linux box). Is this possible? If so , can you give me a pointer in the right...
2
by: Dica | last post by:
i'm just learning java now, so this might be obvious, but not to me. my app needs to do two things: (1) connect to and fetch a list of tasks for a user from a web service (2) connect to mapquest...
12
by: Mactash | last post by:
Folks, I am trying just to run a simple asp commands in the Internet Explorer. ( I have windows XP) When I run this asp lines on ASP Matrix web server it is ok. But, when I run this on IIS...
4
by: SiuLoBow | last post by:
Hi, Is there anyway to detect the ActiveX control is able to run on the browser or not? After I installed the ActiveX control to my system, user sometimes switch the secruity setting to "not...
4
by: Ed | last post by:
Hello, I took a course in asp about 2 years ago and I was practicing with IIS 5.0. Then I put it down for a while. Now trying to get back to it. I can't run asp files from subdirectories of...
2
by: Jenna Olson | last post by:
Hi all- I've never seen this particular issue addressed, but was wondering if there's anything to support one way or another. Say I have a class: class ManipulateData { public:...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
9
by: Brett Wesoloski | last post by:
I am new to VS2005. I changed my program.cs file to be a different form I am working on. But when I go to run the application it still brings up the form that was originally declared as new. ...
8
by: David Thielen | last post by:
Hi; In our setup program how do I determine if I need to run "aspnet_regiis –i" and if so, is there an API I can calll rather than finding that program on the user's disk and calling it? --...
3
by: traceable1 | last post by:
Is there a way I can set up a SQL script to run when the instance starts up? SQL Server 2005 SP2 thanks!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.