S
Steve Albert
I am using vb code to track user names and log in times with the following
code found in this forum:
Private Sub Form_Load()
Dim t As DAO.TableDef, texists As Boolean, Curr_Usr As String
texists = False
Curr_Usr = fOSUserName
For Each t In CurrentDb.TableDefs
If t.Name = "users_logged_in" Then
texists = True
Exit For
End If
Next t
Set t = Nothing
If Not texists Then
create_table
End If
CurrentDb.Execute "INSERT INTO users_logged_in ( [USER], TIME_IN )
VALUES('" & fOSUserName & "', '" & Now() & "')"
Me.TimerInterval = 100
DoCmd.OpenForm "Logon"
End Sub
Private Sub create_table()
Dim t As DAO.TableDef, db As DAO.Database, f1 As DAO.Field, f2 As
DAO.Field, f3 As DAO.Field
Set db = CurrentDb: Set t = New DAO.TableDef: Set f1 = New DAO.Field:
Set f2 = New DAO.Field: Set f3 = New DAO.Field
t.Name = "users_logged_in"
f1.Name = "USER"
f1.Type = dbText
f2.Name = "TIME_IN"
f2.Type = dbDate
f3.Name = "TIME_OUT"
f3.Type = dbDate
t.Fields.Append f1
t.Fields.Append f2
t.Fields.Append f3
db.TableDefs.Append t
Set f1 = Nothing: Set f2 = Nothing: Set f3 = Nothing: Set t = Nothing:
Set db = Nothing
'Application.SetHiddenAttribute acTable, "users_logged_in", True
Application.RefreshDatabaseWindow
End Sub
Private Function fOSUserName() As String 'Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
Private Sub Form_Close()
CurrentDb.Execute "INSERT INTO users_logged_in ( [USER], TIME_OUT )
VALUES('" & fOSUserName & "', '" & Now() & "')"
End Sub
Private Sub Form_Timer()
Me.Visible = False
Me.TimerInterval = 0
End Sub
_________________
The problem is that the INSERT INTO Statement creates another row in the
table. I want it to find the last instance of "fOSUserName" in the table, and
put the "TIME_OUT" date/time in the SAME row with the user name and "TIME_IN"
date/time that's already there.
Thanks.
- Steve
code found in this forum:
Private Sub Form_Load()
Dim t As DAO.TableDef, texists As Boolean, Curr_Usr As String
texists = False
Curr_Usr = fOSUserName
For Each t In CurrentDb.TableDefs
If t.Name = "users_logged_in" Then
texists = True
Exit For
End If
Next t
Set t = Nothing
If Not texists Then
create_table
End If
CurrentDb.Execute "INSERT INTO users_logged_in ( [USER], TIME_IN )
VALUES('" & fOSUserName & "', '" & Now() & "')"
Me.TimerInterval = 100
DoCmd.OpenForm "Logon"
End Sub
Private Sub create_table()
Dim t As DAO.TableDef, db As DAO.Database, f1 As DAO.Field, f2 As
DAO.Field, f3 As DAO.Field
Set db = CurrentDb: Set t = New DAO.TableDef: Set f1 = New DAO.Field:
Set f2 = New DAO.Field: Set f3 = New DAO.Field
t.Name = "users_logged_in"
f1.Name = "USER"
f1.Type = dbText
f2.Name = "TIME_IN"
f2.Type = dbDate
f3.Name = "TIME_OUT"
f3.Type = dbDate
t.Fields.Append f1
t.Fields.Append f2
t.Fields.Append f3
db.TableDefs.Append t
Set f1 = Nothing: Set f2 = Nothing: Set f3 = Nothing: Set t = Nothing:
Set db = Nothing
'Application.SetHiddenAttribute acTable, "users_logged_in", True
Application.RefreshDatabaseWindow
End Sub
Private Function fOSUserName() As String 'Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
Private Sub Form_Close()
CurrentDb.Execute "INSERT INTO users_logged_in ( [USER], TIME_OUT )
VALUES('" & fOSUserName & "', '" & Now() & "')"
End Sub
Private Sub Form_Timer()
Me.Visible = False
Me.TimerInterval = 0
End Sub
_________________
The problem is that the INSERT INTO Statement creates another row in the
table. I want it to find the last instance of "fOSUserName" in the table, and
put the "TIME_OUT" date/time in the SAME row with the user name and "TIME_IN"
date/time that's already there.
Thanks.
- Steve