I would suggest using J's second block of code to determine which user is logged in to the PC and it is essentially the same code I use for such things.
File names and the system environment variables can be altered.
This is the code I borrowed from Dev Ashish ages ago.
Following his lead, I needed the name assigned to the PC so I wrote another function based on Ashish's example. Notice that I use the "PtrSafe" in the declaraions to account for 64Bit installs... if for some reason this chokes try deleting it.
This has been tested several times and is in a production database I use:
- Option Compare Database
-
Option Explicit
-
-
'******************** Code Start **************************
-
' This code was originally written by Dev Ashish.
-
' It is not to be altered or distributed,
-
' except as part of an application.
-
' You are free to use it in any application,
-
' provided the copyright notice is left unchanged.
-
'
-
' Code Courtesy of
-
' Dev Ashish
-
'
-
'
-
'ZMBD: Added the PtrSafe - have some 64bit installs that need this
-
Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
-
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
-
-
'
-
'ZMBD: I want the PC name too - :)
-
'ZMBD: Added the PtrSafe - have some 64bit installs that need this
-
Private Declare PtrSafe Function apiGetComputerName Lib "kernel32" Alias _
-
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
-
-
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
-
'
-
'
-
'ZMBD: Based on the above and from the Win32API_PtrSafe.txt
-
Function fOSPCName() As String
-
' Returns the network login name
-
Dim lngLen As Long, lngX As Long
-
Dim strPCName As String
-
strPCName = String$(254, 0)
-
lngLen = 255
-
lngX = apiGetComputerName(strPCName, lngLen)
-
If (lngX > 0) Then
-
fOSPCName = Left$(strPCName, lngLen - 1)
-
Else
-
fOSPCName = vbNullString
-
End If
-
End Function
-
-
'******************** Code End ************************