Solved

Get Logged-In User Full Name Sorted Proper

Posted on 2010-11-12
4
727 Views
Last Modified: 2012-05-10
Using the code below, from a Previous Post I can retrieve the full name of the current logged in user for my database, but I need the format to display a different way.

The full name format I need to convert FROM:  last, first@domain
The full name format I need to convert TO: First Last

Example:
fGetFullNameOfLoggedUser() -- will give me Doe, John@domain

I need:  John Doe


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

Open in new window

0
Comment
Question by:aehare70
4 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 250 total points
ID: 34123030


Private Sub Command1_Click()
dim usr as string
usr=fGetFullNameOfLoggedUser()
usr=left(usr,instr(usr,"@")-1)
MsgBox split(usr,",")(1) & " " & split(usr,",")(0)
End Sub
0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 250 total points
ID: 34123046
a couple of lines perhaps

say

dim s as string

s = fGetFullNameOfLoggedUser()
s = Split(s, "@")(0)
s = Mid$(s, InStr(1, s, " ") + 1) & " " & left(s, InStr(1, s, ",") - 1)

msgbox "reformatted name is " & s


this assumes there is a comma
0
 
LVL 1

Author Closing Comment

by:aehare70
ID: 34123324
Thanks guys!
0
 

Expert Comment

by:Marilync1266
ID: 37503649
Fantastic! thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now