One-time variable definition

  • Thread starter Thread starter BruceM
  • Start date Start date
B

BruceM

I am having trouble sorting out how to resolve this situation, although I am
pretty sure it is not complex. I need to use a particular field's contents
in a number of text strings in various events. I will call the field
ProblemNo. It is a text field. At each event in which it needs to be added
to a text string I can have:
Dim strProbNo as String
strProbNo = Me.ProblemNo

However, I would rather declare it once in the Declarations section or in a
separate module so that I can just use it without having to declare it in
each procedure.
 
Bruce

If you are saying you will ONLY use it in relation to a particular form, I
believe you could add a form-level variable by DIM'ing it above all (event)
procedure's code in the form's module. I know this works for general code
modules.

--
Regards

Jeff Boyce
Microsoft Office/Access MVP


Microsoft IT Academy Program Mentor
http://microsoftitacademy.com/

Microsoft Registered Partner
https://partner.microsoft.com/
 
That's what I thought, but I get a compile error about an invalid outside
procedure if I use:
Public strProbNo as String
strProbNo = Me.ProblemNo

The "Me" is highlighted. I have also tried Me!ProblemNo and
Forms!MyForm!ProblemNo, with the same sort of result. This happens whether
the code is in the form's module or a general code module.
 
Bruce,

Your declaration is just fine. However, you will need to assign the value
to the variable in some event. For example, you could assign a value to the
variable in the On Current event of your form. You would normally try to
make this assignment at the first opportunity of having the value available.

By having the variable defined as a public variable at the form level, it
will be avaiable and maintain it's value as long as the form is loaded or
until a new value is assigned to it.

--
HTH

Mr B
email if needed to:
draccess at askdoctoraccess dot com
 
Thanks. My brain must have wandered off without my knowing it. Now that
you have pointed out the error I don't know why I tried to assign the value
in the Declarations section. I assigned the value in the Current event, as
you suggested (it makes the most sense to do it there, for the reason you
mention), and it works as expected.
 
Your welcom, Bruce.

Glad to help.

Mr B
email if needed to:
draccess at askdoctoraccess dot com
 
Back
Top