What is the best way to transfer data between forms?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

When I want to transfer data between forms, I define public variables in a module and make the forms access the data via the public variable.

Problem is that when the code stops running, the data dissappears even though it is defined as public.

What is a good way to exchange information between forms? How do you define your settings? Do you define a specific table to store data?

This was a general question but I would like to have different opinions.

Thanks
Kamil
 
Kamil Dursun said:
Hello,

When I want to transfer data between forms, I define public variables in
a module and make the forms access the data via the public variable.
Problem is that when the code stops running, the data dissappears even
though it is defined as public.
What is a good way to exchange information between forms? How do you
define your settings? Do you define a specific table to store data?
This was a general question but I would like to have different opinions.

Are you seeing runtime errors? These will often cause public variables to
lose their values. Otherwise they should hold their value until your code
does something to change them.

You do have them declared in a standard module right? Not a Form, Report,
or Class module?
 
Hello,

When I want to transfer data between forms, I define public variables in a module and make the forms access the data via the public variable.

Problem is that when the code stops running, the data dissappears even though it is defined as public.

What is a good way to exchange information between forms? How do you define your settings? Do you define a specific table to store data?

This was a general question but I would like to have different opinions.

Thanks
Kamil

One real concern I see here:

Data is not stored in forms. You should not be using the mindset of
"transfer data between forms", in my opinion! If you want to store
data, yes, it should be stored in a Table, perhaps a temp table.

I've had trouble with temp variables too; if you must have a temporary
"parking place" for data, you can use an unbound control on a form -
but in general, data should be in tables.
 
The real question here is why do you need to transfer the data?

Further, if the form is open, then why not just use a reference to that
form?

In other words, it is FAR better to reference values in a form, then trying
to TRANSFER them.

For example, lets assume formA has to open formB.

Lets also assume that formB needs a bunch of values from formA

In place of transferring a bunch of values, it is far easier to simply use a
reference to that first form.

So, in the formsB on-open (and, in fact we can even use a event as late as
the on-load), we can grab a reference to the previous form.

In form B's module code we go:

Option Compare Database
Option Explicit

Dim frmP As Form ' prevous form

Then, in the forms on-load we now go:

set frmP = Screen.ActiveForm

So, now our form B has a ref to the previous form. To grab the key id, we
can go:

msgbox "value of form A key id = " & frmP!ID

We can also force form A to write its data to disk (say, we are about to do
some operation that needs this data for a report in form B), We can now go:

frmP.Refresh

And, any other value we need can be used.

msgbox "The company name is " & frmP!CompanyName

So, you see, why transfer the data, when you can use a reference tot he
previous form. This takes no global vars, you can grab ANY value from the
form, and further, you can also reference all of the forms properties and
events to do things like writing data to disk!


So, my answer is don't transfer data..but use a reference, and leave the
data where it belongs.

Perhaps your question is NOT in regards to data entry form, but how can one
make some prompt forms, and dialog forms that RETURNS values to a calling
form?

While the above form ref is about the best approach in ms-access to TRANSFER
things, if you need a form to RETURN values, and wrap it in a function (say,
a nice date prompt form), then you can read the following article of mine
that shows how to have a form return values, and NOT have to use global
vars:

http://www.attcanada.net/~kallal.msn/Dialog/Index.html
 
Back
Top