Phredd
Registered User.
- Local time
- Today, 16:59
- Joined
- May 28, 2002
- Messages
- 34
Hi Guys,
I am trying to determine which user currently has a database open in my company to allow me to ask them to close it for maintenance.
I am a little (Lot) out of my depth with this level of coding, but have stolen this code from another site on recommendation.
While I have no errors, I have no idea where/wehat/how to get the resulting user information. The macro appears to run great (No errors) but nothing obvious tells me the user either..
Thx
------------------------------------------------
Option Compare Database
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Dim t As DAO.TableDef, texists as Boolean
texists = False
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
End Sub
Private Sub create_table()
Dim t As DAO.TableDef, db As DAO.Database, f1 As DAO.Field, f2 As DAO.Field
Set db = CurrentDb: Set t = New DAO.TableDef: Set f1 = New DAO.Field: Set f2 = New DAO.Field
t.Name = "users_logged_in"
f1.Name = "USER"
f1.Type = dbText
f2.Name = "TIME_IN"
f2.Type = dbDate
t.Fields.Append f1
t.Fields.Append f2
db.TableDefs.Append t
Set f1 = Nothing: Set f2 = 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 "DELETE * FROM users_logged_in WHERE USER = '" & fOSUserName & "'"
End Sub
Private Sub Form_Timer()
Me.Visible = False
Me.TimerInterval = 0
End Sub
I am trying to determine which user currently has a database open in my company to allow me to ask them to close it for maintenance.
I am a little (Lot) out of my depth with this level of coding, but have stolen this code from another site on recommendation.
While I have no errors, I have no idea where/wehat/how to get the resulting user information. The macro appears to run great (No errors) but nothing obvious tells me the user either..
Thx
------------------------------------------------
Option Compare Database
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Form_Load()
Dim t As DAO.TableDef, texists as Boolean
texists = False
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
End Sub
Private Sub create_table()
Dim t As DAO.TableDef, db As DAO.Database, f1 As DAO.Field, f2 As DAO.Field
Set db = CurrentDb: Set t = New DAO.TableDef: Set f1 = New DAO.Field: Set f2 = New DAO.Field
t.Name = "users_logged_in"
f1.Name = "USER"
f1.Type = dbText
f2.Name = "TIME_IN"
f2.Type = dbDate
t.Fields.Append f1
t.Fields.Append f2
db.TableDefs.Append t
Set f1 = Nothing: Set f2 = 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 "DELETE * FROM users_logged_in WHERE USER = '" & fOSUserName & "'"
End Sub
Private Sub Form_Timer()
Me.Visible = False
Me.TimerInterval = 0
End Sub