Results 1 to 6 of 6
  1. #1
    Larryg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    50

    GetUserName

    Hello;
    After many unsuccessful attempts to get the MS User Name (using different code snippets), the below code works when it's applied to a button on a form. However, what I would like to do is populate an unbound text box on the form, and, have it available when writing data to a table. I have been working on trying to get this working for way too many hours, and have tried so many iterations of code, placement, and hail-mary's I have lost count. If you have any suggestions I would surely appreciate it.

    I have placed the Declare Function section in a Module, and placed the rest of the code as Private Sub, and also tried as Public Sub, (albeit I don't have a firm grasp on the functionality of the difference between Module and Class Module). I've tried the UserName = Environ("USERNAME")and couldn't get that to work either. But again, the below code returns the user name when the code is used as a Private Sub in the OnClick event of a button on the form where I want the user name to populate an unbound text box (not trying to file the user name from this form, just have it show to the user - I figure if I can get it to populate in the text box I can write it to a table in another form).


    Using Access 2010 on a Win-7/64bit machine.


    Thanks.
    _______________________________________
    ' 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

    Const NoError = 0 'The Function call was successful

    Sub GetUserName()

    ' Buffer size for the return string.
    Const lpnLength As Integer = 255

    ' Get return buffer space.
    Dim status As Integer

    ' For getting user information.
    Dim lpName, lpUserName As String

    ' 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 Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,515
    You've sort of made the function less flexible, but instead of

    MsgBox "The person logged on this machine is: " & lpUserName

    put this

    Forms!FormName.TextboxName = lpUserName

    What you'd normally do is have the function return a value:

    Public Function GetUserName() As String

    instead of the lines above:

    GetUserName = lpUserName

    And then wherever you want you can use the function:

    Forms!FormName.TextboxName = GetUserName()

    This is an example:

    http://access.mvps.org/access/api/api0008.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,515
    Oh, and this may be informative:

    http://www.baldyweb.com/SubFunction.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Larryg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    50
    Quote Originally Posted by pbaldy View Post
    You've sort of made the function less flexible, but instead of

    MsgBox "The person logged on this machine is: " & lpUserName

    put this

    Forms!FormName.TextboxName = lpUserName

    What you'd normally do is have the function return a value:

    Public Function GetUserName() As String

    instead of the lines above:

    GetUserName = lpUserName

    And then wherever you want you can use the function:

    Forms!FormName.TextboxName = GetUserName()

    This is an example:

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

    Thanks so much for the reply...

    I thought I would take the simpler route of using the code supplied in the Dev Ashish code link you supplied. So here is what I did:

    I created a Module (Named it GetUserName_3)
    Entered the both sets of code (Private Declare and Function) in that Module.
    In the OnOpen Event of the form I entered "Call GetUserName" (also tried "GetUsereName_3)
    In the Unbound text box, in the Control Source line I entered "=GetUserName()" (also tried entering "=[UserName]" in both Control Source & the Default Value field also(but not at the same time)).

    I went this route in an effort to have the [UserName] available to any form in the database, and, to be able to write the UserName to any table along with any other data I may be writing from a form.
    (The form I am attempting to Display the username on, simply displays the username, it does not write any data). I figure I would try to get the username to display first, then work on getting a form to write it to a table.

    Obviously I bungled it, but it looked like your original suggestions would have only addressed the one form. The MsgBox code was only testing to see if the code was working.

  5. #5
    Larryg is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2015
    Posts
    50
    Paul... thanks so much for the reply !

    OK... I had a reply written and thought I posted it, but I guess something happened to it. Anyway...

    I took the route of using the code in the link you provided. Here is what I did:

    I created a Module, and named it GetUserName_3.
    I placed both pieces of code in the same Module (it automatically created 2 separate functions).
    I then placed "Call GetUserName" in the OnOpen event of the form.
    In the unbound text box I entered "=[fOSUserName] in the Control Source line. When that didn't work, I tried =[UserName], then tried both in the Default Value line. None worked.

    However... then for chuckles I created a brand new text box on the form as a test, and entered =[fOSUserName] in the Control Source line, and it worked ! Go figure ! Both text boxes appear to be identical, but one works and one doesn't. Weird (to me anyway).

    So, I can work with that. I'll just swap out the text boxes and I think I'm good to go. Now I move on to trying to get the UserName to write into a table record. I guess I need to always use the variable "fOSUserName" when referring to the operators name (instead of simply UserName, right?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,515
    Your post got moderated because of the link and being a new member (even though the link was in a quote). It's an anti spam setting.

    I often put it into a hidden textbox on a form that stays open and refer to that elsewhere in the db. You could load it into a global variable. You can also just keep calling the function.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Getusername of currently logged in user
    By nkuebelbeck in forum Access
    Replies: 8
    Last Post: 06-29-2011, 04:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums