HCS Consulting Group.
We make Complex Systems simple

Building dialog and prompt forms in ms-access.

By Albert D. Kallal
Friday, June 04, 2004

Dialog and prompt forms in Access.

The following prompt form information is likely one of the BEST things you can learn to improve your UI in ms-access.

Of course with access, we have what we call event driven programming. You click on a button, and some code runs. As normal course of things, this is usually a good thing. However, often we do want some procedural type things to happen. You might want to pop up a form to ask for a date, or date range, or even have a custom yes/no box that has additional features that you don't have with built in dialog boxes, and the built in input box.

The reason why we find the built in msgbox, and the built in input box so handy is that they are dialog boxes.

Dialog boxes in ms-access are SO often confused with modal forms. A modal form is a form that keeps the focus, and you MUST close that form to return to where you came from. This setting is found in the forms "other" tab. Often, when you have a series of forms you need to force the user to return back the way they came from, you use the modal setting to accomplish this.

A dialog box, or a dialog form on the other hand is quite a bit different. In fact, they are VERY different from modal forms. DO NOT CONFUSE THE two!!! They are very different, and have very different uses. Further, the ONLY way to open a form in dialog mode is to use:

docmd.OpenForm "yourFormName",acNormal,,,,acDialog

For a modal form, this is a setting you find in the forms "other" tab of the properties sheet. Again, i can't stress how different these two features are, yet, many ms-access users, and even some developers are still confused about these two different features. So, the modal setting of the form is not really done via code (but by simply using the "other" tab). Further, the docmd.OpenForm does not have any provisions for setting the modal option (but, it does let you set the acDialog feature). So, modal forms, and acDialog forms are VERY different.Mission Hill Wines - Picture by Albert D. Kallal

First, when you pop up a msgbox, (or a dialog form), you are STUCK in that dialog. You can't even use the menu bar, can't right click, in fact can't do  anything else!!. So, a dialog form is MUCH more nasty then is a modal form. So, in code when you open up a msgbox, or a dialog form, the CODE HALTS and WAITS for the users input. It is important to note that NOTHING else can occur. A modal form on the other hand allows full use of menus, and of course does not halt code.

If we open a form, and want to set some datefield to today, we can go

docmd.OpenForm "MyCoolForm"

forms!MyCoolForm!MyCoolDate = date()

The above of course opens the form, and the code continues to run..so we can actually setup, or do things to that form.

Often, for re-usable code, and forms, we would like the above form to wait for the user done with the form.

This means we can build forms that ask users for info, and have that form return the values back without using a bunch of global vars, or other weird things.

Here is how you do it. You open the form in acDialog mode. This will
halt the calling code, and wait. The user then enters some data into fields,
and the hits the ok button. The trick here is that the ok button DOES NOT
close the form, but sets the forms visible property to false. The will cause
the form to drop out of acDialog mode, and then the calling code will
continue!. If the user does in fact hit you cancel button , or simply closes the form, you
consider that as a CANCEL!! (ie: the cancel, or even the "X" in the upper
right corner is thus considered a close/cancel).

So, lets say we need a form with a combo box, a check box..and some other things.

Hence, the code looks as

' lets open a prompt form
strF = "frmGetComboName"
docmd.OpenForm strF,acNormal,,,,acDialog

' the above code opens our form "frmGetComboName", and the waits until the user
enters some data, and then hits ok. The code behind the ok button in the
GetName form does NOT close the form, but does

me.Visible = false

' at this point, either the user hit ok, or closed the form. If the form is
still open, then user did not cancel, and we assume he hit OK

if isloaded(strF) = true then
   ' code goes here to example values
   strName = forms(strF)!ThenameField
   strSex = forms(strF)!GenderField
   ' ok, got our data...lets close the form (don't forget this!!)
   docmd.Close acForm,strF
  ' if the form is not open, then user hit the "x", or the cancel
   ' button on the frmGetComboName form. Note that the cancel button on
   ' this form simply does a docmd.close
   msgbox "user canceled"
end if

You also need the code for isloaded. It can be found in tons of examples,
and also in the northwind traders. It is reproduced below just in case
you don't have it:

You can also thus wrap the whole acDialog form code in a function that
returns a value if you want. (I do this for date calendar prompts, and
you thus get re-usable form that can be used anywhere by a simple function).

Function fIsLoaded(ByVal strFormName As String) As Integer
   'Returns a 0 if form is not open or a -1 if Open
   If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> 0 Then
      If Forms(strFormName).CurrentView <> 0 Then
         fIsLoaded = True
      End If
   End If
End Function

Albert D. Kallal
Edmonton, Alberta Canada