using a static variable in two different form modules

P

Paul James

I'm trying to use the same static variable in two different form modules -
one being the main form and the other its subform.

I'm using this variable to set the value of a subtotal control in the main
form, that functions as a sort of calculator. As the user checks or
unchecks a Paid field in the subform datasheet, that subtotal displays the
sum of the Paid records. So I need the static variable to remember it's
value between procedure calls. However, I also need it to respond to events
in the main form. For example, when the user clicks a certain control on
the main form, it unchecks all the Paid fields in the subform, and it needs
to reset the static variable to zero. The same variable is also used in the
Form_Current event of th main form.

This variable seems to be working for the most part, but I've noticed that
under certain conditions the variable doesn't get properly reset. I think
it may just be a flaw in my programming logic, and I'm trying to track it
down. But I'm also wondering if it could be a problem with the way I've
defined the static variable. Here's what I've done:

In the standard Module1 I have the Dimension statement

Public subTotal

(The compiler produces an error if you try using Static subTotal in the
definition area of Module1).

In each of the procedures in the form modules that use the subTotal
variable, I include the statement

Static subTotal

My question is this: is this a legitimate way to define a static variable
that's used by procedures in two different form modules? Or do I need to
modify my variable definitions?

Thanks in advance,

Paul
 
B

Brendan Reynolds

No, the way you have the variables declared at the moment makes them
completely separate and independent variables. The variables declared with
Static in the individual procedures have no connection to the variable
declared at module level, nor to each other.

It might be easier to use a hidden text box.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
P

Paul James

No, the way you have the variables declared at the moment makes them
completely separate and independent variables. The variables declared with
Static in the individual procedures have no connection to the variable
declared at module level, nor to each other.

You're right. That's what I'm experiencing.
It might be easier to use a hidden text box.

Of course. That would work - don't know why that didn't occur to me.
But now that you mention it, I don't know why I couldn't just use the
visible control itself, where I'm actually displaying the subtotal value.
I'll try it.

Thanks, Brendan.
 
T

Tim Ferguson

My question is this: is this a legitimate way to define a static
variable that's used by procedures in two different form modules? Or
do I need to modify my variable definitions?

I would be tempted to do the whole thing with a custom Property

Private Dim m_dblSubTotal

Public Property Get SubTotal() As Double
SubTotal = m_dblSubTotal
End Property

Public Property Let SubTotal(SomeValue as Double)
m_dblSubTotal = SomeValue
End Property

You can access these from anywhere else

With frmMainForm
.SubTotal = .SubTotal + 45.67


and with a couple of public functions (i.e. Methods) call something like
this:

frmMainForm.ResetSubTotal

and so on. The difference between Forms modules and regular Class Modules
is that the procedures default to private rather than public, so you have
to remember to mark everything Public Sub and so on.

HTH


Tim F
 
P

Paul James

Thanks for the suggestion, Tim. I haven't done any work with class modules,
so this is something I'll want to look into.

For the particular example in my original question, I followed Brendan's
suggestion about using a value in the form control, and I actually used the
visible control itself. Looking back on that question, I realize I was
trying to hold the light bulb and spin the ladder at the same time. Working
with the value in the subform control works just fine.

So thanks again for your suggestion and example. It gives me an idea for
something else I've been working on.

Paul
 

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