access print logged on user on report

Posted on 2012-09-11
1 Endorsement
Last Modified: 2012-09-11
Does anyone know an easy way of printing the current logged on user on an Access Report?
Question by:HKFuey
    LVL 61

    Expert Comment

    Try placing this in the control source property of a textbox on your report (include the = sign):

    = Environ("username")

    Author Comment

    Hi mbizup,  I did try that in a text box like this: ="Printed: " & Now() & " by " & Environ("username")

    I get a message "Enter parameter value" and 'Environ', will this function only work by itself?
    LVL 61

    Accepted Solution

    An alternate (and better) solution is to use an API function to get the user name.  

    Set your textbox control source to this:

    = GetUserName()

    and add this code to a module:

    Option Explicit
         ' Declare for call to mpr.dll.
       Declare Function WNetGetUser Lib "mpr.dll" _
          Alias "WNetGetUserA" (ByVal lpName As String, _
          ByVal lpUserName As String, lpnLength As Long) As Long
       Const NoError = 0       'The Function call was successful
       Function GetUserName()
          ' Buffer size for the return string.
          Const lpnLength As Integer = 255
          ' Get return buffer space.
          Dim status As Integer
          ' For getting user information.
          Dim lpName, lpUserName As String
          ' Assign the buffer size constant to lpUserName.
          lpUserName = Space$(lpnLength + 1)
          ' Get the log-on name of the person using product.
          status = WNetGetUser(lpName, lpUserName, lpnLength)
          ' See whether error occurred.
          If status = NoError Then
             ' This line removes the null character. Strings in C are null-
             ' terminated. Strings in Visual Basic are not null-terminated.
             ' The null character must be removed from the C strings to be used
             ' cleanly in Visual Basic.
             GetUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
             ' An error occurred.
             GetUserName = ""
          End If
       End Function

    Open in new window

    The function is a modified version of the code here:

    Its simply modified to return a string rather than posting a message box
    LVL 61

    Expert Comment

    << I get a message "Enter parameter value" and 'Environ', will this function only work by itself?>>

    Are you certain that you included the = sign in the control source?  It looks like it should have worked.


    although, depending on your security settings, Environ might not work.   Try the API suggestion I just posted...

    Author Closing Comment

    Spot on, thanks very much! I'm sure I will get a lot of use out of this in the future!!

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now