Global variable

cmac2210

Registered User.
Local time
Today, 13:34
Joined
Mar 26, 2009
Messages
12
I need to declare a global variable so it can be used by several forms. The forms will open another form, tied to the record. I was using something like the following to put the data into the sql query, but now that the project has grown and additional functions need to be added, this form needs to be able to look to different forms for the criteria.

Originally
Code:
forms!frmnewcourt!combo22

Can this be done with a global variable? If so how... I have no idea how to do this. Totally new to this.
 
Don't know enough about what you're doing to say there's a better way, but there may be. In any case, you can use a global variable for a query criteria, but you have to do it indirectly. You can't refer directly to the variable in the query. You have to create a public function that returns the value of the variable, and use that in your criteria.
 
This is what I am unsure of. I have never built a public function, so I am a bit unsure of how to proceed. Would it be something as easy as this?

Code:
Option Compare Database
Public Function acc()
acc As String
End Function

If the above is correct, how would I get this to work accros the different forms. Example. Each form has a text box "accused" that basically identifies a table to use in the query on frmreplace. Like I said, if it were just 2 forms, not a problem just identifying the textbox as the query criteria to open frmreplace. So how would I call the above, if its even correct. Thanks for the help.
 
I'm still not clear on what you're trying to accomplish. If this textbox identifies what table to use, I would suspect a design problem. You could not use the variable or function in a query to determine the table, only a criteria.

In any case, the correct method would be to declare the variable at the top of a standard module, then inside your function:

acc = VariableName
 
Close.

You need to do it like:

Code:
Option Compare Database
Option Explicit 

Public MyPublicVariable As String

Public Function fPublicVariable() As String

fPublicVariable = MyPublicVariable

End Function
 
Here is a demo with documentation on doing exactly what you want.

Demo


David
 

Users who are viewing this thread

Back
Top Bottom