That's the general idea, but let me critique it a bit.
You shouldn't have fields named Date and Time in your table: those are
reserved words, and you should never use them for your own purposes. Also,
in Access, the Date/Time field is intended to hold both the date and the
time: there's no reason to have two separate fields. (If you want only the
date from a timestamp, the DateValue function will retrieve that part.
There's also a TimeValue function to retrieve only the time portion of a
timestamp)
Assuming txtRGRHName, txtUser and so on are the names of controls on your
form, you should be referring to them as Me!txtRGRHName, Me!txtUser and so
on.
There's really no reason to open a recordset for what you're doing: an
INSERT query would be better.
Try:
Dim strSQL As String
strSQL = "INSERT INTO tbl_UserLog (UserName, UserID, LoginDtm) " & _
"VALUES ("' & Me!txtRGRHName & "', '" & Me!txtUSER & "', " & _
Format(Now(), "\#mm\/dd\/yyyy hh\:nn\:ss\#") & ")"
CurrentDb.Execute strSQL, dbFailOnError
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Jeff C said:
Thank you, as you suggested
I added the following to my on open event
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
With rst
.Open "tbl_UserLog", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic, adCmdTable
.AddNew
![Name] = [txtRGRHName]
![USER] = [txtUSER]
![Date] = [txtDate]
![Time] = [txtTime]
.Update
.Close
End With
--
Jeff C
Live Well .. Be Happy In All You Do
Douglas J Steele said:
Far safer than using the ID from the Environ variable is to use the API call
shown in
http://www.mvps.org/access/api/api0008.htm at "The Access Web".
It's trivial to reset the Environ variable.
To log whenever a user opens the database, have a form that opens when the
database first opens, and put code in its Open event to write to the table.
If you have a form that's always open while the user is in the application,
you can put code in that form's Unload event to write when the user leaves
the application. However, that's not as reliable, as they could simply power
off.
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Jeff C said:
I am using the following on a simple startup form to open a specific form
based on the user who opens the database.
Dim USER As DAO.Recordset
Set USER = Me.RecordsetClone
USER.FindFirst "[rsUSER] = '" & Me.txtUSER & "'"
If USER.NoMatch Then
DoCmd.Quit
Else
DoCmd.OpenForm USER![rsForm], acNormal
End If
DoCmd.Close acForm, "startup"
This works great matching an ID from the Environ Variable. Can someone
suggest a solution to record the date and time each user opens the database
and hold the information in a table? Thank you