473,387 Members | 1,528 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,387 software developers and data experts.

problems with Login Form with VBA that validates username and password?

2
Hey everyone,

I know this question has been asked many times, but I can't seem to get a handle on it.

In a nutshell, I just want a form that validates the username and password someone puts in that's in a database. I know the code will the use the DLookup function, but that's where I get a little mixed up. I know what it's used for, but I'm not sure how to make use of it.

I currently have a table called "klanten" wich contain the rows "login" and "wachtwoord" (with various other rows). What i have at this moment is the code to check is somthing is filled in in the form.

Private Sub cmmndLogIn_Click()

'Check to see if data is entered into login

If IsNull(login) Or login = "" Then
MsgBox gelieve een login in te vullen.", vbOKOnly, "Required Data"
.login.SetFocus
Exit Sub
End If
''Coded in by Bryan


If anyone could help explain to me how I would get this to work, that would be great!

Thanks in advance!
Nov 26 '14 #1
4 1145
twinnyfo
3,653 Expert Mod 2GB
Bryan,

First, please use Code Tags when posing code to aid us in reviewing your code. Also, make sure you are doing a direct copy and paste from your project (as it does not appear you have done that with yours).

Based on your code, I am surprised you don't get a group of errors--which is why it is difficult to troubleshoot from the start.

Next, I'm not quite sure what your exact question is. You talk about looking up Login and Password credentials form a table, using DLookup, but your code refers to data validation.

Both are valid questions, but I'm not sure what your true question is. You don't state whether your current code works or not. So, this is the place to start.

We'll be glad to assist once we get a little more clarification.

By they way, I think your are on the right track with this, so don't be discouraged!

:-)
Nov 26 '14 #2
Bryan
2
Thanks already for your reply! What i want to achieve is first look up the data, so i can later use the data achieved from the lookup to validate the data that was put in the form.

The code i have at the moment is the following:

Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me.Username) Or Me.Username = "" Then
  2.         MsgBox "gelieve een login in te voeren.", vbOKOnly, "Required Data"
  3.             Me.Username.SetFocus
  4.         Exit Sub
  5.     End If
  6.  
  7. 'Check to see if data is entered into Password
  8.  
  9.     If IsNull(Me.Password) Or Me.Password = "" Then
  10.         MsgBox "Gelieve een paswoord in te voeren.", vbOKOnly, "Required Data"
  11.             Me.Password.SetFocus
  12.         Exit Sub
  13.     End If
  14.  'Coded in by Bryan
  15.  
  16.      If Username.Value <> DLookup("[login]", "Klanten", "[Username]='" & Username & "'") Then
  17. MsgBox "Invalid Username. Please try again.", vbOKOnly, "Invalid Entry!"
  18.  
  19. End If
  20.     If Password.Value <> DLookup("[wachtwoord]", "Klanten", "[Username]='" & Username & "'") Then
  21. MsgBox "Invalid Password. Please try again.", vbOKOnly, "Invalid Entry!"
  22. End If
  23. End Sub
  24.  
  25.   If Username.Value <> DLookup("[login]", "Klanten", "[Username]='" & Username & "'") Then
  26. MsgBox "Invalid Username. Please try again.", vbOKOnly, "Invalid Entry!"
  27. End If
  28.  
  29.   If Password.Value <> DLookup("[wachtwoord]", "Klanten", "[Username]='" & Username & "'") Then
  30. MsgBox "Invalid Password. Please try again.", vbOKOnly, "Invalid Entry!"
  31. End If
  32.  
  33. THis code seemed to be working so far. But i seems to only work correct with the first ID or login/password combo from the table "klanten". If i for example take the login and password combo from the third row of the table and use that to try on log into the form i get the message that the used login password is incorrect while it is correct!
  34.  
  35. Furthermore i want to open a new form is the combination of both the password value and the usernam value is correct. I tried with the code underneath but it doesn't seem to work.
  36.  
  37. If Password.Value = DLookup("[wachtwoord]", "Klanten", "[Username]='" & Username & "'") And Username.Value <> DLookup("[login]", "Klanten", "[Username]='" & Username & "'") Then DoCmd.OpenForm "Mainmenu"
  38. end if 
  39. end sub
