Declaring Variables Public

  • Thread starter Thread starter ob3ron02
  • Start date Start date
O

ob3ron02

Is this the right syntax for declaring a variable to be public? This i
what I found in the VB Help File.
Public Statement Example
This example uses the Public statement at the module level (Genera
section) of a standard module to explicitly declare variables a
public; that is, they are available to all procedures in all modules i
all applications unless Option Private Module is in effect.

Public Number As Integer ' Public Integer variable. But executing the following code code gives me the error "Invali
Attribute in Sub or Function" occurring at the "Public mystr..." line.
______________________
Sub mysub1()
Public mystr As String
mystr = "hello world"
End Sub
______________________

Also, I'm guessing mysub1() needs to be executed just once before thos
strings are set in memory?

Thanks,

To
 
You declare the variable at the top of the module (a general module),
outside any procedure

Public mystr As String

Sub mysub1()
mystr = "hello world"
End Sub

You would need to execute some code to give the variable a value.

If the value will not change, you could declare a constant:

Public Const MyConst As String = "ABCD"


Sub Testit()
MsgBox MyConst
End Sub


--
Regards,
Tom Ogilvy

ob3ron02 said:
Is this the right syntax for declaring a variable to be public? This is
what I found in the VB Help File.
following code code gives me the error "Invalid
 
Back
Top