Full Version: Returning a value from a form in a function...
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
cbtoolkit
There may be an easy answer to this but I couldn't find it....

I have a form that I want to return a date value - basically it is a calendar control that I can use throughout my app. I want to create a function that opens the form, potentially passing it a date, opening the form, and then, when another date or cancel is clicked, closes the form, returns to the calling function, and sets the value in the designated control.

something like...
==============================
CODE
  
Function GetCalValue(optional inDt as date)

If IsNull(inDt) then
  inDt - Date()
End If

' open the form here and return the value from the form???
GetCalValue = 'open form modal and return value based on user selection

End Function

=======================================

I am missing something obvious?

Thanks.
Newander5
No, you're not missing anything obvious. Forms just don't return values, and on destruction they delete all variables associated with themselves.

The best workaround I have found is tro create a public (global) variable and have the form clear that value on entry and set it on exit.

Then just be sure that your function opens the form as a modal dialog to ensure execution freezes until the form returns.

HTH,
New
cbtoolkit
Thanks. I was thinking that might be the route. I just didn't want the global variable.

That is one of the nice things with DHTML forms where you can make the form (window) return a value.

Thanks.
cbtoolkit
another question. How do I ensure code execution halts in the function until the form exits?

I could do a loop that runs as long as the form is open? Is that the method your thinking of or is there a simpler/more elegant way.

Thanks.
Newander5
the simpler more elegant way is to open the form as a modal dialog, which can be accomplished like so:

DoCmd.OpenForm(<FormName>, , , , , acDialog, <any OpenArgs you might want>)

Setting the windowmode to acDialog tells it to suspend whatever is going on until the form is closed.

HTH,
New
cbtoolkit
That didn't work. The form opens modal but the function I've contained the open form code in returns the default value immediately.

I'll play around a bit more and report back.
Newander5
The only other stipulation is to make sure the form you are popping up has the Modal and Popup properties set to yes.

I use this method a lot to impose my own InputBox function because I hate the display of the default one.

New
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.