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 : - Option Compare Database
-
Option Explicit
-
Private Sub Form_load()
-
'When the form loads the enter form will open
-
DoCmd.OpenForm "frmLogin"
-
End Sub
-
-
Private Sub cmdLogin_Click()
-
'The dlookup funtion allows the records to be searched within the required table
-
If txtPassword.Value = DLookup("[Password]", "tblUsers", "[UserName]='" & txtUserName.Value & "'") Then
-
'This is opening the main menu and then closing the form enter
-
DoCmd.OpenForm "frmWelcome"
-
DoCmd.Close acForm, "frmLogin"
-
'txtusername.Value = Null
-
'txtpassword.Value = Null
-
ElseIf (txtUserName.Value) Then
-
MsgBox "Enter Username and Password", vbCritical, "Error"
-
txtPassword.Value = ""
-
txtUserName.SetFocus
-
ElseIf (txtPassword.Value) Then
-
MsgBox "Enter password", vbCritical, "Error"
-
txtPassword.SetFocus
-
Else
-
'If incorrect details are entered into any of the fields and enter is selected this would display an error message
-
MsgBox "Log in details are incorrect, Please try again", vbCritical, "Error"
-
'Both the fields would clear
-
txtUserName.Value = Null
-
txtPassword.Value = Null
-
txtUserName.SetFocus
-
-
End If
-
End Sub
-
-
Private Sub txtUserName_LostFocus()
-
'after the user leaves the colour text box, convert the entered
-
'colour to proper case, e.g. all words start with upper case letters
-
txtUserName.Value = StrConv(txtUserName, vbProperCase)
-
End Sub
-
-
Private Sub cmdExit_Click()
-
'This closes the forms down completely
-
DoCmd.Quit
-
End Sub
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.
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: - Private Sub cmdLogin_Click ()
-
'The dlookup funtion allows the
to: - Private Sub cmdLogin_Click()
-
STOP
-
'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: - Private Sub cmdLogin_Click()
-
'The dlookup funtion allows the records to be searched within the required table
-
'
-
Dim z_str_userrecordlookup AS string
-
'
-
z_str_userrecordlookup = dLookup "[Password]", "tblUsers", "[UserName]='" & txtUserName.Value & "'")
-
'
-
If txtPassword.Value = z_str_userrecordlookup Then
-
'This is opening the main menu and then closing the form enter
-
DoCmd.OpenForm "frmWelcome"
-
'(... 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.
There is another point that I feel is worth mentioning, and that is I strongly feel that Passwords should be Case Sensitive, namely: - Password <> PASSWORD
-
Password <> PassworD
-
PaSSword <> PaSsword
-
etc.
-
Password will only equal Password
The following Code will account for that if you wish to use it: -
'***************************** Code intentionally omitted and modified *****************************
-
Dim z_str_userrecordlookup As String
-
-
z_str_userrecordlookup = DLookup("[Password]", "tblUsers", "[UserName]='" & Me![txtUserName] & "'")
-
-
If StrComp(z_str_userrecordlookup, Me![txtPassword], vbBinaryCompare) = 0 Then
-
MsgBox "Passed Validation"
-
Else
-
MsgBox "Does NOT Pass Validation"
-
End If
-
'***************************************************************************************************
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 : - Dim strEncPW As String, strWhere As String
-
-
strWhere = Replace("[UserName]='%N'", "%N", Me.txtUserName)
-
strEncPW = DLookup("[Password]", "[tblUsers]", strWhere)
-
-
If StrComp(strEncPW, EncFunc(Me.txtPassword), vbBinaryCompare) = 0 Then
-
'Passed Validation
-
Else
-
'Did NOT Pass Validation
-
End If
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
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. ...
|
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?
--...
|
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!
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |