Tracking Log In and Log Out times of Users

G

Guest

Thanks for all of your time. I think I figure it out my reference did not
have Microsoft DAO 3.6 Object Library checked. Thanks again

Dan said:
I changed my code as seen below:

Option Compare Database
Public Function logger(strObject As String)
Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset("AuditLog")
With rst
.AddNew
![UserId] = GetCurrentUserName
![TimeStamp] = Now()
![ObjectAccessed] = strObject
![Computer] = GetComputerName
.Update
End With
Set rst = Nothing
End Function

Now when I open my form I get a Complie error: User-definded type not
defined with Public Function logger (strObject as String) hightlited in
yellow and rst as DAO.Recordset hightlighted in blue
Douglas J. Steele said:
The fact that the database is on the server should be irrelevant. Regardless
of the location of the database, you're running Access on your workstation.

Your code is using DAO: the statement "Set rst =
CurrentDb.OpenRecordset("tblAuditLog")" proves that.

That means your declaration needs to be:

Dim rst As DAO.Recordset

If that statement generates an error, then I suspect you don't have a
reference set to DAO. With any code module open, select Tools | References
from the menu bar, scroll through the list of available references until you
find the one for Microsoft DAO 3.6 Object Library, and select it.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Dan said:
I tried both Dim rst as DAO.Recordset and ADO and still came up with
errors.
Could it be that my database is on a server?

:

This one I don't understand. I see your dim as
Dim rst As Recordset.

If you have both DAO and ADO references set, this might be the problem.
You
may need to specify
Dim rst As DAO.Recordset (or ADO if that is what you are using)

:

also when I go into the form it give me an error message that says Run
Time
Error '13': Type mismatch and then highlights Set rst =
CurrentDb.OpenRecordset("AuditLog")

:

I made the change and rename my Fucnciton "Logger" and my Module
"AuditLogger". Now I'm getting an error when I run my forms that say
it
can't find my table "AuditLog". Then it goes to the debuger and
hightlights
Set rst = CurrentDb.OpenRecordset("AuditLog") in the code you gave me
for the
auditlogger module which is listed below. Even if I use tblAuditLog
as you
did, which I assume is the name you gave the table you created, I get
the
same message. I'm assuming that because I did not use tbl to
distinquish my
table in its name I do not need to use the tbl in the code or is tlb
part of
the code?

Public Function AuditLogger(strObject as String)
Dim rst As Recordset

Set rst = CurrentDb.OpenRecordset("tblAuditLog")
With rst
.AddNew
![User] = Me.UserId
![TimeStamp] = Now()
![OjbectAccessed] = strObject
.Update
End Wtih
Set rst = Nothing
End Function


:

AuditLogger is a Sub, not a module. Modules conain Subs and
Functions.
There are 3 types of modules.
A Form module is an object attached to a form object that contains
code for
that form. It loads with the form.
A Report module is the same, except it is specific to that report.
A Standard module is not associated with any specific form or
report, but is
a collection of Subs and Functions visible to any object. This is
where you
put code you want to use throughout the application.

Now, there is one important point. A module cannot have a Sub or
Function
in it that has the same name as the module. For example, you nave
a Function
name AuditLogger; therefore, you can't name the module AuditLogger.
It will
confuse Access.

:

Sorry but I'm just learning how to use modules.
On my startup form, (that opens every time the database is open,
but is
visible) I went to design and clicked on upper left box of the
design form
and click on properties for the form. I then went to the load
event and
clicked on the 3 dots and chose code builder. When it the code
builder comes
up I have "form" in the left box and "Load" in the right dropdown
box with
the following in the lower pane

Private Sub Form_Load()

End Sub

I then put AuditLogger("Logged In") as follows

Private Sub Form_Load()
AuditLogger("Logged In")
End Sub

I also did the same for my Closed Event except I put
AuditLogger('Logged Out")

Then on the form that everyone accesses for this database I did
the same
thing. I open it in designe view clicked on the box in the
upper left
corner and then went to properties for the form then to Load
event and did
the same as above but used

AuditLogger(me.Name)

However I keep getting an error message that says that
AuditLogger is not a
module. I'm I doing this right. I have a icon in my Modules for
AuditLogger
with the code you had given me before. Thanks for the help, I'm
just
learning so I'm sorry for the repeted questions.

:

You don't need to use Call. Call is for Subs, this is a
function. Subs and
Functions are very similar, but there are some differences.

To repeat the instructions I posted earlier:
With your form or report open in design view, open the
properties dialog and
select the Events tab. Click in the text box for the Load
event. Click the
command button with the 3 dots when it appears. select code
builder.

Once you have selected Code Builder, the VBA editor will show
with the sub
declaration already done. This is where you put the call to
the function,
which is:

AuditLogger(Me.Name)

:

I sorry this is what I dont understand how to put the call to
the function in
the event
procedure. I have never worked with modules before and this
is all new to
me. I'm assuming that this a command line that goes like
"Call AuditLogger"
then "end sub"


:

We have too many nested answers and it is getting hard to
follow. I apologize
if I missed something.

If you copied the code I posted into a function, then you
don't need to
worry abount an append query. The code I sent uses the
recordset way of
doing it.

Since you are trying to learn to use VBA, we will use that
method, although
it is not the only way to do it. With your form or report
open in design
view, open the properties dialog and select the Events tab.
Click in the text
box for the Load event. Click the command button with the
3 dots when it
appears. select code builder. Now put the call to the
function in the event
procedure.

:

see below

:

See answers below, I hope this is useful.

:



:

It will take a little work, but you can do it.
First, you will need to be
able to capture the userid of the logged on user.
Here is a link to an API
routine that does that:
http://www.mvps.org/access/api/api0008.htm

I have copy this code and placed it in my modules

You're app will need a form that is opened when
assess opens. You identify
that form from the menu bar Tools, Startup and put
the name of the form in
Display Form/Page:

I have a start-up form that opens when the database
opens.

This form should always be open. You can make it
invisible, but it is the
easiest way to handle closing the application
without having to put code in
every form's close event.

My startup form is always open and is the only way to
navigate within the
database

You will need a function in a standard module that
can be accessed from any
form or report that will write an entry to an audit
log table.

I have a audit log tabel, however what fields should
I put in this table.
What would this function look like. I'm new to
module and I'm trying to
learn how to write code and how this works.

What fields you put in the table are up to you. You
probably want UserId,
TimeStamp, and Object Accessed. (that would be the form
or report opened, or
Logged In or Logged Out).

I set up my AuditLog with 4 fields, autoNumber, UserId,
TimeStamp and Object
Accessed


As to the code, it could be done a couple of ways.
One would be an append query that would add a row to
the audit log

c>
and the
other would be using a recordset.
 

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