Wrapping a Form Call in a Function

How do I wrap a form in a function?

There are lots of cases where you may want a pop-up form to gather user input and return a value to a "calling" form. Calendars, calculators and password pop-ups are some common examples.

One way to do this is to "wrap" the pop-up form in a function call. Here is an overview of the concept, with frmMain being the main or "calling" form, and frmPassword being the pop-up or "called" form.

1. In frmMain in response to a button click.
2. Call the wrapper function.
3. The wrapper function opens frmPassword in dialog mode.
4. The "OK" button on frmPassword will hide the form (not close!)
5. The wrapper function gets the data from a control on frmPassword.
6. The wrapper function then closes frmPassword
7. The wrapper function returns the retrieved value.
8. frmMain now has the value that was entered in frmPassword.

This sounds like a lot of steps, but once this is implemented, use is very simple and would look like this in response to the button click in frmMain. In the sample, GetPassword() is the wrapper function.

Private Sub cmdTest_Click()
    Dim sPassword As String
    sPassword = GetPassword()
    MsgBox "You entered: " & sPassword
End Sub
The attached sample works just as described above. It is a very simple implementation to illustrate this technique. After reviewing the sample, you should be able to implement your own wrapped pop-up forms.

Wrapped form sample db