access vba code to get logged on user name

I have a button on the form that calls a on click sub that calls this function
if someone has a better way of getting the information straight from the sub I would appreciate it or tell me what is wrong with this function.

thanks gary

i have the following code in module1
for the line:status = WNetGetUser(lpName, lpUserName, lpnLength)
it tells me invalid outside of procedure

Option Compare Database
   Const NoError = 0       'The Function call was successful
   Const lpnLength As Integer = 255
   Dim lpName, lpUserName As String
   
 ' Declare for call to mpr.dll.
 Declare Function WNetGetUser Lib "mpr.dll" _
      Alias "WNetGetUserA" (ByVal lpName As String, _
      ByVal lpUserName As String, lpnLength As Long) As Long


   
Public GetUserName
  ' Buffer size for the return string.
     

      ' Get return buffer space.
      Dim status As Integer

      ' For getting user information.
     

      ' Assign the buffer size constant to lpUserName.
     ' lpUserName = Space$(lpnLength + 1)

      ' Get the log-on name of the person using product.
      status = WNetGetUser(lpName, lpUserName, lpnLength)

      ' See whether error occurred.
      If status = NoError Then
         ' This line removes the null character. Strings in C are null-
         ' terminated. Strings in Visual Basic are not null-terminated.
         ' The null character must be removed from the C strings to be used
         ' cleanly in Visual Basic.
         lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
      Else

         ' An error occurred.
         MsgBox "Unable to get the name."
         End
      End If

      ' Display the name of the person logged on to the machine.
      MsgBox "The person logged on this machine is: " & lpUserName

End Function
javagairAsked:
Who is Participating?
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
If you want the actual Full Name of the person logged in, you can use this:
http://access.mvps.org/access/api/api0066.htm
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Well, this is kind of the de-facto standard:

http://access.mvps.org/access/api/api0008.htm

Put the code in a standard VBA module.

This will give you the logged in Windows User Name

mx
0
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
You've got a comment on the line that pushes the vairable out to allow enough space for the name.  Outside of that, I don't see anything wrong outright. Below is the routine I use.

Jim

Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserNameA Lib "advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long


Public Function WhoAmI(bReturnUserName As Boolean) As String

        ' Function returns either user name or computer name

        Dim strName As String * 255

10      If bReturnUserName = True Then
20        GetUserNameA strName, Len(strName)
30      Else
40        GetComputerNameA strName, Len(strName)
50      End If

60      WhoAmI = left$(strName, InStr(strName, vbNullChar) - 1)

End Function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.