Declaring variables for multiple command buttons

  • Thread starter Thread starter RussCRM
  • Start date Start date
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?
 
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
 
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?
 
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
 
Back
Top