Im sorry if i'm being unclear i'm still fairly new to all these things!
Nov 26 '14 #3
jforbes
1,107 Expert 1GB
Hello Bryan,

I'm not seeing anything glaring wrong with your code. You may have to step through it in debug mode and see what values you are getting. You also might want to type the DLookups into the Immediate Window (Ctrl-G while in the VB Editor) to see what they are returning:
Expand|Select|Wrap|Line Numbers
  1. ?DLookup("[wachtwoord]", "Klanten", "[Username]='" & Username & "'") 

That being said, when you get it to work, you may want to consider going about your security slightly different. Creating a way to login to you database is usually only half the security model people tend to use. The other half is permission to Forms and Data. So when you get your Login to work, it looks like you will need to then look up the permissions in the Klanten table as needed or grab all the permission on login.

What I do is to look up the Windows User Name on Open of the Database, then set global variables for permissions based on their record in the Employees (Klanten) table.

This code gets the Windows User login and is located in a Module. The first line is not to be inside a code block, it just supposed to hang out:
Expand|Select|Wrap|Line Numbers
  1. Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  2.  
  3. Global gImpersonateUser As String
  4. Public Function GetWindowsUser() As String
  5.  On Error GoTo GotoError
  6.  
  7.     Dim st As String
  8.     Dim slCnt As Long
  9.     Dim slDL As Long
  10.     Dim slPos As Single
  11.     Dim slUserName As String
  12.     Dim txtUsername As String
  13.  
  14.     If Len(gImpersonateUser) > 0 Then
  15.         GetWindowsUser = gImpersonateUser
  16.     Else
  17.         'Get the Windows User Name
  18.         slCnt = 199
  19.         st = String(200, 0)
  20.         slDL = GetUserName(st, slCnt)
  21.         slUserName = Left(st, slCnt) & slCnt
  22.         slPos = InStr(1, slUserName, Chr(0))
  23.         If slPos > 0 Then
  24.            txtUsername = Left(slUserName, slPos - 1)
  25.            GetWindowsUser = txtUsername
  26.         Else
  27.            GetWindowsUser = ""
  28.         End If
  29.     End If
  30.  
  31. GotoExit:
  32.     Exit Function
  33. GotoError:
  34.     MsgBox Err.Description
  35.     Resume GotoExit
  36. End Function
The variable gImpersonateUser is to allow you as the developer to login as someone else, you can rip that out if you don't need it.

