Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables 1

Status
Not open for further replies.

HebieBug

Programmer
Jan 8, 2001
354
JP
Is is possible to have a variable which will be reconised through out the whole form? Meaning you only have to Dim it once and then in all the private sub routines it will be reconised and can be used.
And also is it a possible to Dim a variable and have it throughout a number of forms within one project?
Thanx
Herbie
 
HebieBug -

Variable visibility is called "Scope".

As you've seen, variables created inside a sub or function have what is called "Local" scope. They are visible/accessable only inside the function.

The next layer of scope is to declare varibles as the module level (outside a sub/function). You can use the "Dim" keyword to do this, but a better way would be to use the "Public" and "Private" keywords, as they make it clear as to which is which. It also depends on the type of module as to which (public/private) you can use.

In a class module, public and private are OK. Public variables will be visible outside the class when accessed as an ActiveX property of a class variable. Private variables are only visible inside the class.

In a .bas module, public variables are visible throughout the project (no ActiveX requried). Private variables are only visible in the .bas module.

In a form module, public variables are only visible outside the form when prefixed by a form variable. Private variables are only visible inside the form.

The best thing for you to experience this for yourself is to create a small project -- one form, one .bas module, and one class. In each, create public and private variables. Then run it and see which variables are visible in which situation.

Chip H.

 
Tried it and it worked fine.
Have now put it into a project that I'm working on and for some odd reason it comes up with an error message of
function or interface marked as restricted, or the function uses an Automation type not supported in Visual basic.
The variables are sitting in .bas as public
the error comes up when a button is pushed on a form with the variable highlighted.
This project has a dataenviroment and the variable is used in a with statement that is working with the dataenviroment.
So to check created a variable in the that very routine and then when the dataenviroment is closed tried moving that data from the local scope to the public scope still same error message
Any ideas
 
Sorry, I've never used the dataenvironment stuff.

But it sounds like you're referencing an object variable that doesn't permit you to instantiate it.

Hmmmm. This sounds familiar. I seem to recall a post about a year ago with a similar problem. But I don't remember anything you could use to search on to find it.

Maybe someone else has a better memory than me and can assist you.

Chip H.
 
I went through the past threads and it seems to point to a dll or operating system problem.
Only thing is it works if I try it in a new project so it seems to have something to do with the data enviroment.
Anyone have any ideas?
 
Sorry, I don't have any experience with VB6. But, in VB4's on-line help, I thought it mentioned something about useing &quot;DIM myObjectName AS Object&quot; in order to reference it's properties and methods in the way that you are saying in a WITH <name> structure.

Again, sorry, if this is of no help, but it may give someone else a start.

--MiggyD
 
Actually, I just remembered something that may somewhat help you.

If you're creating your own objects that have properties/methods, you'll have to run VB (again--while you have the first program running) and in your second program (if you have one) have it call the object so that Windows will register it as a DLL.

That's the best I can offer.

--MiggyD
 
It's an answer and a little bit of a work around
In the first form declare the variabes as public
option explicit
public test as variable

Then on the second form when you want to use that variable say we are putting it into a text box
Txttest = form1.test

All fixed and finished
Thanks for everyones time and help



 
if the Form is open (Form1) then this works,but what as the Form1 = unloaded !!! then Txttest = form1.test is Nothing.

So make a Global declaration on a Module (.bas file)
Global strText As String

strTexe = test

No we unloaded that Form and in the Form1 : Txttest = strText Eric De Decker
vbg.be@vbgroup.nl

License And Copy Protection AxtiveX.

Download Demo version on my Site:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top