Save this Module
Code:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function UserNameWindows() As String
Dim lngLen As Long
Dim strBuffer As String
Const dhcMaxUserName = 255
strBuffer = Space(dhcMaxUserName)
lngLen = dhcMaxUserName
If CBool(GetUserName(strBuffer, lngLen)) Then
UserNameWindows = Left$(strBuffer, lngLen - 1)
Else
UserNameWindows = ""
End If
End Function
'Sub test()
'MsgBox UserNameWindows
'End Sub
To call the current network pcs username use:
If you wanted to use it as a login function on a form:
Code:
Private Sub Command4_Click()
Dim rs As Recordset
Dim strSQL As String
Dim strPassword As String
Dim strUserName As String
On Error Resume Next
strUserName = Username.Value
strPassword = Password.Value
Set db = CurrentDb
strSQL = "SELECT fldPassword FROM tblUsers WHERE fldUsername='" & strUserName & "'"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
If rs.Fields(0) <> strPassword Then
MsgBox "You typed wrong password. Try again.", vbOKOnly, "Warning!"
Else
Me.Visible = False
DoCmd.OpenForm "frmHome", acNormal
End If
Else
MsgBox "You typed wrong username or password. Try again.", vbOKCancel, "Warning!"
Password.SetFocus
End If
End Sub
From here you can manipulate a populated list of current users, however, if the system is virtual.. you cannot see other users unless it has been allowed by an admin. I use this as a login method by locking the username box on the login screen so the db can only be accessed at that users PC, allowing different users different privileges with ease. I have been able to use the intermediate window in the past for this however ultimately gave up, not out of lack of information just the immense amount of trial and error I went through. You could even call the Shell to see active users in the Back End DB and echo that command to a text log, then if you really wanted to go further you can call the text in that log to a text box on a form. Again went through all of this many times and it became easier to use other means outside of the DB to view active users. Sorry if this doesn't help.