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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Variable from closed Form

Status
Not open for further replies.

mystuff

Programmer
Apr 30, 2004
86
US
Hi,

I have a form that opens another form in dialog mode. The user selects the record they want by pressing a button on the 2nd form. I save the [ID] in a variable and then close the 2nd form. Then control goes back to the first form.

Is there a way to use the variable that I saved?

Thanks.
 
Only if the variable is on the current form. In other words, you would need to pass the data from the form the user will close, to the form that called that form in the first place.

Another way to do it would be to declare the variable in a module and then you can access the variable anytime you want it from anywhere in your application.

You need to be careful with that however. Make sure you empty the variable when you are finished with it and also make sure you know when you have finished with it so you don't overwrite data you need.

Hope this helps.
 
What about using a Public variable that can be passed between various objects? Like this:
Code:
Option Compare Database
Option Explicit
Public strMyStringVariable As String

And just store it in a Module (not form level coding, but a module that would be listed under "Modules" in the VBA window, and in the Access (F11) window.

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Public variables are usually not a good idea, also, if you have the public variable on the form that closes, it goes away.
 
OK, thanks ... I was able to declare the variable as a global variable in a Public Module.

But I still have one more problem: The variable name I am using is gstrID. When I try to use the value with ApplyFilter, the system thinks the value is a variable name and it prompts me with "Enter Parameter Value".


strFilter = "[Project Budget Number] = " & gstrId
DoCmd.ApplyFilter , strFilter

The value of gstrID is "PB0002"
But the system thinks this is a field name and prompts me to Enter Parameter Value PB0002. Then, if I type in PB0002 as the value, the ApplyFilter doesn't work anyway.

In the Debug immediate window, I can see that the strFilter variable is "[Project Budget Number] = PB0002".

I hope this is not too confusing. Any thoughts?


 
Hmm, just a guess, what about changing your above to this:
Code:
strFilter = "[Project Budget Number] = '" & gstrId & "'"

just a guess... I've not messed with filters yet, that I know of..


Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Hi...
Try converting the variable to a function. Place this in the module where you declared the variable Global

Public Function FngstrId()
FndstrId = gstrId
end function

Use the Function name in your applyfilter

strFilter = "Project Budget Number = " & FngstrId()DoCmd.ApplyFilter , strFilter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top