Declare public variable and assign a default value

aman

Registered User.
Local time
Today, 11:57
Joined
Oct 16, 2008
Messages
1,251
Hi ALl
I want to define a public variable and i am using the following code but it gives me Compile error Invalid outside procedure.
Code:
option explicit
public ABC1 AS VARIABLE
ABC1="FALSE"
Any help would be much appreciated.
 
You cannot declare variables outside any method/sub/function.. Unless those are constant declaration.. You need to create a Sub to declare/initialize the variables by calling them in the first instance maybe by using an AutoExec macro or OnLoad of a Form that will be opened at the very start of the application.
 
.... and VARIABLE is not a type of data :)
 
Please can you give me an example how can I declare Public variable and assign a default value to it.

Thanks
 
Copy this code into a Common module (not a Form module).

Code:
Option Compare Database
Option Explicit

Public welcomeStr As String, welcomeDate As Date

Public Function initPubVar()
    welcomeStr = "Hello World"
    welcomeDate = Date()
End Function
Then save it by giving a name (say we call the module declareMod). Then compile it first.

Next, create a AutoExec Macro, ask it to RunCode by passing initPubVar() as the argument..

Close and reopen the database.. It should now be declared and assigned..

To test this, open the VBA editor (Alt+F11) then open the immediate window (Ctrl+G) and type..
Code:
? welcomeStr & ". The Date Today is - " & welcomeDate
You should het the output..
Code:
Hello World. The Date Today is - 07/11/2013
Then change/add more variables according to your design..
 
Take a look HERE.
I am not sure that all this data types are allowed by Access VBA.
But, if you use a data type that Access do not allow then Access will pop up an error message.

Examples:
Dim (or Public) VarName As Boolean
Dim (or Public) VarName As Long
Dim (or Public) VarName As Integer
Dim (or Public) VarName As Object
Dim (or Public) VarName As Variant (or, simple, Dim (or Public) VarName)

The variables can't have a "default" value.
You should assign a value in a statement.
 
Copy this code into a Common module (not a Form module).

Code:
Option Compare Database
Option Explicit

Public welcomeStr As String, welcomeDate As Date

Public Function initPubVar()
    welcomeStr = "Hello World"
    welcomeDate = Date()
End Function
Then save it by giving a name (say we call the module declareMod). Then compile it first.

Next, create a AutoExec Macro, ask it to RunCode by passing initPubVar() as the argument..

Close and reopen the database.. It should now be declared and assigned..

To test this, open the VBA editor (Alt+F11) then open the immediate window (Ctrl+G) and type..
Code:
? welcomeStr & ". The Date Today is - " & welcomeDate
You should het the output..
Code:
Hello World. The Date Today is - 07/11/2013
Then change/add more variables according to your design..
Thank you this helped me too! It taught me three new things. Not bad for an hour at 7AM!!
 

Users who are viewing this thread

Back
Top Bottom