Shared Access Database Management

Here is a handy setup for those who need to manage a shared access database.

Either you can create this from scratch as per the example or add the new tables
to your existing database.

This setup will track:

1. the users currently using the database
2. what time the user and computer name currently logged in.
3. what version of the database the user is currently using.
4. allow the shutdown of a user’s database at your (the database manager) discretion.

Items 1-3

This screenshot will show the users currently in the database and the minutes they have been logged in.

In the backend database we are going to have a table called “tblSystemSecurityUsers” which will hold this information (see image above).

We will be linking to it from our front end databases all the users will use.

This is the code behind “frmWelcome”. This form will launch when the database starts up.

Option Compare Database

Private Sub btnClose_Click()
    CloseDatabase

End Sub

Private Sub Form_Close()
    CloseDatabase
End Sub

Private Sub Form_Open(Cancel As Integer)
    'update the shared table with the login
    
    Dim dblVersion As Double
    Dim rst As Recordset
    
    dblVersion = DLookup("DBVersion", "tblVersion")
        
    'IMPORTANT: Both linked and local tables can use the FindFirst methon, if you specify _
        dbOpenDynaset on the second argument above.

    Set rst = CurrentDb.OpenRecordset("tblSystemSecurityUsers", dbOpenDynaset)
    
    'find the manually entered user you are tracking
    rst.FindFirst "LoginName='" & Environ("Username") & "'"

    rst.Edit
    
    rst.Fields("LoginName") = Environ("UserName")
    rst.Fields("LoginComputerName") = Environ("ComputerName")
    rst.Fields("LoggedIn") = True
    rst.Fields("LoginTime") = Now()
    rst.Fields("LoggedInVersion") = dblVersion
    
    rst.Update
    
    rst.Close
    Set rst = Nothing
    
    
    'update the current form with currently logged in users
    CurrentDb.Execute ("DELETE * FROM tblSystemSecurityUsers_Local")
    
    DoCmd.SetWarnings False
    DoCmd.OpenQuery ("qrySystemSecurityUsers_APPEND")
    DoCmd.SetWarnings True
    
    Me.lstOnline.Requery

End Sub

Private Sub Form_Timer()
    Me.lblTime.Caption = Now()
    
    'update the current form with currently logged in users
    CurrentDb.Execute ("DELETE * FROM tblSystemSecurityUsers_Local")
    
    DoCmd.SetWarnings False
    DoCmd.OpenQuery ("qrySystemSecurityUsers_APPEND")
    DoCmd.SetWarnings True
    
    Me.lstOnline.Requery
    
    DoEvents
    
    ' if file exists then shutdown db
    If ShutdownFile = True Then
        CloseDatabase
    End If
    
End Sub

Use the form’s properties to set the timer:

Item 4

Notice the “If ShutdownFile=True … ”

If the “shutdown.txt” file exists in a specified directory, that user’s front end will shut down.

Here is the code:

Public Function ShutdownFile() As Boolean
    
    Dim blnTemp As Boolean
    
    Dim objFSO As Object
    Dim strFileName As String
    
    'make this a path on your network
    strFileName = CurrentProject.Path & "\shutdown.txt"
    

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If (objFSO.FileExists(strFileName)) Then

        'MsgBox "The database requires a shutdown now. The database will be closed.", vbInformation, APP_NAME
    
        blnTemp = True
    Else
        blnTemp = False
    End If
    
    Set objFSO = Nothing
        
    ShutdownFile = blnTemp
    
End Function

This code runs on the timer event of the form, and checks for the existence of the “shutdown.txt” file.

…and here is the shutdown process:

Public Sub CloseDatabase()
    
    Dim rst As Recordset
    
    Set rst = CurrentDb.OpenRecordset("tblSystemSecurityUsers", dbOpenDynaset)
    
    'IMPORTANT: Both linked and local tables can use the FindFirst methon, if you specify _
        dbOpenDynaset on the second argument above.
         
    rst.FindFirst "LoginName='" & Environ("Username") & "'"

    rst.Edit
    
    rst.Fields("LoggedIn") = False
    rst.Fields("LogOffTime") = Now()
    
    rst.Update
    
    rst.Close
    Set rst = Nothing
    
    Application.Quit acQuitSaveNone
End Sub

Perhaps you can “morph” this to use it in your own unique way.

Here are the database files:

SharedAccessDatabase.accdb

SharedAccessDatabase_BE.accdb

Let me know if you have any questions.






By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How do I handle errors in Access VBA code?

I am going to give you the answer to “How do I handle errors in Access VBA code?” In my opinion there are 2 ways to handle errors: 1. Avoid the potential for an error to occur. 2. Handle the error in your code. Number 1 – If you have a control on your form […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

What Is Microsoft Access Used For?

To those of you who are asking the question of “What is microsoft access used for?” , here is my opinion. I’ve been using this program for well over 15 years, and it’s become fairly easy to deal with. Many people feel that it is hard to work with, but that’s not my experience anymore […]

How To Make An Access Select Query

Hi everyone, This is not really a VBA item, but it’s important and you will need to understand this concept when you are creating your SQL statements in VBA. In our example we have a table called “Customers”: Now we just want the customers from “London”. So we set up a select query to select […]

Previous Post

How To restrict user logon to specific computers

Next Post

Access To PHP MySQL Tutorial