TempVars

  • Thread starter SurveyorinVA via AccessMonster.com
  • Start date
S

SurveyorinVA via AccessMonster.com

Hello-

I am starting to work with Access 07 SP1 on Vista and trying to figure out
the right way to set the TempVars in an On_Click event. I followed what the
current help says where:

Dim myWorld As String
myWorld = "Hello"

TempVars.Add myText, myWorld

But I get an error, "You must specify a name to set or remove a temporary
variable." I thought I did with myText as the variable and myWorld as the
value.

If any one can help me that would be greatly appreciated.

Thanks,
CF
 
G

Graham Mandeno

Hi CF

The first argument of the Add method is a string, so you should enclose
"myText" in quotes.

(Unless you have a string variable named "myText" which contains the
variable name, but this looks not to be the case)

This raises another matter. It appears that you are implicitly declaring
variables, which means they are created the first time they are mentioned.
This is very bad practice for several reasons, not the least of which is
that if you make a small typo then you can inadvertently introduce a
completely new variable. This makes your code extremely difficult to
debug!!

To avoid this, EVERY module should start with the line:
Option Explicit

There is also a setting in the VBA options to "Require Variable
Declaration", which automatically inserts this line into any *new* module
you create. (Note that you have to do it manually in existing modules).
 
T

tobesurveyor via AccessMonster.com

Hi Graham-

I appreciate the info. I have in the past been notorious for no defining my
variables, and will defentitly start now.

I still am not sure if I am doing this correctly. If I wanted to use this
TempVars.Add(Name,Value)
If I am correct the Name is going to be a String variable that can be called
from any VBA code, query, etc... until I Remove it with the TempVars.
RemoveAll.


So in the example below, if I want to use the variable MyText, and load the
string data from myWorld into it, I would declare myWorld as a String and
myText as a String, or use:
TempVars.Add("myText",myWorld)

once I declare it, I would call upon it from anywhere (until cleared) by
using:
TempVar![myText]

When I do this, I get an error saying TempVar![myText] = <object required>.

Any further information you have would be greatly appreciated.
Thank you again for the information,
CF

Graham said:
Hi CF

The first argument of the Add method is a string, so you should enclose
"myText" in quotes.

(Unless you have a string variable named "myText" which contains the
variable name, but this looks not to be the case)

This raises another matter. It appears that you are implicitly declaring
variables, which means they are created the first time they are mentioned.
This is very bad practice for several reasons, not the least of which is
that if you make a small typo then you can inadvertently introduce a
completely new variable. This makes your code extremely difficult to
debug!!

To avoid this, EVERY module should start with the line:
Option Explicit

There is also a setting in the VBA options to "Require Variable
Declaration", which automatically inserts this line into any *new* module
you create. (Note that you have to do it manually in existing modules).
[quoted text clipped - 16 lines]
Thanks,
CF
 
G

Graham Mandeno

Hi CF

Sorry, I've been away for a few days.

The name of the collection is TempVars, not TempVar, so you need to refer to
it as:
TempVars![myText]
or
TempVars("myText")

--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

tobesurveyor via AccessMonster.com said:
Hi Graham-

I appreciate the info. I have in the past been notorious for no defining
my
variables, and will defentitly start now.

I still am not sure if I am doing this correctly. If I wanted to use this
TempVars.Add(Name,Value)
If I am correct the Name is going to be a String variable that can be
called
from any VBA code, query, etc... until I Remove it with the TempVars.
RemoveAll.


So in the example below, if I want to use the variable MyText, and load
the
string data from myWorld into it, I would declare myWorld as a String and
myText as a String, or use:
TempVars.Add("myText",myWorld)

once I declare it, I would call upon it from anywhere (until cleared) by
using:
TempVar![myText]

When I do this, I get an error saying TempVar![myText] = <object
required>.

Any further information you have would be greatly appreciated.
Thank you again for the information,
CF

Graham said:
Hi CF

The first argument of the Add method is a string, so you should enclose
"myText" in quotes.

(Unless you have a string variable named "myText" which contains the
variable name, but this looks not to be the case)

This raises another matter. It appears that you are implicitly declaring
variables, which means they are created the first time they are mentioned.
This is very bad practice for several reasons, not the least of which is
that if you make a small typo then you can inadvertently introduce a
completely new variable. This makes your code extremely difficult to
debug!!

To avoid this, EVERY module should start with the line:
Option Explicit

There is also a setting in the VBA options to "Require Variable
Declaration", which automatically inserts this line into any *new* module
you create. (Note that you have to do it manually in existing modules).
[quoted text clipped - 16 lines]
Thanks,
CF
 

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