Now that the function to get the Currently logged in Windows User is available, I use the following on Open of the Database to lookup Permission and System Wide variables:
Expand|Select|Wrap|Line Numbers
  1. Global gQuoteDirectory As String
  2. Global gGlobalAttachmentDirectory As String
  3. Global gLongAppName As String
  4. Global gShortAppName As String
  5.  
  6. Global gAdmin As Boolean
  7. Global gEngr As Boolean
  8. Global gManagement As Boolean
  9.  
  10. Public Sub LoadPreferences()
  11. On Error GoTo GotoError
  12.  
  13.     Dim rst As DAO.Recordset
  14.     Dim sSQL As String
  15.  
  16.     ' Load System Values
  17.     sSQL = ""
  18.     sSQL = sSQL & "Select TOP 1 SystemDefaults.* FROM SystemDefaults "
  19.     Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset, dbForwardOnly + dbSeeChanges)
  20.         If rst.RecordCount > 0 Then
  21.             gQuoteDirectory = Nz(rst!QuoteDirectory, "")
  22.             gGlobalAttachmentDirectory = Nz(rst!GlobalAttachmentDirectory, "")
  23.             gLongAppName = Nz(rst!LongAppName, "")
  24.             gShortAppName = Nz(rst!ShortAppName, "")
  25.         End If
  26.     rst.Close
  27.  
  28.     ' Load Preferences/Security
  29.     sSQL = ""
  30.     sSQL = sSQL & "SELECT TOP 1 * FROM Employees "
  31.     sSQL = sSQL & "WHERE  UserID='" & GetWindowsUser() & "'"
  32.     Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenDynaset, dbForwardOnly + dbSeeChanges)
  33.         If rst.RecordCount > 0 Then
  34.             gAdmin = Nz(rst![Admin], False)
  35.             gEngr = Nz(rst![Engr], False)
  36.             gManagement = Nz(rst![Managment], False)
  37.         Else
  38.             gAdmin = False
  39.             gEngr = False
  40.             gManagement = False
  41.             msgBoxError("You do not currently have permissions to " & gLongAppName & ".  Please see your System Administrator to gain access.")
  42.         End If
  43.     rst.Close
  44. GotoExit:
  45.     Exit Function
  46. GotoError:
  47. msgBoxError ("There was an error in LoadPreferences.  The most likely cause is that you do not currently have permissions to this Database.  Please see your System Administrator to gain access.")
  48. Resume GotoExit    
  49. End Sub
  50. Public Function msgBoxError(ByVal Message As String) As Boolean
  51.     msgBoxError = False
  52.     If MsgBox(Message, vbCritical + vbOKOnly, gLongAppName) = vbOK Then
  53.         msgBoxError = True
  54.     End If
  55. End Function
After this runs, the variables gAdmin, gEngr, gManagement contain the permissions for the current user.

This is just an option for you. I also use a SQL Server back end using Trusted Connections (Windows User) so if the user doesn't have permissions in the SQL Database, they cannot even see the data.
Nov 26 '14 #4
twinnyfo
3,653 Expert Mod 2GB
Also, just looking briefly at your code, it is unclear as to whether "Password" and "Username" are declared variables or controls on your form. I have a tendency to always be completely explicit. If I am trying to refer to a text box control, I always name the control something that differentiates it from the underlying field name. For example, if I have a table with the Field Name "UserName" and I want to have a text box that hold that field's information, I will name the text box "txtUserName". Then, when I reference the control in VBA, I always know what I am referring to. In the example above, the following code could produce different results, depending on the status of the record:

Expand|Select|Wrap|Line Numbers
  1. strUserName = Me.txtUserName
  2. strUserName = Me.UserName
It might be rare that the field would not get updated immediately, but in your case, the text boxes are updated by the user and not bound to any table, so either way is immaterial, but it is a good practice to get into to refer to object explicitly, rather than letting Access try to figure out what you mean.
Nov 26 '14 #5

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

Similar topics

5
by: calaha | last post by:
Hi all, I have been working with this since last night, and can't quite figure out why it's not working. I have a simple login box form that is set to be my startup form in my Access app (upon...
0
by: shangardezi | last post by:
hi im a beginner with visual basic. I was wondering can someone tell me how i can somehow do a thing that when someone presses ok on the LOGIN FORM, the username and password is sent to my email. im...
1
by: deepg | last post by:
Hello ! I need to validate my login form which is displayed using <div> to give sliding effect and not to refresh page, hence can't use generic php submit but to validate using javascript and/or...
19
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash...
4
by: nitin005 | last post by:
Hi, I have created a window application. I have to provide security to it. I have to provide a login form with username and password. The username and password are present in the database. When i...
1
by: postmanpat | last post by:
i have to create a login form that validates the users and passwords from a text file. I have another function that can add new users and passwords by writing to a test file split by a delimiter. But...
1
Jacotheron
by: Jacotheron | last post by:
Hi all I have a problem. I need to create a script that takes user input from a form (Username, Password, Domain) and with these input go to the page that contains their website statistics. I...
1
by: geetamadhavi | last post by:
Hi All, I have developed a php applciaiton where a new window is opening on checking the whether valid user orntot how to make that in same window after checking i have die(' not valid user ' ); i...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.