User Login Report for Access 2003

M

Mike

Hi folks. I am running Access 2003 for a company that has 20 employees. The
Operating System is Windows XP. I have set up User Groups and users that are
assigned to various groups. Each user has his/ her own log in name and
password.
What I am looking for is to create an entry into a table or report each
time a users logs into the database, but does not delete the entry once the
user logs off and a different users logs in.
For an example: if an entry was made into the database on 04/01/08 at
11:15A.M., which user was logged in at that time?
Thank You for your assistance.
 
T

Tom Wickerath

Hi Mike,
Each user has his/ her own log in name and password.

It sounds like you are using Access User Level Security (ULS) instead of a
"roll-your-own" method of security. Is this correct? If the answer is yes,
you should be able to interrogate the value of CurrentUser, adding this value
along with the current date & time to a logins table. If you are not using
ULS, then CurrentUser will simply return "admin".

To get this to work, either call a function from an autoexec macro or use
code behind a startup form that determines the currentuser and runs an append
query to append this data into your logins table. Here is how I do this for a
database where I use the "roll-your-own" method in leiu of ULS:

frmDisclaimer
Includes a label with the following caption:

This Data Management System is intended for the sole use of authorized users
of The Boeing Company. Unauthorized access, use or adaptation of this
database, or any of the information contained within, is strictly prohibited.
Such action may result in disciplinary action and/or possible legal action.

This disclaimer form includes two command buttons: one to allow the person
to bail out, ie. DoCmd.Quit, and the other one named cmdOK. The code behind
cmdOK is as follows:

Option Compare Database
Option Explicit

Private Sub cmdOK_Click()
On Error GoTo ProcError

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

Set db = CurrentDb()

If Len(gstrUserName) = 0 Then 'Populate global variable, if it happens
to be null.
GetWorkstationInfo
End If

strSQL = "SELECT Person_rk, NTUserID, blnActivePerson, blnDBAdministrator
" _
& "FROM Person " _
& "WHERE NTUserID = '" & gstrUserName & "' AND blnActivePerson <> 0"

Set rs = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)

If rs.RecordCount = 0 Then 'We have an invalid user
MsgBox "Please contact {Enter Name Here}" & vbCrLf & _
"to become a valid user of this database.", _
vbCritical, "Invalid User Credentials..."
cmdExitApp_Click
Else

'Store pkUserID in global variable
glngUserID = rs!Person_rk
gvarAdmin = rs!blnDBAdministrator

'Record last login
strSQL = "INSERT into tblLogins (fkPersonID, LastLogin) " _
& "VALUES (" & glngUserID & ", #" & Now() & "#);"
'Debug.Print strSQL
db.Execute strSQL, dbFailOnError + dbSeeChanges

'Store glngLoginID in global variable (used later to record
'logout time in frmSwitchboard Form_Close procedure.
glngLoginID = DMax("pkLoginID", "tblLogins")

'Open switchboard form and close this form
DoCmd.OpenForm "frmSwitchboard"
DoCmd.Close acForm, Me.Name
End If

ExitProc:
'Cleanup
If Not rs Is Nothing Then
rs.Close: Set rs = Nothing
Set db = Nothing
End If
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in cmdOK_Click event procedure..."
Resume ExitProc
Resume
End Sub


The GetWorkstationInfo function referenced above comes from here:
http://support.microsoft.com/?ID=210088

This function is included in a stand-alone module. The global variable
glngLoginID is declared in this module as well.

If you are using ULS, this could be simplified. Instead of calling the
GetWorkstationInfo function, I think you could simply have something like
this, although consider this "air code", as I have not taken the time to test
it:

Private Sub cmdOK_Click()
On Error GoTo ProcError

Dim db As DAO.Database
Dim strSQL As String

Set db = CurrentDb()

'Record last login
strSQL = "INSERT into tblLogins (UserName, LastLogin) " _
& "VALUES ('" & CurrentUser & "', #" & Now() & "#);"
'Debug.Print strSQL
db.Execute strSQL, dbFailOnError + dbSeeChanges

'Store glngLoginID in global variable (used later to record
'logout time in frmSwitchboard Form_Close procedure.
glngLoginID = DMax("pkLoginID", "tblLogins")

'Open switchboard form and close this form
DoCmd.OpenForm "frmSwitchboard"
DoCmd.Close acForm, Me.Name
End If

ExitProc:
'Cleanup
Set db = Nothing
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in cmdOK_Click event procedure..."
Resume ExitProc
Resume
End Sub


My tblLogins table currently includes the following fields:
pkLoginID Autonumber primary key
fkPersonID Number (Long Integer)
LastLogin Date/Time
LastLogout Date/Time
blnAutoLogout Yes/No

The blnAutoLogout is a field I use to track users kicked out automatically,
after 20 minutes of inactivity. In your case, since you appear to be using
Access ULS, you could rename the fkPersonID field to UserName and change the
data type to Text.

To get all this to work smoothly, you should have a split database. Please
see this page if you are unfamiliar with the concept of splitting:

http://www.access.qbuilt.com/html/gem_tips1.html#SplitDB

Pay particular attention to the last paragraph "Beware splitting a secure
database with the wizard". Each user should have a copy of the front-end (FE)
application on their local hard drive. You may find this article helpful as
well:

Implementing a Successful Multiuser Access/JET Application
http://www.accessmvp.com/TWickerath/articles/multiuser.htm


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 
M

Mike

Thank You for your fast response.
Yes, I am using Access User Level Security (ULS). I do not believe the
database is split. I was not the original creator of the database. The
original creator no longer works for the company. Even though I was not the
original creator, I have made some drastic and advanced (I think) changes to
the database. All my training has been self-taught.
I am not sure how to start with your “air codeâ€, since I am new to modules
and Visual Basics. I will try this and see where it gets me.
 
T

Tom Wickerath

Hi Mike,

As you will read in my Multiuser Applications paper, sharing an unsplit
database is one of the quickest ways to end up with database corruption. So,
my advise is to proceed with splitting the database, but doing so without
using the wizard, since you have ULS implemented. Before splitting, I would
ensure that Name Autocorrect is disabled, and set all table subdatasheets to
[None]. Of course, you can do this after splitting, but you might as well do
it now. You will definately want to maintain an open connection between the
FE (Front-End) and BE (Back-end) files. I discuss all of this in the
Multiuser Applications paper.
I am not sure how to start with your “air codeâ€, since I am new to modules
and Visual Basics. I will try this and see where it gets me.

Here are some resources to help you get started using VBA:

Always Use Option Explicit
http://www.access.qbuilt.com/html/gem_tips.html#VBEOptions

http://www.seattleaccess.org/downloads.htm
See the download for Jan/Feb 2007
DAO - Back To Basics Compilation/Demo by Tom Wickerath

Access Basics by Crystal
http://www.accessmvp.com/Strive4Peace/Index.htm
See 2. Database Objects, starting on page 24


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
http://www.access.qbuilt.com/html/expert_contributors.html
__________________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top