Online Book Reader

Home Category

Access Cookbook - Ken Getz [52]

By Root 1842 0
will return back to you the information it gathered on that form. The key to this process is in using acDialog as the WindowMode argument when opening the form. That way, the code processing in the original function waits, and the form doesn't relinquish the focus until you've either hidden it (which is what pressing the OK button does) or closed it (which is what pressing the Cancel button does). Once back in the original function, it can check to see if the form is still loaded (indicating that you pressed the OK button) and, if so, retrieve the information it needs directly from the form and then close the pop-up form. Here's the code from acbInputBox that does all that work:

' 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

Return Main Page Previous Page Next Page

®Online Book Reader