Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: Access Modules (VBA Coding) FAQ

How To

View login ID's of users currently using the DB by sucoyant
Posted: 27 Feb 04

There are other FAQ's that show how to view users currently logged in using the computer name, and sometimes that just wont work.

This FAQ will explain how to view the LOGIN ID's of users currently using the database.

The process of how this works goes like this:
Open DB -> Auto loading form gets called -> Grabs Login ID -> Places Login ID into empCurrentlyLoggedIn field in tblCurrentlyLoggedIn table
Close DB -> Auto loaded form deletes your Login ID from tblCurrentlyLoggedIn -> DB closes

We will need a few things to make this work:
 Table: tblCurrentlyLoggedIn
   Fields: empCurrentlyLoggedIn (Text datatype)
 Code: basfOSUserName
 Form: frmShowDatabaseUsers
    List Box - lstEmpName
    Command Button - cmdExit

For this FAQ we will be using some excellent code by Dev Ashish. This code grabs the LOGIN ID of the user:


******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
' Code Courtesy of
' Dev Ashish
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If ( lngX > 0 ) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
        fOSUserName = vbNullString
    End If
End Function
'******************** Code End **************************

Place the above code into a new module called "basfOSUserName".

If you are using a form that automatically launches when you open the database, this will be easy. For simplicity I will assume this.

In the "On Open" event of the auto launching form past the following code:


DoCmd.SetWarnings False

DoCmd.RunSQL "INSERT INTO tblCurrentlyLoggedIn ( empCurrentlyLoggedIn ) " & _
             "SELECT fOSUserName() AS Users;"

DoCmd.SetWarnings True

Then in the "On Close" event past this code:


DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblCurrentlyLoggedIn WHERE empCurrentlyLoggedIn = fOSUserName()"
DoCmd.SetWarnings True

Now, you must create a new form called "frmShowDatabaseUsers". Place the 2 items needed on the form from the list at the top of the page.

Set the Row Source Type to "Table/Query" for lstEmpName.
Set the Row Source to "tblCurrentlyLoggedIn".

In the "On Click" event of the cmdExit button place the following code:


On Error GoTo Err_cmdExit_Click


    Exit Sub

    MsgBox Err.Description
    Resume Exit_cmdExit_Click

You're done!

Back to Microsoft: Access Modules (VBA Coding) FAQ Index
Back to Microsoft: Access Modules (VBA Coding) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close