Friday, July 3, 2020

Get User's Full Name

I received an email from someone requesting assistance in Access on how to retrieve the user's full name from the system, not their username. I was told that Excel has the method Application.UserName available to do this.Not being familiar with Excel, I wasn't aware of that method.

Unfortunately, that method is not available in Access. However, I knew you can run some Excel methods from Access. For example, the following code should do the job.

Public Function GetUserXLFullName() As String
'thedbguy@gmail.com
'7/3/2020

Dim xlApp As Object

Set xlApp = CreateObject("Excel.Application")

GetUserXLFullName = xlApp.Application.UserName

Set xlApp = Nothing

End Function

But on the other hand, I also know I have used code to query the Active Directory before. As an example, here is a link to a function on my website on how to get the user's email address from Active Directory.

So, based on that example, a simple modification is all we need to get the user's Full Name from Active Directory. For instance:

Public Function GetUserADFullName() As String
'thedbguy@gmail.com
'7/3/2020

Dim objADInfo As Object
Dim objADUser As Object

Set objADInfo = CreateObject("ADSystemInfo")
Set objADUser = GetObject("LDAP://" & objADInfo.UserName)

GetUserADFullName = objADUser.FullName

Set objADUser = Nothing
Set objADInfo = Nothing

End Function

While testing these two approaches, I discovered using the Active Directory method to be faster than using Excel. Please let me know if your experience is the same. Also, please tell us if you know another method of retrieving the user's full name from the system. Thank you for reading. Cheers!

5 comments:

  1. Glad you put here the answer to my question....Thanks a lot.

    ReplyDelete
  2. I get the following error

    -2147023564: Automation error
    No mapping between account names and security IDs was done.

    on the

    Set objADUser = GetObject("LDAP://" & objADInfo.UserName)

    line. Do I have a Tools|Reference missing?

    ReplyDelete
    Replies
    1. Hi Aziz. I'm not sure. Do you also get an error if you try this other code from my website?

      http://www.accessmvp.com/thedbguy/codes.php?title=emailaddy

      Please let me know...

      Delete
  3. Hi
    Just found this article 18 months on ...
    I use this function which doesn't depend on Excel or AD
    ========================================================

    Public Function GetUserFullName() As String
    ' This function returns the full name of the currently logged-in user
    Dim objWin32NLP, objItem As Object
    On Error Resume Next
    ' Win32_NetworkLoginProfile class https://msdn.microsoft.com/en-us/library/aa394221%28v=vs.85%29.aspx
    Set objWin32NLP = GetObject("WinMgmts:").instancesof("Win32_NetworkLoginProfile")
    If Err.Number <> 0 Then
    FormattedMsgBox "Unable to retrieve current user name" & _
    "@WMI is not installed @", vbExclamation, "Windows Management Instrumentation"
    Exit Function
    End If
    For Each objItem In objWin32NLP
    If objItem.Flags > 0 Then GetUserFullName = objItem.FullName
    Next
    End Function

    ReplyDelete