Solved

Multiple forms accessing similar variables

Posted on 2001-06-06
6
134 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

830 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