Results 1 to 8 of 8
  1. #1
    AMJADJ is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jul 2018
    Posts
    34

    Get "Windows" User name on Form

    Hi,

    I am trying to insert the username that is signed on the machine into a form. I have tried a vba code but that returns the value "Admin" so I am assuming that is the user logged into the database.
    I have tried on the net but the answers are a bit advanced for me.

    Code:
    Function UserNameWindows() As String
         UserName = Environ("USERNAME")
    End Function
    Function

    But I don't know where to put this ad (possibly) assign a text box to this value.

    Any help would be appreciated.


    Thank you in advance.

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    The textbox's data source would be:
    =UserNameWindows()

  3. #3
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    your function is wrong..you didnt assign it:

    Code:
    Function UserNameWindows() As String
         UserNameWindows = Environ("USERNAME")
    End Function

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You also have to set the value of the function. You're setting a different value.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    AMJADJ is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jul 2018
    Posts
    34
    Hi All

    Thank you for taking the time out to respond, tobe honest the above code I copied from another forum but was not sure how it is used and I am assuming it requires somewhat understanding of functions etc
    Can someone explain in lamens terms if its not too much trouble i.e. do I create a module first (I know how to), if so what is the code fo rthat module, and once this is done, do I create a text box and what the code for the text box wold be.

    Apologies, I guess I am asking to be spoon fed on this occasion.. sorry!!

  6. #6
    Beanie_d83 is offline Advanced Beginner
    Windows 8 Access 2013 64bit
    Join Date
    May 2016
    Posts
    73
    I've used this code which I found a link for on this forum:

    Code:
    Option Compare Database
    Option Explicit
    
    
    ' 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 at The Access Web
    
    
    
    
    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 = ""
        End If
    End Function
    Save this as Mod_Username or something like that and then create an unbound text box on a form where you wish the username to appear. In design view in the text box, type this =fOSUserName().

    You should then see the username from that computer appear in the text box.

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    This shows 2 ways to get the username in the textbox:
    Code:
    username.zip

  8. #8
    AMJADJ is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Jul 2018
    Posts
    34
    Thank you all to those who replied and took the time out Beanie_d83 solution worked perfectly.

    Thank you once again!

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

Similar Threads

  1. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  2. Replies: 1
    Last Post: 09-03-2014, 03:27 AM
  3. Replies: 2
    Last Post: 01-03-2014, 09:35 AM
  4. Replies: 0
    Last Post: 12-06-2011, 11:01 AM
  5. Replies: 13
    Last Post: 07-27-2011, 12:38 PM

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