Results 1 to 6 of 6
  1. #1
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    115

    Include username and name from Windows into textbox

    Hi everyone,



    I am making a form as report and also want to include the creator of the report based on the "windows user name and also the name" by default in the Windows system

    The username is not an issue but I don't know how to add the "name by default" from the computer registration. Does somebody know the option?


    Dim wshNetwork As Object 'New wshNetwork
    Set wshNetwork = CreateObject("WScript.Network")
    Me.Issuerusername = wshNetwork.username
    'Me.IssuerName = wshNetwork. ???
    Set wshNetwork = Nothing

    'username: charlycsh
    'Name: "Charles M. Burns"

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    49,219
    I don't think name is in computer environment info. Might require VBA pulling data from Active Directory https://docs.microsoft.com/en-us/win...g-visual-basic

    WScript.Network is new to me. If you select library Windows Script Host Object Model then Dim wshNetwork As wshNetwork you will get intellisense help. Not seeing name property.

    I use Environ() function. Environ("USERNAME"). There is no parameter for "NAME". There are 48 parameters.
    Code:
    Sub test()
    Dim x As Integer
    For x = 1 To 48
        Debug.Print Environ(x)
    Next
    End Sub
    

    Otherwise, build a Users table and do a lookup on that table to pull name based on login USERNAME. My db has code that when db opens immediately pulls USERNAME and checks Users table for record to pull name and if no record, creates one. However, user is prompted for input of name (actually, in my case, initials) for new record.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    115
    I think it could be also a good option to do with a table an also use the Dlooklookup!

    Thanks!!

  4. #4
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    1,383
    Maybe fGetFullNameOfLoggedUser in this module would work for you?

    Me.Issuerusername = fOSUserName
    'Me.IssuerName = fGetFullNameOfLoggedUser

    Save the following code in a standard module, compile and save.
    Code:
    Option Compare Database
    Option Explicit
    
    
    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
    
    
    #If VBA7 And Win64 Then
        'x64 Declarations
        Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
            "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
        
         
        Private Declare PtrSafe 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 PtrSafe Function apiNetAPIBufferFree _
            Lib "netapi32.dll" Alias "NetApiBufferFree" _
            (ByVal buffer As Long) _
            As Long
         
        ' Retrieves the length of the specified wide string.
        Private Declare PtrSafe Function apilstrlenW _
            Lib "kernel32" Alias "lstrlenW" _
            (ByVal lpString As Long) _
            As Long
         
        Private Declare PtrSafe 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 PtrSafe Sub sapiCopyMem _
            Lib "kernel32" Alias "RtlMoveMemory" _
            (Destination As Any, _
            Source As Any, _
            ByVal Length As Long)
     
    #Else
        'x32 Declaration
        Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
        "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    
     
        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)
        
    #End If
    
    
    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(Optional ByVal strUserName As String = "") 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
    
    
    '******************** Code Start **************************
    ' This code was originally written by Dev Ashish.
    ' It is not to be altered or distributed,
    ' except as part of an application.
    ' You are free to use it in any application,
    ' provided the copyright notice is left unchanged.
    '
    ' Code Courtesy of
    ' Dev Ashish
    '
    
    
    Function fOSUserName() As String
    ' Returns the network login name
    Dim lngLen As Long, lngX As Long
    Dim strUserName As String
        strUserName = String$(254, 0)
        lngLen = 255
        lngX = apiGetUserName(strUserName, lngLen)
        If (lngX > 0) Then
            fOSUserName = Left$(strUserName, lngLen - 1)
        Else
            fOSUserName = vbNullString
        End If
    End Function
    '********************
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    charly.csh is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Nov 2014
    Posts
    115
    Woooow this is exactly What I needed!!!
    Thank you Vlad!!!

  6. #6
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    1,383
    You're very welcome, good luck and stay safe!
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Detect windows username in a form
    By rayted in forum Forms
    Replies: 13
    Last Post: 03-09-2018, 01:55 PM
  2. Replies: 10
    Last Post: 09-23-2015, 11:26 AM
  3. Replies: 14
    Last Post: 08-17-2015, 02:32 AM
  4. Windows Username Authentication
    By james28 in forum Security
    Replies: 2
    Last Post: 04-30-2014, 02:55 PM
  5. Windows Log In "Username" in an append query
    By jlgray0127 in forum Queries
    Replies: 1
    Last Post: 02-26-2013, 09:46 AM

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 - Senior Forums