MS Access - FInd Out Who Is Logged On.

CipherIS used Ask the Experts™
I am using the code in the below link.

The issue I am having is that it should be showing me the user who is logged in per the example below in field 1 but instead I am receiving Admin.

I need to obtain the User.  Any ideas?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

The user rooster only displays the users logged into using the old Access security system.

When it's not active, then every user is admin.

I never tried it, but you may try using some Win32 API programming to check who has opened the laccdb file.
You need to use Win32 API to check the current logged in user.

You can also use Environ$("Username") to get the username.

Or you can try this below code:
 ' 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

   Sub 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.
         lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)

         ' An error occurred.
         MsgBox "Unable to get the name."
      End If

      ' Display the name of the person logged on to the machine.
      MsgBox "The person logged on this machine is: " & lpUserName

   End Sub

Open in new window

Top Expert 2016
here is short code to get the user name

Function GetUserName() As String
  Dim oName As Object
  Set oName = CreateObject("WScript.Network")
  GetUserName = oName.UserName
  Set oName = Nothing
End Function
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Jim Dettman (EE MVE)President / Owner
Most Valuable Expert 2017
Most Valuable Expert 2012

You can't directly get the user name, all you can do is get the computer name.

 But there are a couple ways of seeing who's logged on remotely, but all require admin access.

 It's often better to build in your own logging and then go off that.  One way to do that is with this:

 by setting a "user lock" when they start the app, then clear it when they log out.  Then anyone that has a current "user lock" is in the DB.   Not foolproof, but probably the simplest overall.

Distinguished Expert 2017

I switched to logging this myself once ULS was deprecated.  That was actually a good thing since I now get a list of active users even when the BE is not Jet/ACE.
The user logs in using a login form.  I update his employee record with login time.  The login form hides and opens the menu.  That makes the login form also the last form to close when the database closes and so it runs an update query to record the logout time.  Then the roster query selects all employees where the login time is > the logout time.  Occasionally, there is a failure due to Access not closing properly but otherwise, it works pretty well.  I don't need history so I elected to add the two fields to the employee record rather than create a new table.  I also added a count field which increments each time the user logs in.  This helps to determine the most active users since they are the ones to talk to when you are contemplating changes.
Top Expert 2016

using a login form is already ancient history with all my applications.
 - this all started when users from a client is getting annoyed by the login process thru a login form.
now, users are silently logged-in to a table when they open the application.
when they logged out or the application was closed, it records the time for the particular user.
Distinguished Expert 2017

You must not be creating applications that work with sensitive data.  But true, the login process can be silent in applications that don't contain sensitive data.


THE laccdb file contains "admin" in it.  My configuration is an MS Access file connected to a centrally located MS Access file on a server.  I want to know who is connected to it.
Top Expert 2016

<You must not be creating applications that work with sensitive data.  > that is not correct, you can do this even with the most sensitive info.
Distinguished Expert 2017

Only if people NEVER walk away and leave their computers unattended.  If you have sensitive information, you don't want to simply use the logged in users credentials.  You want to make them use a logon screen.  My clients are very sensitive to HIPAA rules.

There is a utility which I believe can be found on UtterAccess but I can't post a link because that violates EE policy.  Search there for "who is logged in".  You can change the view to see the credentials of the logged in user.  If you are not already a member, you will have to join.  It is free.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial