Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing a Public Variable in a Function

Status
Not open for further replies.

FarmboyEsq

Programmer
Apr 2, 2007
17
0
0
US
I must be stupid!!! Basically I want to use a function as in the criteria for a query. I've found a few examples on the net and believe I'm very close.

The Function is

Public Function GetPkgName() As String
GetPkgName = strPkgNamePublic
End Function

My query references GetPkgName() in the criteria and if I force a value for this inside the function it works fine. My problem is, how do I set the value, before I open the query.

I've attempted this in the VBA procedure behind the form:

(in the general declarations)
Public strPkgNamePublic As String

(In a private sub click event)
strPkgNamePublic = Me!fldANSPkgName
strFunc = GetPkgName()

The MSGBOX displays I put in the function show no value for strPkgName Public. Therefore, it has nothing to work with.

Arg! Help! Thanks!

Stg
 
If I understand right you may have a scoping issues. A public variables declared in a form exists only in during the life of a form. A public variable declared in a standard module exists through the application. So if you set the value and then close the form the variable goes out of scope. Also a function declared in a form's module is not accessible throughout the program. I would put both the function and the variable in a standard module not a forms module. Not certain if that is your issue because I am not certain what is open and the order of operation. However try putting these in a standard module and then try this in your click event to be sure that there is a value in fldANSPkgName and that the function works.

strPkgNamePublic = Me!fldANSPkgName
msgbox me!fldANSPkgName
Msgbox GetPkgName()
 
That seems to be the problem. I've got it working now.

Thanks!

Stg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top