Carrying Info From One Form to Another

  • Thread starter Thread starter TimWillDoIt
  • Start date Start date
T

TimWillDoIt

Hello all. I can't believe how helpful this forum has been to me in the
short time I've been expanding my horizon into VBA, both in asking a
questions and just reading all the posts and learning things that MIGHT apply
at some point. Thank you.

I've created a form that will serve as the primary form and I've created
other forms that will open when I want to perform a calculation or manipulate
some data. I don't want to make all my declarations every time I open a form
or run an Event Procedure. I've REM'd out all the DIM and SET statements in
the "child" forms and pasted them in the Open event of the main form.

It all seems to work...until I reference one of the Recordset objects in the
"child" form. The message tells me that an object is needed, which means to
me that the Recordset object I've declared in my main form didn't carry to my
child form.

Is there something I need to do to link the forms? Or to pass the
information from one form to another? Thanks.
 
You are going backwards.
What you are trying to do is set up global variables and establish global
objects. That is not a good idea. You Dim and Set statments should be at the
lowest level possible. With all variables and objects, there is a concept
called Scope. That is, at what level each is visible.
Local variables defined in subs or functions (procedures) can only be seen
in the procedure in which they are defined. If a variable is only used in a
procedure, that is where it should be dimmed.

In some cases, a variable or object may need to be visible to the module.
This is when in a form, for example, you need to use a variable from anywhere
in the form's code, you would dim it at the top of the module after the
Option statements but before any code. Now any procedure in the module will
see the variable.

The highest level is Global. A global variable has to be dimmed in a
standard module at the top of the module after the Option statments, but
before any code. You will find that most professional developers don't use
global varialbes. There are problems with them. For example, if you have an
unhandled error any where in your application, all global variables loose
their values. And, it is very easy to inadvertently use that variable in two
different routines and have an unexpected value in the variable.

If you write your application well, there is no real need for global
variables.
 
Dave,

Thank you for that information, it is hugely helpful. So...if there's
data in a variable that I'd like to pass from one form to another what's the
best way to do that?

I've taken the results of a calculation in the "child" form and placed it
in the Text Box of the "parent" form when the child closes, but without a
Text Box in which to place it, would the correct solution be to put the data
in a table record and then access it from the new form? Thanks.

Tim
 
It really depends on how the forms interact, but in the specific case you
describe, I would put a text box on the receiving form and make it's Visible
property No. That way, you can use it in code, but the user will not see it.

Using a table field would be my very last choice. It is much slower to
write to a table then read back from that table than to pass the value as
described above.
 
Back
Top