Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting Full User Name

Posted on 2004-06-20
10
Medium Priority
?
154 Views
Last Modified: 2012-06-08
Hi

I have been trying to figure this one out for a while.

I would like to get the full user name to put in a welcome message when my database opens.  I have found some code that gets the windows logon user name i.e. JSmith but I would like to be able to get the full name i.e. John Smith.  Am at a lost as to how to achieve this.

Any help would be appreciated.
0
Comment
Question by:ajaeclarke
  • 4
  • 4
  • 2
10 Comments
 
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 400 total points
ID: 11357121
Hi ajaeclarke,

Unless you specifically read the Registry to extract the user name entered when installing the product (or the MS-Office suit), I suspect that a name such as "John Smith" would be available to you.

If you have MS-Outlook installed as well, you may be able to extract the "sender" user name, that may, or may not be what you are looking for.

Alternatively, you may like to create a "Users" table in MS-Access, keyed on the windows logon Id you have already extracted ("JSmith"), and have a column that contains the "Full Name" (e.g. "John Smith").

That said, whenever anybody joins your organisation and requires use of the database you will have to remember to add them to this "Users" table.

BFN,

fp.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 total points
ID: 11357123
Just put this in a new module and try it out (it worked for me).
Run the last function (command1_click)

Private Type USER_INFO_2
    usri2_name As Long
    usri2_password  As Long  ' Null, only settable
    usri2_password_age  As Long
    usri2_priv  As Long
    usri2_home_dir  As Long
    usri2_comment  As Long
    usri2_flags  As Long
    usri2_script_path  As Long
    usri2_auth_flags  As Long
    usri2_full_name As Long
    usri2_usr_comment  As Long
    usri2_parms  As Long
    usri2_workstations  As Long
    usri2_last_logon  As Long
    usri2_last_logoff  As Long
    usri2_acct_expires  As Long
    usri2_max_storage  As Long
    usri2_units_per_week  As Long
    usri2_logon_hours  As Long
    usri2_bad_pw_count  As Long
    usri2_num_logons  As Long
    usri2_logon_server  As Long
    usri2_country_code  As Long
    usri2_code_page  As Long
End Type
 
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 wide 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 fGetFullNameOfLoggedUser() As String
'
' Returns the full name for a given UserID
'   NT/2000 only
' Omitting the strUserName argument will try and
' retrieve the full name for the currently logged on user
'
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 (Len(strUserName) = 0) 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))
        fGetFullNameOfLoggedUser = fStrFromPtrW(pTmp.usri2_full_name)
    End If
 
    Call apiNetAPIBufferFree(pBuf)
ExitHere:
    Exit Function
ErrHandler:
    fGetFullNameOfLoggedUser = vbNullString
    Resume ExitHere
End Function
 
Private 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
 
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
 
Private 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


Private Sub Command1_Click()
MsgBox fGetFullNameOfLoggedUser()
End Sub
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 11357197
:)

However, shouldn't the definition be:

Function strFull_User_Name(Optional ByVal strUserName As String = "") As String

In case the "strUserName" is to be used?

BFN,

fp.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 53

Expert Comment

by:Dhaest
ID: 11357207
That's correct. If forgotten to set it back because I tested it from that function (without parameter). My mistake.
Thx fanpages to correct it.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 11357244
No problem :)


As an aside, do you know if we can change the API calls to support all versions of (32-bit) Windows?

BFN,

fp.
0
 
LVL 1

Author Comment

by:ajaeclarke
ID: 11357288
Thanks for your prompt replies!!

Dhaest I tried your code and it works fine, so what are the changes that fp has pointed out and do I have to change anything as it is working.

Cheers
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 11357313
To be completely correct, you have to change
Function fGetFullNameOfLoggedUser() As String
to
Function fGetFullNameOfLoggedUser(Optional ByVal strUserName As String = "") As String

In that case you can use the function on 2 ways:
1) fGetFullNameOfLoggedUser
2) fGetFullNameOfLoggedUser "MyUserName"
0
 
LVL 1

Author Comment

by:ajaeclarke
ID: 11357407
Thanks again.

One more quick one....if I wanted to get just the first name....how is this done?
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 11357426
Hi,

Are your "full names" defined as "Surname, Firstname" or "Firstname Surname"?

If "Surname, Firstname", then you will need to use the Instr() function to find the position of the comma, then use Mid$() accordingly, this:

strFirstName = Mid$(fGetFullNameOfLoggedUser, Instr(fGetFullNameOfLoggedUser, ",") + 2)

Or alternatively, if "Firstname Surname"...

strFirstName = Left$(fGetFullNameOfLoggedUser, Instr(fGetFullNameOfLoggedUser, " ") - 1)

BFN,

fp.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 11357431
You'll have to split the result then.
Simple example:

Private Sub Command1_Click()
Dim strName As String
Dim splitName() As String
strName = fGetFullNameOfLoggedUser
splitName = split(strName, " ")
MsgBox splitName(0) & " " & splitName(1)
End Sub
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question