Solved Pass variables between forms (1 Viewer)

yamus

Member
Local time
Today, 19:10
Joined
Aug 12, 2020
Messages
81
Hi
I want to know how to pass variables between forms
I have "formSearchEquip" which within i have a variable "idEquip". I want to pass this variable to another form "formEditEquip"
Thanks in Advance
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:10
Joined
Feb 19, 2002
Messages
42,971
Sounds like you are building a pathological connection between these forms.

When formA opens formB as happens when you display search results on formA and want to open formB to edit a single record, you use the WHERE argument of the OpenForm Method to bring up the selected record.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:10
Joined
Oct 29, 2018
Messages
21,358
Hi. Perhaps you could also consider using TempVars.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 18:10
Joined
Jul 9, 2003
Messages
16,244
This video demonstrates passing information from a textbox on one Form to a textbox on another Form , however you could adapt it to pass a variable. I wouldn't actually use a variable I would use a custom property.

Open One form From Another And Pass Info - Nifty Access

 

yamus

Member
Local time
Today, 19:10
Joined
Aug 12, 2020
Messages
81
Hi. Perhaps you could also consider using TempVars.
Hi
Could you explain in detail please
How do i use the tempVars ?
Are they a special data type ?
I would appreciate if there is an example
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:10
Joined
Jan 20, 2009
Messages
12,849
How do i use the tempVars ?
Are they a special data type ?

Using TempVars for something like you describe is like using a sledge hammer to crack a peanut.

TempVars is a special collection that has scope across the whole project and database allowing them to be set in VBA and used in queries. They persist until the application is closed, even after a code reset. (This begs the question why they have Temp in their name at all since they are the least temporary of any variable.)
 

yamus

Member
Local time
Today, 19:10
Joined
Aug 12, 2020
Messages
81
Using TempVars for something like you describe is like using a sledge hammer to crack a peanut.

TempVars is a special collection that has scope across the whole project and database allowing them to be set in VBA and used in queries. They persist until the application is closed, even after a code reset. (This begs the question why they have Temp in their name at all since they are the least temporary of any variable.)
Hi
Then what should i do ?
How can i pass variables between forms?
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:10
Joined
Jan 20, 2009
Messages
12,849
A Public variable on a form is available as a member of the object so, provided you declare it Public, any form can simply refer to the variable on your form as:
Code:
formSearchEquip.EquipId

This way the variable is available with the value you gave it without any further code such as would be required when using a TempVar. Plus you won't have a TempVar sitting around with an obsolete value to cause problems if you forget to set it somewhere.

As UncleGizmo suggested, there are advantages to having the variable as Private and exposing it as a custom Property. For example it can be made available as Read Only which can't be done with a Public variable.

As a general principle of programming, variables are normally given only the scope they need, which is another reason why using a TempVar would not be recommended, though of course it would work.
 

Dreamweaver

Well-known member
Local time
Today, 18:10
Joined
Nov 28, 2005
Messages
2,466
I use a number of different methods Except tempVar

I just added A calculator And A Calendar which shows one method, Useing A Global Varible for a control so the selected value is returned to the calling form.
Most os my search screens use anoter method
 

yamus

Member
Local time
Today, 19:10
Joined
Aug 12, 2020
Messages
81
Thank you everybody
Appearently, i forgot the fact the fact a variable declared as Dim is by default private. So, i declared the variable as public and it worked fine
Thanks again
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:10
Joined
Feb 19, 2002
Messages
42,971
The reason that TempVars were introduced is because all other variables LOOSE THEIR VALUE under certain conditions. Do NOT use this method. Do what I suggested if all you need to do is to open a form to a specific record. Otherwise, create TempVars and populate them. Another option is to reference the first form, which should remain open but hidden until the popup form closes.

Me.somefield = Forms!frmOriginal!txtSomefield

But, you might want to do some reading on Coupling and Cohesion before you embark on the design you seem to have in mind.

To summarize -
Objects/Procedures should be loosely coupled meaning that the fewer arguments passed between them the better
Objects/Procedures should be tightly coupled meaning that the internal workings should do only those things related to a single function. i.e. opening the employee form should not automatically reboot the server.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:10
Joined
Jan 20, 2009
Messages
12,849
The reason that TempVars were introduced is because all other variables LOOSE THEIR VALUE under certain conditions.

