Exit Windows Under Program Control

Problem

You’d like to be able to control what happens once you quit your applications: you may want to shut down Windows at the same time, or perhaps even reboot the machine. How can you do that from within Access?

Solution

The Windows API provides an ExitWindowsEx function that grants you control over exiting Windows, and you have a choice of three different things you can do: log off and await a new login; shut down to the point at which it’s safe to turn off the computer’s power; or reboot the computer. This solution demonstrates these simple functions.

To try closing Windows under program control, load and run frmExitWindows from 11-06.MDB. This sample form, shown in Figure 11-7, allows you to choose from the three options. Make your choice and click on the Go button, which will execute the code necessary to quit in the manner you’ve specified.

frmExitWindows presents three options

Figure 11-7. frmExitWindows presents three options

To use this functionality within your own applications, follow these steps:

  1. Import the module basExitWindows from 11-06.MDB.

  2. Call the function from Table 11-4 that best suits your needs. In each case, if the function returns at all, it indicates that some Windows process wasn’t able to shut down and that your function call failed. This won’t happen often.

    Table 11-4. Available functions for exiting Windows

    Function

    Description

    acbLogOff

    Shuts down ...

Get Access Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.