Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2012
    Posts
    4

    Question Unanswered: Displaying Windows Username\Name on an Access 2007 Report

    Hello all,

    I want to be able to take the name or username of the person logged into Windows to display in a textbox of a report.

    Code in the textbox: ="Report created on " & Date() & " at " & Time() & " by: " & [????]

    I have seen some VBA code around but I can not get it to work. If you post VBA code for me to use...let me know where to place it. Like as a module etc.

    Thanks all!

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    1. Paste this code in a Standard module:
    Code:
    ' Windows API declarations
    '
    Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Public Function GetNTUser() As String
    '
    ' Retrieve the ID of the currently connected user
    '
        Dim strUserName As String
        Dim lngUserNameSize As Long
        
        strUserName = String$(255, 0)
        lngUserNameSize = Len(strUserName)
        GetUserName strUserName, lngUserNameSize
        strUserName = Left$(strUserName, lngUserNameSize - 1)
        GetNTUser = strUserName
        
    End Function
    2. Set the ControlSource property of the TextBox in the Report to:
    Code:
    ="Report created on " & Date() & " at " & Time() & " by: " & GetNtUser()
    Have a nice day!

  3. #3
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    495
    Provided Answers: 24
    also , put a text box on a form with source: =Environ("Username")
    on report it would be: forms!frmRpts!txtUser

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    environ no
    API yes
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Don't use Environ, ever ! If the computer is not part of a domain where strict security policies are enforced, it can be very easily hacked.
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •