 |
|

03-04-2004, 11:20 AM
|
 |
Senior Contributor
|
|
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
|
|
Public Variables in all forms
|
Hi everybody
If I have a form, and I declare a public var in it like:
Code:
Option explicit
Public MyOwnVar
' ... the code
' Private SUB Form_Load ()
I could use it from other place in the app, ej: MyForm.MyOwnVar = 10
but ... there is a way to declare a property that affecs to all the forms in my app?. What I don't want to do is to declare that property in each form
Thanks
(If I'm not clear, please tell me and I'll try to explain a little better)
|
|

03-04-2004, 11:26 AM
|
|
Senior Contributor
* Expert *
|
|
Join Date: Jul 2003
Posts: 1,300
|
|
|
put it in a module.
thingimijig.
|
|

03-04-2004, 11:29 AM
|
|
Junior Contributor
|
|
Join Date: Nov 2003
Location: Philadelphia, Pa. USA
Posts: 197
|
|
Quote: Originally Posted by Peperl Hi everybody
If I have a form, and I declare a public var in it like:
Code:
Option explicit
Public MyOwnVar
' ... the code
' Private SUB Form_Load ()
I could use it from other place in the app, ej: MyForm.MyOwnVar = 10
but ... there is a way to declare a property that affecs to all the forms in my app?. What I don't want to do is to declare that property in each form
Thanks
(If I'm not clear, please tell me and I'll try to explain a little better)
if you want to change a property in all forms at run time, you can use the Forms object and a For Each loop.
Check it out. You can loop through all your forms and change thier properties.
|
|

03-04-2004, 11:32 AM
|
 |
Senior Contributor
|
|
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
|
|
Well .. what I want to do is to "invent" a property to that form ...
that i could do in every place:
Code:
MyForm1.MyProperty = "1"
MyForm2.MyProperty = "2"
without to have to declare MyProperty for each form individually
|
|

03-04-2004, 11:43 AM
|
|
Retired Contributor
|
|
Join Date: Mar 2002
Posts: 1,829
|
|
I don't think you can create a property for a control.
Code:
MyForm1.MyProperty = "1"
MyForm2.MyProperty = "2"
You should create an Array for your property something like
Code:
'Module
'Say you have 7 forms
Public MyCustomFormValue(1 to 7)
'Then you can just set the value for each of the forms
'For form3
MyCustomFormValue(3) = "Whatever"
That make sense?
|
Last edited by Agent707; 03-04-2004 at 03:03 PM.
|

03-04-2004, 12:44 PM
|
 |
Regular
|
|
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
|
|
Here's how to solve that problem:
Let's say you have Form1, Form2 and Form3
In Form1 you declare a public variable as follows:
Code:
Option Explicit
Public MyOwnVar
' ... the code
' Private SUB Form_Load ()
In all other forms (Form2 and Form3) you now can access this variable like follows:
Code:
Form1.MyOwnVar = "test"
'or
msgbox(Form1.MyOwnVar)
'and so on...
Is that what you wanted to know?
Zeek
|
|

03-04-2004, 01:23 PM
|
 |
Senior Contributor
|
|
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
|
|
Quote:
In all other forms (Form2 and Form3) you now can access this variable like follows:
Form1.MyOwnVar = "test"
'or
msgbox(Form1.MyOwnVar)
|
not exactly ...
I want that all forms have the MyOwnVar without that I have to declare it in each form
that i want is that all forms have his <OwnVar>
|
|

03-04-2004, 02:32 PM
|
 |
Regular
|
|
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
|
|
|
You don't have to declare it in every form... The Variable is accessible in every form just by using Form1.MyOwnVar
Zeek
|
|

03-04-2004, 02:49 PM
|
 |
Senior Contributor
|
|
Join Date: Mar 2004
Posts: 851
|
|
|
"I want that all forms have the MyOwnVar |without that I have to declare it in each form"
I don't think that's possible, you have to declare variables..
how many forms do you have?
|
|

03-04-2004, 03:00 PM
|
 |
Regular
|
|
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
|
|
You only have to declare the variable in the first form (Form1) as public.
After that it is accessable in ANY other form of your project w/out declaring it there!
In Form2, Form3, Form4, ....
You just have to use Form1.MyOwnVar instead of just MyOwnVar in all other Forms.
Got it?
Zeek
|
|

03-04-2004, 03:06 PM
|
|
Retired Contributor
|
|
Join Date: Mar 2002
Posts: 1,829
|
|
|
But if you declare it in a Module, you don't have to use form1.MyOwnVar, you just use MyOwnVar.
However, I don't see where he's going with this anyway.
|
|

03-04-2004, 03:10 PM
|
 |
Regular
|
|
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
|
|
Maybe he doesn't want to add a module to his project just because of one single public var...
Either way will do it and I'm sure he'll understand that pretty soon....
Zeek
|
|

03-04-2004, 03:12 PM
|
|
Centurion
|
|
Join Date: Feb 2004
Location: Tasmania, Australia
Posts: 114
|
|
|
From what you say in your first post you don't want to declare the variable in any Form so declaring it in a Module seems like what you want?
|
|

03-04-2004, 03:17 PM
|
 |
Regular
|
|
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
|
|
He wrote he doesn't want to declare it in each form...
Nevermind, guess he'll make it...
Zeek
|
|

03-04-2004, 03:33 PM
|
 |
Senior Contributor
|
|
Join Date: Mar 2004
Posts: 851
|
|
|
from what I understand he wants every form to have a DIFFERENT MyOwnVar without having to WRITE a declaration in every form...
|
|

03-04-2004, 03:51 PM
|
 |
Regular
|
|
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
|
|
|
Where's the problem then? In VB it's possible to use a variable w/out having to declare it at all, as long as "Option Explicit" is not set.
Zeek
|
|

03-04-2004, 04:11 PM
|
 |
Senior Contributor
|
|
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
|
|
|
Hi!
Well .. I'll try to exaplain what I have in mind
Always I make what you said, make a public var when i want to store a value, and calling it from other places, like a year or anything
But if I have more than one form open that uses that value i have to declarate more than one public var and i thinked that it was a mess, and maybe there was a way to create a new value of each form.
So I have a new <TAG> to store values
I could make that in the TAG but how I store here more than one value i thinked that if I put a var named <Anio> for each form, i improved the code and clarity.
P.S: My idea is that a form have a different value, and Form1.Anio could be differeent to Form2.anio
|
|

03-04-2004, 04:16 PM
|
 |
Senior Contributor
|
|
Join Date: Mar 2004
Posts: 851
|
|
|
you mean two vars with the same name but with different values?
if so, then just set the var as Private and you can give a var on another Form the same name... as long as you set it to Private too.
|
|

03-04-2004, 04:18 PM
|
 |
Regular
|
|
Join Date: Feb 2004
Location: Berlin, Germany
Posts: 99
|
|
I see! If you don't need the variables in any other forms, simply declare them as private.
You can also declare a public array in a module, like this:
Code:
Public Anio(5) As Long
After that you could use Anio(0) for first form, Anio(1) for second form and so on.
If you don't want to use an array but need to access the variables though all forms, you have to declare the variables in every form.
Zeek
|
|

03-04-2004, 04:20 PM
|
 |
Senior Contributor
|
|
Join Date: Apr 2003
Location: Cartagena, Spain
Posts: 1,371
|
|
No no ... (ugh ... i have to speak really bad english  ...
what I said is, at the same way that every form as a property named TAG where you can put a value, create other property named like I want
This is not the problem, the problem is that I want that only one time and all the users have this property.
Could you think that it can be with a class module?
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|