Solved

Multiple forms accessing similar variables

Posted on 2001-06-06
6
133 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
6 Comments
 
LVL 4

Accepted Solution

by:
vindevogel earned 20 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

803 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