Results 1 to 9 of 9
  1. #1
    f15e is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2016
    Posts
    71

    Get User/Profile Name that has special char "."

    Is there a way to get the current logged on user name that has a period "." in it? I some cases, a logged on user may have a user name that includes a period.
    Example, user name = john.doe
    When I use


    usr_name = Environ("username")
    I only get back the john part of the user name. How to I get the full john.doe user name?

    Thank you!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,011
    i have never seen that function give part of an answer.

    try:
    Code:
    Private Type ExtendedUserInfo
        EUI_name As Long
        EUI_password  As Long ' Null, only settable
        EUI_password_age  As Long
        EUI_priv  As Long
        EUI_home_dir  As Long
        EUI_comment  As Long
        EUI_flags  As Long
        EUI_script_path  As Long
        EUI_auth_flags  As Long
        EUI_full_name As Long
        EUI_usr_comment  As Long
        EUI_parms  As Long
        EUI_workstations  As Long
        EUI_last_logon  As Long
        EUI_last_logoff  As Long
        EUI_acct_expires  As Long
        EUI_max_storage  As Long
        EUI_units_per_week  As Long
        EUI_logon_hours  As Long
        EUI_bad_pw_count  As Long
        EUI_num_logons  As Long
        EUI_logon_server  As Long
        EUI_country_code  As Long
        EUI_code_page  As Long
    End Type
     
     'Windows API function declarations
    Private Declare Function apiNetGetDCName Lib "netapi32.dll" _
    Alias "NetGetDCName" (ByVal servername As Long, _
    ByVal DomainName As Long, _
    bufptr As Long) As Long
     
     ' function frees the memory that the NetApiBufferAllocate function allocates.
    Private Declare Function apiNetAPIBufferFree Lib "netapi32.dll" _
    Alias "NetApiBufferFree" (ByVal buffer As Long) As Long
     
     ' Retrieves the length of the specified Unicode string.
    Private Declare Function apilstrlenW Lib "kernel32" _
    Alias "lstrlenW" (ByVal lpString As Long) As Long
     
    Private Declare Function apiNetUserGetInfo Lib "netapi32.dll" _
    Alias "NetUserGetInfo" (servername As Any, _
    UserName As Any, _
    ByVal level As Long, _
    bufptr As Long) As Long
     
     ' moves memory either forward or backward, aligned or unaligned,
     ' in 4-byte blocks, followed by any remaining bytes
    Private Declare Sub sapiCopyMem Lib "kernel32" _
    Alias "RtlMoveMemory" (Destination As Any, _
    Source As Any, _
    ByVal Length As Long)
     
    Private Declare Function apiGetUserName Lib "advapi32.dll" _
    Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
     
    Private Const MAXCOMMENTSZ = 256
    Private Const NERR_SUCCESS = 0
    Private Const ERROR_MORE_DATA = 234&
    Private Const MAX_CHUNK = 25
    Private Const ERROR_SUCCESS = 0&
     
    Function GetFullNameOfLoggedUser(Optional strUserName As String) As String
         '
         ' Returns the full name for a given network username (NT/2000/XP only)
         ' Omitting the argument will retrieve the full name for the currently logged on user
         '
        On Error GoTo Err_GetFullNameOfLoggedUser
        Dim pBuf As Long
        Dim dwRec As Long
        Dim pTmp As ExtendedUserInfo
        Dim abytPDCName() As Byte
        Dim abytUserName() As Byte
        Dim lngRet As Long
        Dim i As Long
         
         ' Unicode
        abytPDCName = GetDCName() & vbNullChar
        If (Len(strUserName) = 0) Then
            strUserName = GetUserName()
        End If
        abytUserName = strUserName & vbNullChar
         
         ' Level 2
        lngRet = apiNetUserGetInfo(abytPDCName(0), abytUserName(0), 2, pBuf)
        If (lngRet = ERROR_SUCCESS) Then
            Call sapiCopyMem(pTmp, ByVal pBuf, Len(pTmp))
            GetFullNameOfLoggedUser = StrFromPtrW(pTmp.EUI_full_name)
        End If
         
        Call apiNetAPIBufferFree(pBuf)
         
    Exit_GetFullNameOfLoggedUser:
        Exit Function
         
    Err_GetFullNameOfLoggedUser:
        MsgBox Err.Description, vbExclamation
        GetFullNameOfLoggedUser = vbNullString
        Resume Exit_GetFullNameOfLoggedUser
    End Function
    
    
    
    
    Private Function GetUserName() As String
         ' Returns the network login name
        Dim lngLen As Long, lngRet As Long
        Dim strUserName As String
        strUserName = String$(254, 0)
        lngLen = 255
        lngRet = apiGetUserName(strUserName, lngLen)
        If lngRet Then
            GetUserName = Left$(strUserName, lngLen - 1)
        End If
    End Function
     
    Function GetDCName() As String
        Dim pTmp As Long
        Dim lngRet As Long
        Dim abytBuf() As Byte
         
        lngRet = apiNetGetDCName(0, 0, pTmp)
        If lngRet = NERR_SUCCESS Then
            GetDCName = StrFromPtrW(pTmp)
        End If
        Call apiNetAPIBufferFree(pTmp)
    End Function
     
    Private Function StrFromPtrW(pBuf As Long) As String
        Dim lngLen As Long
        Dim abytBuf() As Byte
         
         ' Get the length of the string at the memory location
        lngLen = apilstrlenW(pBuf) * 2
         ' if it's not a ZLS
        If lngLen Then
            ReDim abytBuf(lngLen)
             ' then copy the memory contents
             ' into a temp buffer
            Call sapiCopyMem(abytBuf(0), ByVal pBuf, lngLen)
             ' return the buffer
            StrFromPtrW = abytBuf
        End If
    End Function

  3. #3
    f15e is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2016
    Posts
    71
    Thanks for you reply ranman256. You are correct that it shouldn't show only half of the user name. I was under the impression the "." was cutting off the complete name but now I realize it has something to do with my computer settings. I remembered that our IT had to create a new user name/profile for me b/c of an issue I was having with my work. What they did was use my original user name (e.g. john) and added a "." with our company initials (e.g. john.abc). I am logged on as john.abc but when I use the Environ("username"), it shows up as just "john". I'm not sure why this is. Anyway, thanks for the response.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    50,423
    Can you get them to change the . to _?
    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.

  5. #5
    ohmydatabase's Avatar
    ohmydatabase is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    79
    I'm not 100% on this solution, but try looking in the Access Options. I'm currently using Access 2007, but under AccessOptions>Popular there is a section to "Personalize your copy of Microsoft Office". There is a User name that is associated with your Microsoft Office. I believe this is the value that environ grabs.

    In my application I use the netAPI stuff that ranman showed you instead of environ. (Mainly because that was the first thing I found) I suspect that the netAPI calls get your Windows:UserName, while eviron returns your MicrosoftOffice:UserName.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    50,423
    I did a test. Changed the db user name. Environ still returns Windows USERNAME. I use Environ in a multi-user db to grab Windows USERNAME to identify user and pull values from Users table.
    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.

  7. #7
    f15e is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Mar 2016
    Posts
    71
    I doubt I will be able to get our IT to change it. I will just have to use a work-around.

  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
    50,423
    Wait, I changed user name in Access, do I need to change user name somewhere else? Nope. Just checked Word and it shows the changed user name.
    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
    ohmydatabase's Avatar
    ohmydatabase is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    79
    Quote Originally Posted by June7 View Post
    Wait, I changed user name in Access, do I need to change user name somewhere else? Nope. Just checked Word and it shows the changed user name.
    So both environ and the netAPI get the Windows UserName.. Hmmm...

    I also use a Users Table (tblUsers) where I store things like FullName and WindowsUserName. Are both UserName and GetFullNameOfLoggedUser giving you the name with the "." in it? If so, you can make a tblUsers where you automatically populate the actual UserName from Windows, but also create another Field in which you store your actual, desired name. This may be a half measure but definitely solves your problem without dealing with your IT department. You may need to make form so you and other users can add NewName to tblUsers.

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

Similar Threads

  1. Replies: 27
    Last Post: 11-18-2015, 12:39 PM
  2. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  3. Replies: 5
    Last Post: 01-12-2015, 03:44 PM
  4. Replies: 2
    Last Post: 01-03-2014, 09:35 AM
  5. Link tables on "special" fields
    By Gargen in forum Access
    Replies: 0
    Last Post: 12-18-2008, 12:02 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