Avatar of CipherIS
CipherIS
Flag for United States of America asked on

MS Access - FInd Out Who Is Logged On.

I am using the code in the below link.

http://www.fmsinc.com/MicrosoftAccess/monitor/user-roster.htm

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.

http://www.fmsinc.com/MicrosoftAccess/monitor/database-users.htm

I need to obtain the User.  Any ideas?
Microsoft Access

Avatar of undefined
Last Comment
PatHartman

8/22/2022 - Mon
ste5an

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.
Abhigyan Srivastava

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)
      Else

         ' An error occurred.
         MsgBox "Unable to get the name."
         End
      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

ASKER CERTIFIED SOLUTION
Rey Obrero (Capricorn1)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jim Dettman (EE MVE)

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:

https://www.experts-exchange.com/articles/5328/Resource-locking-in-your-applications.html

 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.

Jim.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
PatHartman

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.
Rey Obrero (Capricorn1)

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.
PatHartman

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CipherIS

ASKER
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.
Rey Obrero (Capricorn1)

<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.
PatHartman

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.

CiperIS,
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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes