If you ever come across a scenario where you want to sign the user out of the computer upon exiting Access database, then this guide will help you! In our scenario, a client has a remote server where the database launches upon login, and we want them to be logged off upon exit, to result in no access to the server itself.

Here are the steps:

You need to create a hidden form, as there is no default action to detect access closing. On the Access Ribbon, navigate to Create > Form Design. In the properties of the form under Event > On Unload put the following VBA code:

Private Sub Form_Unload(Cancel As Integer)
End Sub

Save the form. In our example, we named the form “frmLogOff”.

Next, we need to create an Auto Exec Macro which will launch our hidden form upon the opening of the database. Go to Create > Macro. In Form Name select “frmLogOff” and make sure to switch Window Mode to “Hidden”. Make sure to save the macro as “AutoExec” as its the default macro name that Access checks for upon load.

AutoExec Sign out upon exiting access database

Now we must create the Module Function for the logoff. Navigate to Create > Module and insert the code below:

Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags&, ByVal wReserved&)
 '//constants needed to exit Windows
Global Const EWX_LOGOFF = 0
Global Const EWX_SHUTDOWN = 1
Global Const EWX_REBOOT = 2
Global Const EWX_FORCE = 4

Sub LogOffPC()    
    call  ExitWindowsEx(EWX_LOGOFF, 0&)
End Sub

In our example, we saved the module name as “ModLogOff”

That’s it! The next time you open your database, the hidden form “frmLogOff” will run with your default form. Whenever you decide to exit out of the database, “frmLogOff” closes and runs the command to log out of Windows.

If you have any questions, feel free to talk to us on the live chat, or give us a call during business hours. Happy coding!

Jeff Shirley

Jeff Shirley

Founder & CEO
Jeff brings over 27 years of experience to the table, along with numerous awards, certifications, and real-world implementations.  His database solutions are currently running in industries including government, mining, agriculture, finance, education, science, research, non-profits, and healthcare businesses around North America and serve thousands of users day to day.  Jeff is a five-time MVP Award recipient for Microsoft Access, acknowledging contributions to community projects, evangelism, and educational outreach on Microsoft technologies.  Today there are less than 50 Access MVP’s worldwide. While Jeff focuses day to day on business management, IoT development, IT integrations and design, database architecture or other typical complex tasks, he also is a co-owner of Blue Heron Gardens, a commercial apiary running over 200 beehives.  Fresh honey is a small perk for many of our clients!

Mitch Redekopp

Mitch works with our clients day to day ensuring web development and IT projects are done to exceed customer expectations.  His background includes formal training in IT, Microsoft and CompTIA certifications, real-world business experience, and a true dedication to ensuring clients receive timely and professional support on their projects with Rivercity Technology Services.  Given the most difficult requests for IT solutions, Mitchell can assist our clients in finding a path to the best options which deliver results for our clients.  Mitchell is an avid soccer player and Manchester City fan, be sure to talk soccer with him when you can.