473,883 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Username and password validation on Login Page in MS Access

25 New Member
Hi, anyone here could help me on my codes. I want to validate my username and password from my user table. If username is not found in user table a message should appear "Username Invalid", and If password is invalid a message should appear "Password is invalid." and if Username is correct and password is Invalid a messages should be "Password is invalid", if password is correct and username is wrong a message should appear "Password is invalid". Here's my code..I am using MS Access (VBA)Programmin gThanks in advance

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdLogin_Click()
  2.     Call Login
  4. End Sub
  6. Public Sub Login()
  7. On Error GoTo ErrorHandler:
  8.     If IsNull(txtUsername) = True Then 'Check Username
  9.         MsgBox "Username is required"
  10.         txtUsername.SetFocus
  11.     End If
  13.     If IsNull(txtPassword) = True Then 'Check Password
  14.         MsgBox "Password is required"
  15.         txtPassword.SetFocus
  17.      End If
  19.      'If (txtUsername) = True And (txtPassword) = True Then
  22.     'Compare value of txtUsername with the saved EmployeeID in tblUser
  23.     If Me.txtUsername.Value = DLookup("EmployeeID", "tblUser", "[EmployeeID]='" & Me.txtUsername.Value & "'") Then
  24.         strUser = Me.txtUsername.Value 'Set the value of the strUser declared as Global variable
  25.         strRole = DLookup("Role", "tblUser", "[EmployeeID]='" & Me.txtUsername.Value & "'") 'set the value of strRole declared as Global Variable
  26.         DoCmd.Close acForm, "frmUserLogin", acSaveNo
  27.         MsgBox "Welcome to Main Page!" & strUser, vbOKOnly, "Welcome"
  28.         'Close logon form and open Main page
  29.         DoCmd.OpenForm "frmMainPage", acNormal, "", "", , acNormal
  32.     Else
  33.         MsgBox "Invalid Username! Please try again.", vbOKOnly, "Invalid Username"
  34.         intLogAttempt = intLogAttempt + 1
  35.         txtUsername.SetFocus
  36.     End If
  39.     'Compare value of txtPassword with the saved Password in tblUser
  40.     If Me.txtPassword.Value = DLookup("Password", "tblUser", "[EmployeeID]='" & Me.txtUsername.Value & "'") Then
  41.         strUser = Me.txtUsername.Value 'Set the value of the strUser declared as Global variable
  42.         strRole = DLookup("Role", "tblUser", "[EmployeeID]='" & Me.txtUsername.Value & "'") 'set the value of strRole declared as Global Variable
  43.         DoCmd.Close acForm, "frmUserLogin", acSaveNo
  44.         MsgBox "Welcome to Main Page!" & strUser, vbOKOnly, "Welcome"
  45.         'Close logon form and open Main page
  46.         DoCmd.OpenForm "frmMainPage", acNormal, "", "", , acNormal
  48.     Else
  49.         MsgBox "Invalid Password! Please try again.", vbOKOnly, "Invalid Password"
  50.         intLogAttempt = intLogAttempt + 1
  51.         txtPassword.SetFocus
  52.     End If
  54.     'If the user enters incorrect password for 3 times database will shutdown
  55.     If intLogAttempt = 3 Then
  56.     MsgBox "You do not have access to this database.Please contact admin." & vbCrLf & vbCrLf & _
  57.         "Application will exit.", vbCritical, "Restricted Access!"
  58.         Application.Quit
  59.     End If
  61. ErrorHandler:
  62. End Sub
Jan 31 '12
18 14847
25 New Member
I rearranged the codes and removed the exit sub on condition if strPass = "" and if strPass<>, I used If elseif endif on it and added intLogAttempt = intLogAttempt + 1 on each condition so that it will call the condition "If user enters incorrect password for 3 times database will shutdown"
Feb 6 '12 #11
2,322 Recognized Expert Moderator Top Contributor
Is it working like you want it to? Did you read the artivles i linked for you? Especially the one about debugging? There is a se tion in there about stepping through code that is very usefull (also) when you are new and trying to see what the code does.

Please post your revised code.
Feb 6 '12 #12
25 New Member
Yes, it is working already... I have read it, thanks. Here's the revised codes.

