How to get current username of system

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

Guest

Hi,
How to get the current login username to system and not the username of
database in Access2003. I do not want to use Application.CurrentUser because
it gives database login username which may be diff from system username.

cheers,
apls
 
Try this from Dev Ashish. Works great

Chris

API: Get Login name
Author(s)
Dev Ashish

(Q) How do I retrieve the UserName with which the user is
logged into the network?

(A) Paste the following code in a new module and call the
function fOSUserName.

'******************** 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
'
Private Declare Function apiGetUserName Lib "advapi32.dll"
Alias _
"GetUserNameA" (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
'******************** Code End **************************

-----Original Message-----
Hi,
How to get the current login username to system and not the username of
database in Access2003. I do not want to use
Application.CurrentUser because
 
I have been trying to get this GetUserName API to work for the past few
days; however, it is not working for me--must not be doing something right.

I got the module saved okay. Just to make sure, I can call the function
within an event procedure, as follows:

Dim vUser As String
vUser = fOSUserName()

Otherwise, do I have to add the reference to the advapi32.dll to Microsoft
Access? If so, where do I find it?

Thanks,

Todd
 
Although less robust and prone to some security risk, you can use the
following:

=Environ$("UserName")

Todd
 
Accoding to Dev Ashish:
"Please note that this function is only available under
Windows NT/Windows 2000."

Chris
 
Hi Todd,

I have the same problem with using advapi32.dll; not found.
I tried your substitute Environ$ and while it worked on a machine that also
worked with the fosusername and the dll, Environ$ does not work on the
machine that is having trouble with the dll.

All our machines are Windows 2000 pro and the network os is Windows 2000
Server.

Any ideas on this?

MichaelM
 
Back
Top