access vba code to get logged on user name

Posted on 2012-09-02
Last Modified: 2012-09-07
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)

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

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

End Function
Question by:javagair
    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    Well, this is kind of the de-facto standard:

    Put the code in a standard VBA module.

    This will give you the logged in Windows User Name

    LVL 75

    Accepted Solution

    If you want the actual Full Name of the person logged in, you can use this:
    LVL 56

    Expert Comment

    by:Jim Dettman (Microsoft MVP/ EE MVE)
    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.


    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Back in July, I blogged about how Microsoft's new server pricing model, combined with the end of the Small Business Server package, would result in significant cost increases for many small businesses (see SBS End of Life: Microsoft Punishes Small B…
    Experts-Exchange users below are the steps you can follow to upgrade your Lync server to latest CU's or cumulative updates. Note: Perform it during non-production hours.   Step 1: Backup your lync and SQL server database. Follow below article: h…
    In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now