?
Solved

Multiple forms accessing similar variables

Posted on 2001-06-06
6
Medium Priority
?
138 Views
Last Modified: 2010-05-02
I have declared the same variable on several forms. Will the variable on one open form be affected by variables of the same name and type on another open form? How do I avoid this?
0
Comment
Question by:eszSEARCA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Accepted Solution

by:
vindevogel earned 80 total points
ID: 6160333
No, no problem at all ....
The only problem can occur when you declare them global in a module.

Even if you declare them public on a form, they will not be accessible without giving the form too (like Form1.Variable)
0
 
LVL 18

Expert Comment

by:deighton
ID: 6160367
In short form, no  - you need to refer to them in another form as formVariableOwener.VariableName to do this


If declared at form level then declare them

private MyVar as string

..they can not be referenced by another form


if you declare them as

public MyVar

then they can be referenced from another form e.g

form2.MyVar = "x"


You can't have 2 public variables of the same name in different .bas modules, but you can if they are declared in a form of if they are declared as private private.


If you are talking about variables in sub or function.. then they are only used/changed in the function, and you can repeat the variable name between functions.
0
 
LVL 2

Expert Comment

by:Microsoft
ID: 6160454
variables explernation


public
option explicit
public myvar as string

this will only let you call back the value by :-

label1=form1.myvar

private
dim myvar as string

label1=myvar
these 2 examples would not affect each other they will both store information seperate from each other

public vars become form properties

private become form vars only.

hope this helps
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:ameba
ID: 6166057
Small correction. You can have 2 public variables (or functions) of the same name in different .bas modules.
Sample:
' in Form1
Private Sub Form_Click()
    Module1.var1 = 5
    Module2.var1 = 2
    MsgBox Module1.var1
    MsgBox Module2.var1
End Sub

' in module1 and in module2
Public var1 As Integer
0
 

Author Comment

by:eszSEARCA
ID: 6175306
Thanks for the simple encouraging answer
0
 
LVL 4

Expert Comment

by:vindevogel
ID: 6176183
You're welcome.  Thanks for the points.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question