declaring global variables

krc777

Registered User.
Local time
Today, 05:32
Joined
Nov 29, 2005
Messages
70
I have the following code:

Option Compare Database
Global theDate As String
Public Sub GetDate()
theDate = InputBox("Enter Volume Date YYYYMM")
mymonth = Right(theDate, 2)

then this sub calls another module sub. When that module is opened the global variable theDAte is not recognized. I'm getting an error message that says an ambiguous name is detected.

Help!
 
I believe you want

Public TheDate as string

at the top of your Module. Do you have another variable named TheDate in a function?
 
1. You want the Public variable (formerly known as Global in previous VB incarnations) to be in the declarations section of a standard module (not a form module).

2. While using the keyword Global can work, the current versions of VB and VBA use PUBLIC as the keyword.

3. It sounds like you have dimmed the variable in another place and not just in the module. If so, Access will not know which is which since one is within a lower scope and the one in the module would be in a larger scope. To use the same name, the scopes must not overlap. Do a FIND within the code window to find all instances of that name in the Project. Eliminate the one that you have elsewhere and leave the one in the module. You can use it elsewhere, just not dim it in multiple places. The variable can be dimmed (made public) in one module but used throughout the project.
 
I don't think global is right

just dim thedate as string

where is the code going to next? - which line is the debugger stopping on.
 
I don't have anything else named TheDate. I made the change you suggested from Global to Public but got the same error. It doesn't seem to hang onto the value once I leave the first module and go to the next.
 
That was it! I found it dimmed in another module. Thanks so much!
 
Are you sure you are doing this in a standard module and not a form module? You can't do it in a form's module (you can make it public in a form's module, but it will only be available to all events within that form module).

You also need to make sure it is in the DECLARATIONS section of the standard module. Plus, you can't use the keyword DIM. (just pointing out stuff that you might already know, but is important).


EDITED after seeing your other post - NEVER MIND since you found it :D
 

Users who are viewing this thread

Back
Top Bottom