Declaring variables

E

ericb

I have a problem declaring a variable.

I declared a variable like this at the top of a module :

Option Compare Database

Dim bntDossier As String


In the same module is a Form_Open sub that will set a value to it. If I
print it with msgbox I can see it is OK

But when I enter the Form_close sub and use msgbox it does not show anything.

How can I declare my variable so all the subs or functions can use it.

And is it possible to declare it so that all the other modules can use the
same variable ?

Thank you for the help
 
S

Squik27

I just tried what you did and it works fine.
Option Compare Database

Dim var As Integer

Private Sub Form_Close()
var = 7
MsgBox var
End Sub

Private Sub Form_Open(Cancel As Integer)
var = 5
MsgBox var
End Sub

and this works fine, there must be something else you're missing and as for
the other question what you want is a global variable that is visible to
every module you write wheter it belong to form or module. You need to create
a module, not a form, and declare at the top. you might need to use the
keyword public and then this variable will be visible to every single form.

Notice that many programmer will go against a global variable. There're some
issues with it. you could also have an invisible form will all of your global
as textboxes and refere to them like forms![form_name]![textbox_name] this is
much better because as long as the form isn't closed all the values will stay
alive. This doensn't happend with global, this is one of the issues
(specially when you're debuggin)

Hope this helps
 
P

PvdG42

ericb said:
I have a problem declaring a variable.

I declared a variable like this at the top of a module :

Option Compare Database

Dim bntDossier As String


In the same module is a Form_Open sub that will set a value to it. If I
print it with msgbox I can see it is OK

But when I enter the Form_close sub and use msgbox it does not show
anything.

How can I declare my variable so all the subs or functions can use it.

And is it possible to declare it so that all the other modules can use the
same variable ?

Thank you for the help

Have a look here:

http://msdn.microsoft.com/en-us/lib...cess2007VBA_Chapt2_UsingVariablesAndConstants

In particular, scroll down to the section "Scope and Lifetime of Variables
and Constants".
 
E

ericb

I did the same with an integer value instead of a string and it does not work.

In the close_form don't do var=7, just print and you should get 5. When I do
it I get 0.

Where is that bug ? so i can ...
--
eric


Squik27 said:
I just tried what you did and it works fine.
Option Compare Database

Dim var As Integer

Private Sub Form_Close()
var = 7
MsgBox var
End Sub

Private Sub Form_Open(Cancel As Integer)
var = 5
MsgBox var
End Sub

and this works fine, there must be something else you're missing and as for
the other question what you want is a global variable that is visible to
every module you write wheter it belong to form or module. You need to create
a module, not a form, and declare at the top. you might need to use the
keyword public and then this variable will be visible to every single form.

Notice that many programmer will go against a global variable. There're some
issues with it. you could also have an invisible form will all of your global
as textboxes and refere to them like forms![form_name]![textbox_name] this is
much better because as long as the form isn't closed all the values will stay
alive. This doensn't happend with global, this is one of the issues
(specially when you're debuggin)

Hope this helps

ericb said:
I have a problem declaring a variable.

I declared a variable like this at the top of a module :

Option Compare Database

Dim bntDossier As String


In the same module is a Form_Open sub that will set a value to it. If I
print it with msgbox I can see it is OK

But when I enter the Form_close sub and use msgbox it does not show anything.

How can I declare my variable so all the subs or functions can use it.

And is it possible to declare it so that all the other modules can use the
same variable ?

Thank you for the help
 
J

Jeff Boyce

How depends on what...

What version of Access are you using?

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.
 
S

Squik27

Sorry you got me on this one. This is my code

Option Compare Database

Dim var As String

Private Sub Form_Open(Cancel As Integer)
var = "1"
MsgBox var
End Sub

Private Sub Form_Close()
MsgBox var
End Sub

and it does work. I also tried different version and played around with
option explicit ans it does work. so I don't have an answer now.
 
S

Steve

When you declare a variable at the top of a module, the variable can be
assigned a value in any sub or function in that module. However, dimming a
variable only declares the type of the variable; it does not assign any
value to the variable. By default, an unassigned string variable has a null
value. So it sounds like you assigned a value to bntDossier in the form open
event and your message box in the open event displayed the value you
assigned. When the open event terminated, bntDossier reverted back to its
default value. In the form close event, you probably did not assign a value
to bntDossier so your message box displayed blank (null).

< How can I declare my variable so all the subs or functions can use it.>
Read the first sentence above.

< And is it possible to declare it so that all the other modules can use the
same variable ?>
Yes! Use the following declaration:
Public bntDossier As String
Again, bntDossier will have no value until you assign it a value and will
revert back to Null when the procedure terminates.

Steve
(e-mail address removed)
 
E

ericb

Hi Steve,

You are correct on that one. In the close form event I do not assign a value
to it. I want to use the value to reset a few things. And those things depend
on how the form was opened.

Actually it is a form that prints a report on a customer. Several command
buttons activate this form. Once open I toggle the color of the button and
note the button in btnDossier. On the close event I wanted to use btnDossier
to reset the good button.

Your explanation helped quite a bit on how the variables work. Thank you.
 
S

Steve

You coulkd do something like this at the top of the module:

Option Compare Database

Public bntDossier As String
bntDossier = "My Message"

Now you could use the variable bntDossier in a msgbox in both the open event
and close event.

Steve
 

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