Access Cookbook - Ken Getz [52]
' Open the form in dialog mode. The code will
' stop processing, and wait for you to either close
' the form, or hide it.
DoCmd.OpenForm acbcInputForm, WindowMode:=acDialog
' If you get here and the form is open, you pressed
' the OK button. That means you want to handle the
' text in the textbox, which you can get as the
' Response property of the form.
If IsFormOpen(acbcInputForm) Then
acbInputBox = Forms(acbcInputForm).Response
DoCmd.Close acForm, acbcInputForm
Else
acbInputBox = Null
End If
How do you know if the form is still open? This code uses the IsFormOpen function, as follows:
Private Function IsFormOpen(strName As String) As Boolean
' Is the requested form open?
IsFormOpen = (SysCmd(acSysCmdGetObjectState, acForm, strName) <> 0)
End Function
IsFormOpen relies on the Access SysCmd function, which, among other things, can tell you the current state of any object. In this case, if there is any state for the object (that is, if SysCmd returns anything besides 0), the form must be open.
Finally, to retrieve the return value from the pop-up form, you can use a user-defined property of the form. In this case, we set up Response to be a property of the form that returns the value that you typed into the text box on the form. You could, of course, retrieve that value directly, but this means that the caller has to have information about the controls on the pop-up form. This way, by exposing a defined interface between the caller and the form, it doesn't matter how you rename or change controls on the form; as long as the form continues to provide the Response property, your code will still work.
To provide the read-only Response property, frmInputBox's module includes the following Property Get procedure:
Property Get Response( )
' Create a user-defined property, Response. This property
' returns the value from the text box on the form.
Response = Me.txtResponse
End Property
This procedure allows outsiders to retrieve what appear to be properties of the form itself. With this Property Get procedure in place, you can use syntax like this to retrieve the property:
acbInputBox = Forms(acbcInputForm).Response
VBA supports Property Let, Get, and Set procedures. See the VBA online help for more information.
Initializing pop-up forms
You've handled the input parameters and opened the dialog form. How do you tell that form what those parameters were? Just as forms can expose properties, modules can expose public variables that other modules and forms can view and modify. In this case, acbInputBox placed the appropriate parameters into various module public variables (varPrompt, varDefault, varXPos, etc.). Code attached to the pop-up form's Open event retrieves the values of those public variables and uses them to initialize itself. As shown in the following code, these variables can be accessed as properties of the module (basInputBox.varDefault, for example). Here is the Form_Open event procedure:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo HandleErr
Me.txtResponse = basInputBox.varDefault
Me.Caption = basInputBox.varTitle
Me.lblPrompt.Caption = basInputBox.varPrompt
If Not IsNull(basInputBox.varHelpFile) And _
Not IsNull(basInputBox.varContext) Then
Me.cmdHelp.Visible = True
' Set things up for the Help button.
mvarContext = basInputBox.varContext
mvarHelpFile = basInputBox.varHelpFile