Declaring variables for multiple command buttons

R

RussCRM

Is it possible to declare values for variables for all the code on a
form? For instance, I have several command buttons that use the
variable stPhotoPath. I can declare it in each code, but when I want
or need to change the path, I have to make several changes. I don't
necessarily need it for multiple forms, though it wouldn't hurt if
that was an option. Can/How do I do this?
 
S

Stefan Hoffmann

hi Russ,
Is it possible to declare values for variables for all the code on a
form? For instance, I have several command buttons that use the
variable stPhotoPath. I can declare it in each code, but when I want
or need to change the path, I have to make several changes. I don't
necessarily need it for multiple forms, though it wouldn't hurt if
that was an option. Can/How do I do this?
Declare a form member variable, e.g.

--
Option Compare Database
Option Explicit

Private m_Variable As Long

Private Sub DoSomething()

MsgBox m_Variable

End Sub

Private Sub Form_Load()

m_Variable = 1234567

End Sub
 
R

RussCRM

OTOH it would be better if you put the path in a field in a
one row table so you would not have to edit a module and
redistribute your application whenever it is changed.

Pardon my ignorance, but I just want to make sure I'm understanding
you. Do you mean that I would have only one field in the table or
something else? Would I use DLookup() to get that value then?
 
S

Stefan Hoffmann

hi Russ,
Is there a way to set m_Variable = 1234567 for every Sub of the form?
Take a look at the example again. You need to initialize it. The first
event called is Form_Open(Cancel As Integer).

An other solution is to use a Property to access the variable, e.g.

--
Option Compare Database
Option Explicit

Private m_Initialized As Boolean
Private m_Variable As Long

Private Property Get Variable() As Long

If Not m_Initialized Then
m_Initialized = True
m_Variable = 12345678
End If

Variable = m_Variable

End Property

Privat Property Let Variable(Value As Long)

m_Variable = Variable

End Property

Private Sub DoSomething()

Variable = 87654321
MsgBox Variable

End Sub

Private Sub Form_Load()

MsgBox Variable

End Sub
 
M

Marshall Barton

RussCRM said:
Is it possible to declare values for variables for all the code on a
form? For instance, I have several command buttons that use the
variable stPhotoPath. I can declare it in each code, but when I want
or need to change the path, I have to make several changes. I don't
necessarily need it for multiple forms, though it wouldn't hurt if
that was an option.


Another thought, if changing the path is extremely rare, is
to declare it at the top of the form's module:
Const cstPhotoPath As String = " . . . "

OTOH it would be better if you put the path in a field in a
one row table so you would not have to edit a module and
redistribute your application whenever it is changed.
 
M

Marshall Barton

RussCRM said:
Pardon my ignorance, but I just want to make sure I'm understanding
you. Do you mean that I would have only one field in the table or
something else? Would I use DLookup() to get that value then?


The table can have many fields if you have other needs for a
single value elsewhere in your program.

OTOH, if you have a lot of this kind of thing, I would
recommend a table with two(?) fields, one for the name of
the variable (primary key) and the other for the value.
This would be kind of like a persistent collection.

Either way, you can use DLookup to retrieve a value. For a
one row table:
DLookup("varname", "table")

For a two field table:
DLookup("valuefield", "table", "namefield='varname' ")
 

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