Jonapthan Wood said:
I need to return a value from a form displayed modally.
Many years ago, if I used VB classic, I could create a public method that
took care of any arguments, displayed the form modally, and then returned
appropriate return values. Something like this:
Public Function GetValue(Args As Integer
m_Args = Args
m_Result = DefaultResult
Show vbModal
GetValue = m_Result
End Function
However, it appears Access VBA doesn't support this. For one thing,
there's no Show method (and so it certainly can't have a modal argument).
No, but you can open the form as dialog, and "dialog" is set in your code,
not
in the forms property sheet. Keep in mind that modal forms and dialog forms
are
two different things and types of forms in access. (we have both). model
does
not cause code to wait, but dialog forms DOES halt the calling code.
So, lets say we need a form with a combo box, a check box..and some other
things
that we need returned BACK from that form.
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 code behind the ok button in the
GetName form does NOT close the form, but does a
me.Visible = 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. Setting
visible = false is what kicks the form OUT OF dialog mode and calling code
now continues to run
if isloaded(strF) = true then
' code goes here to example values
strName = forms(strF)!ThenameField
strSex = forms(strF)!GenderField
' ok, got our data...lets close the form (don't forget this!!)
docmd.Close acForm,strF
else
' if the form is not open, then user hit the "x", or the cancel
' button on the frmGetComboName form. Note that the cancel button on
' this form simply does a docmd.close
msgbox "user canceled"
end if
You have to handle the cancel logic anyway (even in VB6 you did). So, the
above is really not any more code. The whole code snip thus looks like:
strF = "frmGetComboName"
docmd.OpenForm strF,acNormal,,,,acDialog
if isloaded(strF) = true then
' code goes here to grab values, or even
' variable values (members of the form)
strName = forms(strF)!ThenameField
strSex = forms(strF)!GenderField
someVar = forms(strF).m_publicVarValue
' ok, got our data...lets close the form (don't forget this!!)
docmd.Close acForm,strF
else
' if the form is not open, then user hit the "x", or the cancel
' button on the frmGetComboName form. Note that the cancel button on
' this form simply does a docmd.close. So, code goes here for cancel:
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 my sig
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.
So, the ability in this case to return values is thus much like vb6.
The above code + example is here:
http://www.members.shaw.ca/AlbertKallal/Dialog/Index.html
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(e-mail address removed)
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
And, in access 2003 and later, you don't need the above function and can
use:
currentproject.AllForms("Name of form").IsLoaded