Public Variables

johny678

Registered User.
Local time
Today, 16:40
Joined
Jun 8, 2005
Messages
28
I want to use a public variable called myFilter which takes values from a form called searchForm and then use it in another form to produce a report with the same filter. The problem is that I cannot make my variable work as public although I declare it in the General level. Any tips or even examples would be appreciated!!!
Thanks.
 
In which module are you declaring it public? You can't declare it in a Class module - i.e. the module that accompanies a form or report.
 
I think what you are referring to is generally called "a global variable" these are frowned upon in programming, as they can lead to serious bugs in your code.

Most things that you would imagine doing with a global variable can be done with a public function.

I would suggest that you post the code, and I'm sure someone will be able to make a suggestion.
 
You could use a global string.

Code:
Option Compare Database
Option Explicit
    
Global gsMyFilter As String
Code:
Public Sub Test1()
 
     gsMyFilter = "ABC123" 'set the gsMyFilter value
  
End Sub
Code:
Public Sub ConfirmTest1()
 
     MsgBox gsMyFilter 'simple test
 
End Sub
 
Where should I declare it then if not in the General level of a class of a form?
Does the word Global works? Cause it gives me a compile error?
 
Create a new public module. Click on the Modules tab and then click the New button. Do not give (save) your module the same name as a function or sub. Copying and pasting my code above into a public module [not a Class form or a Class report module] will work as-is for a quick test.
 
Done it! Thanks! So I will have a module just to declare my public variables (when need ones)??? Great!!!
 

Users who are viewing this thread

Back
Top Bottom