Passing variables between two forms

wjburke2

Registered User.
Local time
Today, 08:56
Joined
Jul 28, 2008
Messages
194
I am trying to pass a string variable between two forms. I use the DoCmd.OpenForm command to call the second Form. I tried declaring "Public PassAnswer as string" in both the Calling and the receiving forms (Not both at the same time) and still can not reference the value set by the called form in the calling form. Any ideas why? I am sure I did this in VB class. Nor working in Access 2000
 
Did that

I use the OpenArgs to pass text to display in the second screen. The second screen populates another variable PassAnswer I need to reference in the first screen.
 
As far as I'm aware you can't declare variables (public or not) in any form's module and reference it in another form's module. You'll have to declare the variable (PassAnswer), in a standard module unrelated to any object and then set its value in the first / second form. Hope this helps.
 
If a variable is declared publicly in the declarations section of a form's module then it is certainly accessible externally if properly referenced.
e.g.
Forms!FormName.PublicVariableName
(about as you'd expect really).

The question is usually - do you want it there, or would it indeed be more appropriate as a central object. (i.e. is it really form specific? :-)
 
Final Solution

Final Solution, I had a “Module1” I created early in the development. Actually, I am not sure how it ended up in my project. LOL, I placed the public declaration in the general section of module1. I assume this works because Modules are loaded when the application starts up. Actually I do like the idea of using a fully qualified variable Forms!FormName.PublicVariableName and may look into that.
Thanks everyone.

Module1:
Option Compare Database
Public UpdAnswer as String

Answer form:
Set in the Update option screen
UpdAnswer = "Add"

Product Form: the calling form
I now access the variable in any screen
Select Case UpdAnswer
Case "Add"
 
Last edited:
Modules are loaded as required.
i.e. when you call a procedure or variable from any given module then that module is loaded into memory.
Access used to load all modules into memory at startup - but this was deliberately moved away from.

Global (public) variables have their own issues. Some are a fan - many not.
They're commonly used though.
(They're handy for a quick fix anyway lol).
 

Users who are viewing this thread

Back
Top Bottom