Results 1 to 15 of 15
  1. #1
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51

    Get username and not eviron(Username) in access

    Hi, how can i get username and not eviron(Username) in access...

    In excel we get different output for Application.Username and not Eviron("Username")

    Thanks in advance for helping....

  2. #2
    tommcmaster is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    2
    Hi Ped,
    When I want to get the username of the current user I use the following function in VBA.

    First I declare this in the top section of the vba code in the line before 'Option Compare Database'. Then I use this function in my code. Calling this should return the current user's username to the variable assigned to the function.


    Private Declare Function apiGetUserName Lib "advapi32.dll" Alias

    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long 'Internal file reference for getting Windows username
    'The fOSUserName function is used to get the username of the user currently logged onto the network.
    Function fOSUserName() As String
    'Initialise variables
    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


    Hope this helps.
    Tom.

  3. #3
    ped's Avatar
    ped is offline Advanced Beginner
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2011
    Posts
    51

    replying back



    Thanks for replying...that is another way like environ("Username")
    Now in access tools >> Popular >> username = Pedie Nz
    and my environ username/computer id = Pedie2009

    I'm trying to get the name...if that is posible...


    Thanks again.

  4. #4
    KrisNeuman is offline Novice
    Windows XP Access 2007
    Join Date
    May 2012
    Posts
    1
    Pedie,
    Did you ever find an answer to this? I am having the same problem.
    Thanks in advance!
    Kris

  5. #5
    bcmarshall is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jul 2010
    Posts
    95
    This works like a charm!

    Function UserName() 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
    UserName = Left$(strUserName, lngLen - 1)
    Else
    UserName = vbNullString
    End If
    End Function

  6. #6
    Snoday is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    4
    Is the computer you are using part of an active directory domain?

  7. #7
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    did anyone got this question resolved? I am also stuck on this. All I see are the network login / environ / and other username codes which shows the network username but not the access application username and initials.

    tried this code but apparently access does not have "username" but have "currentuser".

    any idea?

    Code:
    Sub myName()
    
        Dim objaccess As Object
        Dim strUserName As String
        
        Set objaccess = GetObject(, "Access.Application")
        
        strUserName = objaccess.UserName
                
        Debug.Print strUserName
    End Sub

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I typed into VBA immediate window:

    ?Application.CurrentUser

    That returned:

    Admin

    Really don't understand what is being asked for.

    Instead of hijacking ancient threads, should start your own. A new thread with no replies gets more attention. Can reference an older thread if you think it contributes info relevant to your issue.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    If you are not using Access security, which requires a user to log in to the database, the CurrentUser is always Admin.

    The Environ function can only retrieve values from current environment variables. The network login ID is (usually) in the environment variable UserName.

  10. #10
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    Thanks June. not the user type/group but the actual username - the name you type upon initial load of ms office application (option > username). but after a few searches, i think this is not possible with access, hopefully Im wrong.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    If it's saved anywhere, might be in a registry, at least that's where CurrentUser is pulled from. https://msdn.microsoft.com/en-us/lib...or=-2147217396

    This talks about MS Office RegCompany and RegOwner properties http://www.pctools.com/guides/registry/detail/460/
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    Thanks for the link June. However both deals with the "CurrentUser" or "Network Username". I tried to search the name within the application. try to run my code in excel:
    Code:
    Sub myName()
    
        Dim objaccess As Object
        Dim strUserName As String
        
        Set objaccess = GetObject(, "Excel.Application")
        
        strUserName = objaccess.UserName
                
        Debug.Print strUserName
    End Sub
    This data is somewhat not readable in access.

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Edited my earlier post (perhaps after you read it). There is another link.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    warhead92100 is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    19
    Hi June, would you know how do I access them to show in access? VBA? Im on a network and does not have access to the registry. Thansk!

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    If you can't access the registry then don't see how you can do it with VBA. But what has network got to do with looking at registries on computer?

    The links I found is the extent of my familiarity with this topic - which is essentially nil.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Username help needed...
    By dotti1966 in forum Access
    Replies: 3
    Last Post: 05-18-2011, 10:12 AM
  2. Username/Password From Table
    By mj-egerton in forum Programming
    Replies: 0
    Last Post: 04-03-2011, 04:46 AM
  3. Recording UserName Login
    By Moonsitter53 in forum Access
    Replies: 1
    Last Post: 03-18-2011, 02:40 PM
  4. Recording Username
    By Juan4412 in forum Forms
    Replies: 9
    Last Post: 02-01-2011, 11:15 PM
  5. Username For Sharepoint
    By is49460 in forum SharePoint
    Replies: 0
    Last Post: 10-08-2010, 05:40 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