Expand|Select|Wrap|Line Numbers
  1.  Public Sub Login()
  3. On Error GoTo ErrorHandler:
  4. 'Check if data is entered in the Username textbox
  5.     If IsNull(txtUsername) Or Me.txtUsername = "" Then
  6.         MsgBox "Username is required", vbOKOnly, "Invalid Entry!"
  7.         txtUsername.SetFocus
  8.         Exit Sub
  9. 'Check if data is entered in the Password textbox
  10.     ElseIf IsNull(txtPassword) Or Me.txtPassword = "" Then
  11.         MsgBox "Password is required", vbOKOnly, "Invalid Entry!"
  12.         txtPassword.SetFocus
  13.         Exit Sub
  14.     Else
  16.         Dim response As String
  17.         Dim strPass As String
  18.         strPass = DLookup("Password", "tblUser", "[EmployeeID]='" & Me.txtUsername & "'") & ""
  20.         If strPass = "" Then
  21.          'No match was found for username
  22.              MsgBox "Username doesn't exist! Please try again.", vbOKOnly, "Invalid Username!"
  23.              intLogAttempt = intLogAttempt + 1
  24.              txtUsername.SetFocus
  26.         ElseIf strPass <> txtPassword Then
  27.         'Password does not match
  28.             MsgBox "Invalid Password!", vbOKOnly, "Invalid Password!"
  29.             intLogAttempt = intLogAttempt + 1
  30.              txtPassword.SetFocus
  31.         End If
  33.       'If the user enters incorrect password and username for 3 times database will shutdown  
  34.         If intLogAttempt = 3 Then
  35.             MsgBox "You do not have access to this database.Please contact admin." & vbCrLf & vbCrLf & _
  36.             "Application will exit.", vbCritical, "Restricted Access!"
  37.             Application.Quit
  38.      End If
  40.     End If
  42.          'Username and password are correct, system will open the Main page
  43.         strUser = Me.txtUsername 'Set the value of the strUser declared as Global variable to be displayed in the main page
  44.         strRole = DLookup("Role", "tblUser", "[EmployeeID]='" & Me.txtUsername & "'") 'Set the value of the strRole declared as Global variable to be displayed in the main page
  46.         DoCmd.Close acForm, "frmUserLogin", acSaveNo
  47.         MsgBox "Welcome to MainPage! " & strUser, vbOKOnly, "Welcome!"
  49.         'Close login form and open Main page
  50.         DoCmd.OpenForm "frmMainPage", acNormal, "", "", , acNormal
  53. ErrorHandler:
  55. End Sub 
Feb 6 '12 #13
2,322 Recognized Expert Moderator Top Contributor
Hi Midzie.

Its very nice to see you have worked on your code. Its clear that you have now understood some basic principles that were obviously lacking in your first attempts.

Keep working on it, and you will soon learn that VBA can perform wonders for your application.

If I should add one comment it would be in regards to your errorhandling. Its fine to exit without really doing anything, but in my oppinion you should as minimum inform the user of the error occuring. A simple modification at the bottom like so should suffice

Expand|Select|Wrap|Line Numbers
  1. Exit sub 'Add this line, otherwise the errorhandler code will run even when no error has occured.
  2. ErrorHandler:
  3.   MsgBox "An unexpected error has occured. Please contact an administrator" & vbnewline & _
  4.        Err.Number & " - " & Err.Description,vbokOnly+vbcritical,"Critical Error"
  5. End Sub
Feb 6 '12 #14
32,584 Recognized Expert Moderator MVP
@Smiley - Did you see something in the post #8 code that was wrong? I got the impression Midzie was already happy by that point.

@Midzie - Good progress. It's always hard working in foreign languages, but you're putting things into practice and thinking logically. I notice you've even started using the [ CODE ] tags when posting code now. I see Smiley has been very helpful for you on this thread. You may like to select the post of his that you feel was the most directly responsible for answering the main question, and select the Best Answer button found with it. This enables others with a similar problem to find an answer more obviously and easily :-)
Feb 6 '12 #15
2,322 Recognized Expert Moderator Top Contributor
Hi NeoPa, thanks for pointing that blunder out.

I think that perhaps when I followed the email link I received upon update of the thread with post #8 I accidentally looked at #4 and got the (wrong) impression that Midzie had not worked at all with the links provided, and thus got agitated and made my post #9.

To Midzie
I sincerely apologize for my unfounded outburst.
Feb 6 '12 #16
8,834 Recognized Expert Expert
One small point to mention is that, at least in my opinion, all Passwords should be Case-Sensitive. This will add another Layer of Security, and is actually something that most Users will expect and are accustomed to. The following Code will always make sure that only an 'Exact' Match will pass the Test:
Expand|Select|Wrap|Line Numbers
  1. Dim strPass As String
  2. Dim txtPassword As String
  4. strPass = "Ren487deZvous"
  5. txtPassword = "Ren487dezvous"
  7. 'Passwords will NOT MATCH because of the Lower Case 'z' in txtPassword
  8. If StrComp(strPass, txtPassword, vbBinaryCompare) Then
  9.   MsgBox "The Passwords are NOT an exact Match - Remember Passwords " & _
  10.          "are Case Sensitive."
  11.     Exit Sub
  12. End If
