Best way to get info from 'called form' back to 'calling form'.

T

ThomasAJ

I have a form that's opened in dialogue mode from another form's code.
What is the best way to get info from the 'called form' back to the 'calling
form'.

I ask this as there are usually many ways to do this but often there is
usually a best way that covers all possible potential problems.
 
W

wphx

I'd be interested in replies to this also.

One method I used which works well for me, was to write a couple 'stack'
functions. Using a global variant array, I 'push' arguments onto the stack
before calling the form and then 'pop' them off the stack within the form
newly opened - then works the opposite when returning data. But I'd be
interested if there were other methods.
 
K

Klatuu

Anything using Global variables is unreliable. Global variables get reset if
there is any error thown.

In most cases, you will probably be wanting to put the value received from
the called form in a control on the calling form. If not, you can create a
hidden control on the calling form to hold the data. Then in the called
form, it is just a matter of populating the controls on the calling form.
Note, it is a good practice to test to make sure the calling form is loaded
before trying to address it>


If CurrentProject.AllForms("CallingForm").IsLoaded Then
Forms!CallingForm!txtSomeData = Me.txtSomeData
Forms!CallingForm!txtMoreStuff = Me.txtMoreStuff
Else
MsgBox "CallingForm is Not Open - Cannot Update"
End If

Another way, if the above will not do for some reason, is to create a Static
Function rather than use a global variable. Static Funtions will not loose
their value when an error occurs. The variables in a Static function are
different from a normal function where all varilables loose their value
between calls. The varilabes in a Static function or a variable Dimmed as
Static will return its current value until explicitly changed or you close
your instance of Access.

Public Function SomeUslessInfo(Optional varInput As Variant) As Variant
Static varStuff as Variant

If Not IsMissing(varInput) Then
varStuff = varInput
End If

SomeUselessInfo = varStuff
End Function

The above is not a static function, but has a static variable. If you call
the function and pass it a value, it replaces the current value in the
varialbe varStuff. If not, the value of varStuff does not change. In either
case, the value of varStuff is returned by the function. So, in the Called
form, you can pass the value with

SomeUselessInfo = "Foo"

Then in the Calling form

Me.txtSomeData = SomeUselessInfo()

Put the function in a standard module.

--
Dave Hargis, Microsoft Access MVP


wphx said:
I'd be interested in replies to this also.

One method I used which works well for me, was to write a couple 'stack'
functions. Using a global variant array, I 'push' arguments onto the stack
before calling the form and then 'pop' them off the stack within the form
newly opened - then works the opposite when returning data. But I'd be
interested if there were other methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top