locked
Getting First and Last Name of logged in user in Access 2007 RRS feed

  • Question

  • Hello

     

    I need to find a way to get the "First name" and "Last Name" field data of a logged in user from Active directory to Access 2007.

    I found the way to get a Username of the user, but thats it. Can anyone help me?

     

    Thank you.

    Friday, December 24, 2010 11:31 AM

Answers

  • Hi,

    try this piece of code:

    Public Function ADtest() As String
    Dim ADSI As Object, UN As Object
      Set ADSI = CreateObject("ADSystemInfo")
      Set UN = GetObject("LDAP://" & ADSI.UserName)
      ADtest = UN.FirstName
      ADtest = ADtest & " " & UN.LastName
      Set UN = Nothing
      Set ADSI = Nothing
    End Function
    

    In my case this function returns exactly what you need:

    ? adtest
    Andrey ARTEMYEV

    Andrey V Artemyev | Saint-Petersburg, Russia
    • Marked as answer by Kukish Friday, December 24, 2010 1:18 PM
    Friday, December 24, 2010 1:13 PM

All replies

  • hi Kukish,

    Take a look at gvee's post:

    http://www.dbforums.com/microsoft-access/1618143-retrieving-active-directory-contents-vba.html


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Friday, December 24, 2010 11:38 AM
  • It would depend on how you user account sare set up on your PC.  There are environment variables that you can read from Access like this

    UName = environ("Username")

     

    This is how you probably got the username.  But there are more variables that you can use.  To seee the entire list do the following

    1. From Start Button select "RUN"
    2. In the Open box type CMD.EXE (or browse and find CMD.EXE in the c:\windows\system32 folder)
    3. A DOS window will open.  Type on the command line SET <ret>
    4. A complete list of all the environment varaibles will be displayed.

    jdweng
    Friday, December 24, 2010 11:52 AM
  • hi Joel,

    reread the op. He already has the Windows account name, which is the same as returned by Environ("Username") in a clean system. He wants the users first and last name from the Active Directory.


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    Friday, December 24, 2010 11:56 AM
  • Hello Joel

     

    This won't help, because the only info stored in the local PCs registry are: username and initials. All the other info like First Name, Phone number etc are stored in Domain Active Directory thus this info doesn't have an environment variable

    Friday, December 24, 2010 12:03 PM
  • Hi,

    try this piece of code:

    Public Function ADtest() As String
    Dim ADSI As Object, UN As Object
      Set ADSI = CreateObject("ADSystemInfo")
      Set UN = GetObject("LDAP://" & ADSI.UserName)
      ADtest = UN.FirstName
      ADtest = ADtest & " " & UN.LastName
      Set UN = Nothing
      Set ADSI = Nothing
    End Function
    

    In my case this function returns exactly what you need:

    ? adtest
    Andrey ARTEMYEV

    Andrey V Artemyev | Saint-Petersburg, Russia
    • Marked as answer by Kukish Friday, December 24, 2010 1:18 PM
    Friday, December 24, 2010 1:13 PM
  • Thank you

    То, что надо :)

    Friday, December 24, 2010 1:18 PM
  • Just be aware that this only works if the user name was included when creating the account in Active Directory. I've known administrators to be lazy, especially when it's a second or third account for the user.
    Doug Steele, Microsoft Access MVP
    http://www.AccessMVP.com/djsteele (no e-mails, please!)
    Friday, December 24, 2010 1:18 PM
  • Don't worry, our Admin is a good hardworking guy, either way I have privileges to manage Active Directory :)
    Friday, December 24, 2010 3:00 PM
  • This works fine on our old domain but we recently switched to a Workgroup and use Azure Active Directory and this code now errors out at this line:

    Set UN = GetObject("LDAP://" & ADSI.UserName)

    With a:

    Run-time error '-2147023564 (80070534)':

    Automation error

    No mapping between account names and security IDs was done. ' error ends here.

    Our Active Directory is on a Windows Server 2019 Standard and syncs every 30 minutes to Azure using the Synchronization Service Manager. Any ideas?

    Wednesday, November 11, 2020 10:27 PM