Display logged in users

  • Thread starter auujxa2 via AccessMonster.com
  • Start date
A

auujxa2 via AccessMonster.com

I have this code which displays in a value list, which user ID is logged into
my database. But I want to show the persons actual name (i.e. John Doe,
instead of BLD2174) I created a table with UserName and UserID as the fields.
UserID has the PC name. I want UserName to show. Here is what I have so far.
Thank you in advance

Private Function WhosOn() As String

On Error GoTo Err_WhosOn

Dim iLDBFile As Integer, iStart As Integer
Dim iLOF As Integer, i As Integer
Dim sPath As String, X As String
Dim sLogStr As String, sLogins As String
Dim sMach As String, sUser As String
Dim rUser As UserRec ' Defined in General
Dim dbCurrent As Database

' Get Path of current database. Should substitute this code
' for an attached table path in a multi-user environment.

Set dbCurrent = DBEngine.Workspaces(0).Databases(0)
sPath = dbCurrent.Name
dbCurrent.Close

' Iterate thru dbCurrent.LDB file for login names.
sPath = Left(sPath, InStr(1, sPath, ".")) + "LDB"

' Test for valid file, else Error
X = Dir(sPath)
iStart = 1
iLDBFile = FreeFile

Open sPath For Binary Access Read Shared As iLDBFile
iLOF = LOF(iLDBFile)
Do While Not EOF(iLDBFile)
Get iLDBFile, , rUser
With rUser
i = 1
sMach = ""
While .bMach(i) <> 0
sMach = sMach & Chr(.bMach(i))
i = i + 1
Wend
i = 1
sUser = ""
While .bUser(i) <> 0
sUser = sUser & Chr(.bUser(i))
i = i + 1
Wend
End With
sLogStr = sMach '& " -- " & sUser
If InStr(sLogins, sLogStr) = 0 Then
sLogins = sLogins & sLogStr & ";"
End If
iStart = iStart + 64 'increment to next record offset
Loop
Close iLDBFile
WhosOn = sLogins

Exit_WhosOn:
Exit Function

Err_WhosOn:
If Err = 68 Then
MsgBox "Couldn't populate the list", 48, "No LDB File"
Else
MsgBox "Error: " & Err.Number & vbCrLf & Err.Description
Close iLDBFile
End If
Resume Exit_WhosOn

End Function
 
W

Wolfgang Kais

Hello "auujxa2".

"auujxa2"wrote:
I have this code which displays in a value list, which user ID is
logged into my database. But I want to show the persons actual name
(i.e. John Doe, instead of BLD2174) I created a table with UserName
and UserID as the fields. UserID has the PC name. I want UserName
to show. Here is what I have so far.
Thank you in advance

[snip]...code to open the ldb file and read the characters...[snip]

I decided not to comment your code, but I think it's time for ADO.
I hope that you use at least Access 2000? If so:

Public Function WhosOn() As String

Dim iLenMachine As Integer, iLenUser As Integer
Dim sMachine As String, sUser As String

With CurrentProject.Connection
With .OpenSchema(adSchemaProviderSpecific, , _
"{947bb102-5d43-11d1-bdbf-00c04fb92675}")
Do Until .EOF
iLenMachine = InStr(.Fields(0), vbNullChar) - 1
iLenUser = InStr(.Fields(1), vbNullChar) - 1
sMachine = Left(.Fields(0), iLenMachine)
sUser = Left(.Fields(1), iLenUser)
If .Fields(2) = True Then
WhosOn = WhosOn & sMachine & " -- " & sUser & ";"
End If
.MoveNext
Loop
.Close
End With
End With

End Function

To lookup the persons actual name (UserName) from a table tblUsers
by passing the machine name, you can use a function like this:

Private Function UserLookup(Machine As String) As String

Dim sSQL As String
sSQL = "SELECT UserName FROM tblUsers Where UserID = @UserID"

With New ADODB.Command
Set .ActiveConnection = CurrentProject.Connection
.CommandText = sSQL
.CommandType = adCmdText
.Parameters.Append .CreateParameter(Name:="@UserID", _
Type:=adVarChar, Size:=15, Value:=Machine)
With .Execute
If Not .EOF Then UserLookup = Nz(.Fields(0).Value, "")
.Close
End With
End With

End Function

Use the value returned by this function when conactenating the
output string.
 

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