Returning the Username

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to open a form with a Where clause that uses the user's system
Username, which for John Smith would be "jsmith".

?Workspaces(0).Username

from the Immediate Window returns "admin".

Can anyone provide the correct expression for the system username?

Thank you.
Sprinks
 
Here is the code I use:

Public Function User()
Dim TmpName As String * 80
95 Dim NameLen As Long
100 Dim Out As String
105 NameLen = 75
110 If csvGetUserName(TmpName, NameLen) Then
115 NameLen = IIf(NameLen < 75, NameLen, 75)
120 Out = Left$(TmpName, NameLen - 1)
125 Else
130 Out = "Not registered"
135 End If
140 User = Left$(Out, 20)
 
Hey Sprinks, here is module I found on these boards. The code works great:

Function fOSUserName() As String
' 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

' 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

This function will return the windows login username.
 
This should Help runs under windows XP 32 bit:

Public function UserName()
Declare Function csvGetUserName Lib "advapi32" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Boolean
Dim TmpName As String * 80
Dim NameLen As Long
Dim Out As String
NameLen = 75
If csvGetUserName(TmpName, NameLen) Then
NameLen = IIf(NameLen < 75, NameLen, 75)
Out = Left$(TmpName, NameLen - 1)
Else
Out = "Not registered"
End If
UserName = Left$(Out, 20)
End Function
 
You forgot to include the critical declaration of the API being used.

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Of course, that's the same code as at the reference Brendan gave a couple of
hours before you.
 
Back
Top