Feb 6 '12 #17
32,584 Recognized Expert Moderator MVP
@Smiley - An unfortunate oversight. When you post as much as you do it's inevitable from time-to-time (I've made similar mistakes).

@ADezii - That's a good point. I would add to that, that passwords shouldn't be stored in plain text. That is to say that a 'processed' version should be stored in the table with the user details and any password being checked should also be 'processed' prior to the comparison. So :
Expand|Select|Wrap|Line Numbers
  1. 'strPass value from table - Stored pre-processed
  2. If StrComp(strPass, Process(Me.txtPassword), vbBinaryCompare) <> 0 Then
  3.     Call MsgBox("The Passwords are NOT an exact Match - " & 
  4.                 "Remember Passwords are Case Sensitive.")
  5.     Exit Sub
  6. End If
  8. Private Function Process(strPW As String) As String
  9.     ' conversion / processing code here
  10. End Function
Clearly Process() is an unspecified function here, but it can be whatever you want it to be as long as it produces consistent results from the same input. Some links that may prove helpful for this are :
  1. AES Encryption Algorithm for VBA and VBScript.
  2. RC4 Encryption Algorithm for VBA and VBScript.
  3. SHA2 Cryptographic Hash Algorithm for VBA and VBScript.
Feb 6 '12 #18
25 New Member
Hi NeoPa/Smiley/ADezii - Thanks to all your efforts for helping me with my codes. More powers to all of you and to this website. To all beginners like me, let's keep on learning new things:-)
Feb 7 '12 #19

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

Similar topics

by: Matt | last post by:
If I want to have a username/password dialog in my page, how can I add http header in ASP for username/password dialog? I want to hardcode the username/password in my page and check the authentication. Please advise. thanks!!
by: Dam6 | last post by:
Okay... Using vb .net within DW MX2004, connecting to an access database: Background: I have created a simple login.aspx page that is supposed to re-direct to default.aspx using FormsAuthentication.RedirectFromLoginPage. The data is correct from testing the dataset etc and the page also informs me that I have entered an incorrect user / password. Problem: When I go to http://localhost/Modify/Login.aspx and enter the username and
by: Shakun | last post by:
Hi All, This is my 1st posting to this group. Can any1 help me with the "Remember Me" which is there in a login form. Im pasting the code below. Im not able to set a cookie.. Thanks, Shakun Vohra
by: phamn1985 | last post by:
I m trying to make a login page via Microsoft access. This is my table .. Table name : tblLogin PK : loginID(Auto number) Attributes : username , password. I make a login form in Microsoft Access which has 2 textfields name : txtUser and txtPass with a login button name cmdLog . How can i make the login button works ( open a form call tblMain) when the righ
by: tolkienarda | last post by:
Hi all I work for a small webdesign company and we have remote hosting. i built a mysql database with phpmyadmin on the server. i then downloaded and modified a php login page. i am continuing to get the ---invalid username or password--- i am not getting the E_USER_ERROR so i don't know if there is a problem with the php, how i set up the database or mabe somthing in between. so everyone knows all of my server interface is GUI. I use a company...
by: yadev | last post by:
can you tell me how to write the code for username and password in php
by: rajamohammed | last post by:
Can you please help me on this I can access one shared folder (folder is in another mahine) from my machine using java program. But From another server machine while accessing the same folder it asks username & password. How can I access it using program. Please reply as soon as possible. Thanks
by: webmaniac | last post by:
Hi, Does anyone know, How to create a Login Page & read the username & password from xml file. Like my XML file is going to store the username & password. & when user is going to put the username & password in Login Page, It will check it on XML File. Grab the Username & print Hi Username. Thanks
by: adweaver | last post by:
Hello All, I'm new to the world of php. I've just had a site designed for me by a company, and I'm now trying to manage and grow it, so it will suit my needs. The site was built in a folder called mysite.com/test. I coppied this folder a number of times, so I now have mysite.com/test2, mysite.com/test3 etc. Each folder acts as its own landing page. they setup an interface where we can control the upsells presented to the customer;...
by: Bob Smith | last post by:
I have to come up with a user authenication page the logs the user in and also gives them access to do the right things. without using server side scripting. Just javacript and access. I have attached the code and the access file and have got started on a few things. I first need to create a login page with the fields username and password have that check the access database and then proceed it to a page to do the following depending on the...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.