Results 1 to 2 of 2
  1. #1
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402

    show display name form active directory


    Hi Guys

    been trying to get my head around how to show in a text box the Full name of a loged in user to our domain, I can pull in the username using this code

    This function returns the full name of the currently logged-in user

    ' 2004-01-05, YM: Using Application.CurrentUser for identification of
    ' current user is very problematic (more specifically, extremely
    ' cumbersome to set up and administer for all users).
    ' Therefore, as a quick fix, let's use the OS-level user's
    ' identity instead (NB: the environment variables used below must work fine
    ' on Windows NT/2000/2003 but may not work on Windows 98/ME)
    ' CurrentWorkbenchUser = Application.CurrentUser
    '
    ' 2005-06-13, YM: Environment variables do not work in Windows 2003.
    ' Use Windows Scripting Host (WSH) Networking object instead.
    ' CurrentWorkbenchUser = Environ("UserDomain") & "\" & Environ("UserName")
    '
    ' 2007-01-23, YM: Somewhere between 2007-01-09 and 2007-01-20,
    ' the WshNetwork object stopped working on CONTROLLER3.
    ' We could not find any easy way to fix that.
    ' At the same time, it turns out that environment variables
    ' do work on Windows 2003.
    ' (Apparently, it was some weird configuration problem back in 2005:
    ' we had only one Windows 2003 computer at that time and it was
    ' Will's workstation).
    '
    ' In any case, at the time of this writing,
    ' returning to environment variables
    ' appears to be the simplest solution to the problem on CONTROLLER3.
    ' Dim wshn As New WshNetwork
    ' CurrentWorkbenchUser = wshn.UserDomain & "\" & wshn.UserName

    CurrentWorkbenchUser = Environ("your-domain-here") & "\" & Environ("username")

    but I can't get the display name to show

    example
    its showing Jsmith
    what I would like is John Smith

    I am using access 2007 and logged into a windows 2008 server active directory.

    any help would be fantastic

  2. #2
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Found this code on the web

    Works great

    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

    Private Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

    Private strUserID As String

    Private strUserName As String

    Private strComputerName As String

    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&

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

    Private Sub Class_Initialize()
    On Error Resume Next
    'Returns the network login name
    Dim strTempUserID As String, strTempComputerName As String

    'Create a buffer
    strTempUserID = String(100, Chr$(0))
    strTempComputerName = String(100, Chr$(0))

    'Get user name
    GetUserName strTempUserID, 100

    'Get computer name
    GetComputerName strTempComputerName, 100

    'Strip the rest of the buffer
    strTempUserID = Left$(strTempUserID, InStr(strTempUserID, Chr$(0)) - 1)
    Let strUserID = LCase(strTempUserID)

    strTempComputerName = Left$(strTempComputerName, InStr(strTempComputerName, Chr$(0)) - 1)
    Let strComputerName = LCase(strTempComputerName)

    Let strUserName = DragUserName(strUserID)

    End Sub

    Public Property Get UserID() As String
    UserID = strUserID
    End Property

    Public Property Get UserName() As String
    UserName = strUserName
    End Property

    Public Function DragUserName(Optional strUserName As String) As String
    On Error GoTo ErrHandler
    Dim pBuf As Long
    Dim dwRec As Long
    Dim pTmp As USER_INFO_2
    Dim abytPDCName() As Byte
    Dim abytUserName() As Byte
    Dim lngRet As Long
    Dim i As Long

    ' Unicode
    abytPDCName = fGetDCName() & vbNullChar
    If strUserName = "" Then strUserName = fGetUserName()
    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))
    DragUserName = fStrFromPtrW(pTmp.usri2_full_name)
    End If

    Call apiNetAPIBufferFree(pBuf)
    ExitHere:
    Exit Function
    ErrHandler:
    DragUserName = vbNullString
    Resume ExitHere
    End Function

    Public Property Get ComputerName() As String
    ComputerName = strComputerName
    End Property

    Private Sub Class_Terminate()
    strUserName = ""
    strComputerName = ""
    End Sub

    Public Function fGetDCName() As String
    Dim pTmp As Long
    Dim lngRet As Long
    Dim abytBuf() As Byte

    lngRet = apiNetGetDCName(0, 0, pTmp)
    If lngRet = NERR_SUCCESS Then
    fGetDCName = fStrFromPtrW(pTmp)
    End If
    Call apiNetAPIBufferFree(pTmp)
    End Function

    Public Function fStrFromPtrW(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
    fStrFromPtrW = abytBuf
    End If
    End Function

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

Similar Threads

  1. Replies: 9
    Last Post: 04-08-2014, 10:39 AM
  2. Replies: 1
    Last Post: 01-11-2014, 12:39 PM
  3. MS Active Directory
    By pkelly in forum Access
    Replies: 9
    Last Post: 10-21-2011, 08:26 AM
  4. Active Directory coding
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 08-28-2010, 08:33 PM
  5. Use form List Box to query Active Directory
    By grafiksinc in forum Forms
    Replies: 4
    Last Post: 12-02-2009, 11:56 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