Passing results back from form to form

  • Thread starter Thread starter Max Moor
  • Start date Start date
M

Max Moor

Hi All,
I hope this is just a simple thing I'm missing. I know about using
OpenArgs to get data to a form being opened. How do I get data back from
one being closed?
I want to open an "InputBox" of my own design, and pass two values
back. What's the simple - and accepted - way to do this?

- Max
 
Of course, if code opens a form, it does NOT wait, the calling code
continues to run. Thus, what we need is the call code (the docmd.Openform)
to halt, and wait while the user selects some value from the prompt form.
When done, then your calling code can examine the form' values.

Ok, 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 cancel, or 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).

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 above form was actaully a combo box
but it can all kindes of text boxes, and can certanly be a un-bound form
that looks like a msg box).
The code behind the ok button in the
GetName form does NOT close the form, but does

me.Visiable = 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 = fomrs(strF)!GenderField
' ok, got our data...lets close the form
docmd.Close acForm,strF
else
' if the form is not open, then user hit the "x", or the cancel
' button on the GetName form. Note that the cancel button on
' the GetName from 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
 
Back
Top