Navigation:  Articles > Nov-1999 >

Tip: When is Modal Not Modal?

Previous pageReturn to chapter overviewNext page

Christopher R. Weber          

There’s a bug in Access 97 when drilling down with popup modal forms. A modal form is one that, when opened, prevents the user from working on any other form until the modal form is closed. Normally, when you open a modal form, it appears on top of any other form.

My problem was that when opening a series of modal forms, the most recently opened modal form didn’t always appear on top. As I investigated, the problem got more interesting: The problem form didn’t even know that it was modal! I tested this by adding a button to a modal form to have it display its Modal property:

Private Sub cmdTest_Click()

  MsgBox CBool(Me.Modal)

End Sub

While the form appeared to open modally, the message box sometimes returned False.

This problem surfaced consistently when my user tried to add values to a combo box by opening a series of dialog boxes. For instance, while editing an Order line item, the user might want to add a new Product to the Product’s combo box. Using the usual NotInList event technique, I’d open a modal form for them to add the value. That form required the user to select a Supplier for this new Product from a combo box. Not surprisingly, while adding a new Product, the user frequently needed to add a new Supplier to the Supplier combo box. While adding the new Supplier, the user might need to add a new ZipCode to the ZipCode combo box, and so on.

My user discovered, when drilling in to add value on top of value, that Access doesn’t always stack the dialog boxes on top of each other in the order in which they were opened. This makes my user interface fall apart unless I can ensure that my modal forms remain open in the proper order (ZipCode atop Supplier atop Product atop Order).

I solved my dilemma by using a pair of API calls. The first, BringWindowToTop, causes the window whose handle you pass to the routine to become the topmost window. The second helps me determine the true state of the window (which is what the form’s Modal property was supposed to do):

Declare Function BringWindowToTop Lib "user32" _

   (ByVal hwnd As Long) As Long

Declare Function GetLastActivePopup Lib "user32" _

   (ByVal hwnd As Long) As Long

Now, when I open my form to add an item to the combo box, I make sure the dialog box pops to the top with BringWindowToTop:

lngReturn = BringWindowToTop(Me.hwnd)

I can check to see whether a form is really a modal form by adding this code to it:

If Not GetLastActivePopup(hWndAccessApp) _

        = Me.hwnd Then

  …processing…

End If

Hopefully, Microsoft will address this bug in Access 2000. Meanwhile, be assured that you can control your dialog boxes and determine their modal states.