Declare a variable for all procedures

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I declare a variable that will be used by more than one procedure?

I have done the following:
-----------------------------------------------
Option Compare Database
Option Explicit
Dim varStartDate As Date ‘ My variables
Dim varEndDate As Date ‘

Private Sub cmdCancel_Click()
…
End Sub

Private Sub
…
End Sub
-------------------------------------------------
But I see some people do this:
------------------------------------------------
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
 
Seth said:
How do I declare a variable that will be used by more than one
procedure?

I have done the following:
-----------------------------------------------
Option Compare Database
Option Explicit
Dim varStartDate As Date ' My variables
Dim varEndDate As Date '

Private Sub cmdCancel_Click()
.
End Sub

Private Sub
.
End Sub
-------------------------------------------------
But I see some people do this:
------------------------------------------------
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type

The two examples are not related. The "Private Type ... End Type"
structure is declaring a user-defined data type, not a variable.
Presumably, somewhere in the code, there's an actual declaration for a
variable of type BROWSEINFO, along these lines:

Dim bf As BROWSEINFO

Or maybe a function of that type

Function Foo(bar) As BROWSEINFO

Anyway, there's nothing wrong with the module-level variable
declarations oin your first example. Those variables will be known to
all procedures inside the module, but not outside it.
 
Back
Top