Variables only lose their value only when the VBA project is reset. Unfortunately there is a persistent myth that they lose their values in an unhandled exception.

I think their introduction was also about making VBA variables available in queries. Of course this can also be done with a function. I consider TempVars to be "a solution looking for a problem" and have never used them.
 

Isaac

Lifelong Learner
Local time
Today, 11:10
Joined
Mar 14, 2017
Messages
8,738
Variables only lose their value only when the VBA project is reset. Unfortunately there is a persistent myth that they lose their values in an unhandled exception.

I think their introduction was also about making VBA variables available in queries. Of course this can also be done with a function. I consider TempVars to be "a solution looking for a problem" and have never used them.
I found this response interesting. Throughout reading this thread, I was thinking to myself "I never use TempVars, but I wonder if I should admit that" (ha ha).
Now I feel more comfortable doing so. I also felt that way about a solution looking for a problem. To this day I still use global or public variables when necessary, and haven't really noticed any downside of doing so. I've never been sold on why TempVar is better, and never felt I had a need for it, so have never used them.

As for unhandled errors, I never really knew if that was true or not, but didn't care too much since error handling is a top priority for me. Good to know.

One of the things that turned me off about them is that they are advertised as too much of a "You can do anything you want with this thing, without understanding anything about anything". Any time I see something like that, I hesitate to use it, because I feel it doesn't do me any favors, but rather encourages me not to learn the necessary programming concepts that are inherently required (more so) to use the other stuff. This last bit is very personal, though, and admittedly might "just be me".

Edit: @Galaxiom As I'd like to master this question once and for all, can you check out the attached and let me know why the global var appears to lose its value after the unhandled error? I think the answer is, "because when I pressed END to the unhandled error, that reset the VBA project", but then my question would be, practically speaking in real life, isn't that would almost every user would do, if Continue wasn't an option?
 

Attachments

  • Testing 20200909 Variables.accdb
    424 KB · Views: 155
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:10
Joined
Jan 20, 2009
Messages
12,849
It isn't the unhandled exception per se that causes it to lose the value. Pressing End in the break dialog will reset the project as will pressing Reset (the red square button in the editor) if you go through to the Debug.

Click debug then drag the execution cursor (yellow arrow in left margin) down past the line with the break and press the Run button you will see the variable still has its value.

This saves a lot of mucking around setting up the state of the VBA variables again when debugging. You can simply fix the problem that caused the break and continue from where you were.

BTW If I get a problem in a function that is going to be called multiple times I edit the code while in Break mode so that it skips the problem or the entire content of the function and press Run. Some developers are wary that this would do something strange because of the compiled code being run but I have never experienced any problems. I don't really know what happens in the background but I assume the edit causes the code to recompile before continuing.

I've edited functions that are being called from a query while in Break mode and the entire query results update automatically to reflect the new code, including those records that have already done the call.
 

Isaac

Lifelong Learner
Local time
Today, 11:10
Joined
Mar 14, 2017
Messages
8,738
It isn't the unhandled exception per se that causes it to lose the value. Pressing End in the break dialog will reset the project as will pressing Reset (the red square button in the editor) if you go through to the Debug.

Click debug then drag the execution cursor (yellow arrow in left margin) down past the line with the break and press the Run button you will see the variable still has its value.

This saves a lot of mucking around setting up the state of the VBA variables again when debugging. You can simply fix the problem that caused the break and continue from where you were.

BTW If I get a problem in a function that is going to be called multiple times I edit the code while in Break mode so that it skips the problem or the entire content of the function and press Run. Some developers are wary that this would do something strange because of the compiled code being run but I have never experienced any problems. I don't really know what happens in the background but I assume the edit causes the code to recompile before continuing.

I've edited functions that are being called from a query while in Break mode and the entire query results update automatically to reflect the new code, including those records that have already done the call.
I see what you mean. Thanks for clarifying.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:10
Joined
Feb 19, 2002
Messages
42,971
I never switched to TempVars either. My solution developed when I was a mere child was, when I have an app that I need to pass arguments around was to use an unbound form and make the variables controls on the form. The upside of this method is that while testing, you can set the form to visible so you can watch the variables as they change. Or, even manipulate them manually to test various logic paths.

I would have liked TempVars better if:
1. they all had to be defined in a single place forcing people to be neat about their use.
2. we were not required to use .value to reference them
 

Users who are viewing this thread

Top Bottom