Solved

Multiple forms accessing similar variables

Posted on 2001-06-06
6
131 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now