[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


access print logged on user on report

Posted on 2012-09-11
Medium Priority
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
  • 3
  • 2
LVL 61

Expert Comment

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

= Environ("username")

Author Comment

ID: 38386571
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

mbizup earned 2000 total points
ID: 38386580
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

ID: 38386585
<< 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

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

Featured Post

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.

Question has a verified solution.

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

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

873 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