This page focuses specifically on creating and using custom dialog boxes using MS Access 97.
Overview
DoCmd.OpenForm "FormName", , , , , acDialog, "Passed Parameter"The calling form even waits until the dialog box is closed.
The problem is how to get data back from it.
tempStr = Forms![Form Name]!FieldName.Value ' Either . or !
tempStr = Forms("Form Name").FieldName.Value ' Either . or !
tempStr = Forms(DialogName).Form.Tag ' The dot is required
Unfortunately, when a dialog box is closed, there is no form
to get the data from.
The solution is simple - don't Close the dialog, just Hide it. Of course, the help explicitly says "other windows in Microsoft Access are disabled until you close the form". (Suprise - the help is wrong.)
Returning Values via Form.Tag
Private Sub OK_UIButton_Click()
' Hide dialog box so the parameters are still available
Me.Visible = False
Tag = ProductID.Value
End Sub
Private Sub Cancel_UIButton_Click()
' Hide dialog box so the parameters are still available
Me.Visible = False
Tag = -1
End Sub
However, if you just hide the form, the next time it is called,
the calling code usually won't wait until the user enters new data.
(I've seen it both ways.)
As a result, it is necessary for the calling form to
actually close the dialog box after the data has been read.
Private Sub Command3_Click()
Dim tempStr As String
Dim ResultFlag As String
Dim DialogName As String
DialogName = "Locate Product" ' The name of the dialog box
tempStr = ProductID.Value ' Parameter used by the dialog box
'DoCmd.OpenForm "FormName", , , , , , "Passed Parameter"
DoCmd.OpenForm DialogName, , , , , acDialog, tempStr
ResultFlag = Forms(DialogName).Form.Tag ' Uses String
'ResultFlag = Forms![Locate Product].Form.Tag ' Alternate syntax
'Unbound_UIField.Value = ResultFlag ' for test only
If ResultFlag <> Str(-1) Then ' Test that OK was pressed
tempStr = Forms(DialogName)!ProductID.Value
ProductID_UIEdit.Value = tempStr
End If
' Without an explicit "DoCmd.Close",
' * The dialog box's Form_Open is run only
' the first time "DoCmd.OpenForm" is called
' * When the form is merely hidden,
' "DoCmd.OpenForm" does not always wait for the
' user to hide the dialog box - very unpredictable
DoCmd.Close acForm, DialogName
End Sub
If you elect to return a button ID via Form.Tag, then I suggest using either string values or the standard MsgBox constants (vbOK vbCancel vbAbort vbRetry vbIgnore vbYes vbNo - with values from 1 to 7).
Using a Module
' This routine accepts the name of the dialog box (form)
' and a single ID value. It returns returns the original value
' if either Esc or Cancel is pressed, otherwise, it returns
' the new value
Public Function OpenDialogBox_Int_MC _
(DialogName As String, CurrentValue As Integer) As Integer
Dim ResultFlag As String
DoCmd.OpenForm DialogName, , , , , acDialog, CurrentValue
ResultFlag = Forms(DialogName).Form.Tag
If ResultFlag <> Str(-1) Then ' Test that OK was pressed
OpenDialogBox_Int_MC = ResultFlag
Else
OpenDialogBox_Int_MC = CurrentValue
End If
DoCmd.Close acForm, DialogName
End Function
This only accepts and returns a record ID (long integer),
a separate routine (OpenDialogBox_Str_MC) accepts and
returns string identifiers (used for short lists, <50 elements -
I have no idea what I ment by this).
Call this routine with something similar to
tempInt = OpenDialogBox_Int_MC("DialogBoxName", RecordID)
I use _MC (ModuleCode) to indicate code that I've written which is located in a module. This just makes it a little easier for other people to read my code.
There are several techniques to get data back from a dialog box
References and Related Information