when user opens the db, the Login form will open and read their userID.
The user enters their userid and password.
The code authenticates it against WINDOWS login, and either lets them into the main menu or exits.
the tUsers table only has their name and loginid. No need to control passwords since Windows does that.
By having their name in the tUsers table is another level of security that they are allowed into the database.
I grab the userID when they open the login screen
vUser = Environ("Username")
Code:
'user click LOGIN button
sub btnLogin_click()
vUser = Environ("Username")
If WindowsLogin(txtUser, txtPass, txtDom) Then
if vUser = Dlookup("[UserID]","tUsers","[userID]='" & vUser & "'") then
DoCmd.OpenForm "frmMainMenu"
DoCmd.OpenForm "frmLogin"
DoCmd.Close
else
msgbox "You do not have rights for this app"
docmd.quit
endif
Else
MsgBox "LOGIN INCORRECT", vbCritical, "Bad userid or password"
'docmd.quit 'exit access
End If
end sub
Public Function WindowsLogin(ByVal strUserName As String, ByVal strpassword As String, ByVal strDomain As String) As Boolean
'Authenticates user and password entered with Active Directory.
On Error GoTo IncorrectPassword
Dim oADsObject, oADsNamespace As Object
Dim strADsPath As String
strADsPath = "WinNT://" & strDomain
Set oADsObject = GetObject(strADsPath)
Set oADsNamespace = GetObject("WinNT:")
Set oADsObject = oADsNamespace.OpenDSObject(strADsPath, strDomain & "\" & strUserName, strpassword, 0)
WindowsLogin = True 'ACCESS GRANTED
ExitSub:
Exit Function
IncorrectPassword:
WindowsLogin = False 'ACCESS DENIED
Resume ExitSub
End Function