473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

VB .NET - access variable in different form

144 100+
hi,

how to access a variable in a form from a different form?

i have a DataSet variable in form1 which needs to be accessed by form2. when i type the exact name as the DataSet variable in form1, the IDE tells me that

Expand|Select|Wrap|Line Numbers
  1. Name 'DataSet1' is not declared

thank you
Oct 4 '08 #1
7 5498
kenobewan
4,871 Expert 4TB
Not sure why you need to access dataset from form 1? How about updating the data in form 1 then querying database again in form 2?
Oct 4 '08 #2
jg007
283 100+
how have you declared the variable ?, is it public or private if it is public you can use ' form2.variablename ' and should be able to read it okay

( asuming you are just calling a variable dataset )
Oct 4 '08 #3
Curtis Rutland
3,256 Expert 2GB
This isn't necessarily the best option available (see kenobewan''s post) but it is possible.

Two things need to happen for this to work properly;
  • You need to declare your DataSet as Public
  • You need to pass a reference of the instance of form1 to the second form to use it though.

The second part is a bit complicated. You need to modify Form2's constructor. If you don't know what that is, read this. The constructor is the Sub New() that your program may or may not contain.

You need to add a global variable to the form like this:
Expand|Select|Wrap|Line Numbers
  1. Dim f1Ref as Form1
and you need your constructor to take a parameter, like this:
Expand|Select|Wrap|Line Numbers
  1. Public Sub New(f1 as Form1)
  2.     f1Ref = f1
  3. End Sub
Now, when you want to show Form2, you need to do it like this
Note that this is the correct way. Just saying Form2.Show() is wrong.
Expand|Select|Wrap|Line Numbers
  1. Dim f2 as New Form2(me)
  2. f2.Show()
now you can get to Form1's DataSet like this (I'm assuming you called it DataSet1, so just replace that with whatever you need)
Expand|Select|Wrap|Line Numbers
  1. Dim ds as DataSet
  2. ds = f1Ref.DataSet1
Oct 4 '08 #4
thesti
144 100+
hi,

thanks for the replies, it works!

the DataSet is declared as a Friend, and yes i can access it with Form1.DataSet1.

well, i create the dataset in form1 as it holds data entered by user and shows them in a datagridview, in form2 i want to show the data in the dataset (table 0) as a report using crystal report.

but now, i'm getting an error of "the report has no tables".

how to bind a dataset to a crystal report? should i use an XML schema?

thank you very much.
Oct 4 '08 #5
Curtis Rutland
3,256 Expert 2GB
Well, I may be wrong, but I believe that you are getting that error because you aren't actually referencing the one that you instantiated in Form1.

Form1.DataSet1 isn't the correct way to reference that dataset. What that actually means is to try to find a Shared (static) member called DataSet1. I'm not sure how VB.NET handles trying to reference non-static members statically, but in C# you would get a compiler error.

You need to try it the way I posted previously. That way, you are actually referencing the instance that was created when you ran the program.
Oct 4 '08 #6
thesti
144 100+
hi, insertAlias

thanks for the help.

yes, your way is logically right, since i'm referencing 'Form1' which is the class name not the object's name. i must be trying to reference a static property. but it just works with 'Form1.DataSet1'

here's two thins that i did

first, i add a new item > DataSet named DataSet.xsd. from this schema (i don't know if this is called a schema file) i add a DataTable from the toolbox and define the columns just like the columns for the DataSet1.Table(0) in Form1.

next, i use the code

Expand|Select|Wrap|Line Numbers
  1. DataSet1.ReadXmlSchema(SCHEMA_FILE_PATH)

i've tried your way by making a ref to Form1 from Form2 and it works, then i'm curious what if i just use Form1.DataSet1. and it works also. i don't know, maybe this is a special thing in VB?
Oct 5 '08 #7
Curtis Rutland
3,256 Expert 2GB
I think that VB.NET "cheats" for you, since it seems to be a common mistake. Static (shared in vb.net) seems to be a difficult concept for many to grasp, so I guess that they compensate for that.
Oct 5 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: James Fortune | last post by:
In response to different users or situations (data context) I transform the appearance and characteristics of Access Forms through code. This seems to fit in with the idea of polymorphism. Do...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
5
by: Bernard Davis | last post by:
Hi, I need to pass the value held in a field in one form and have it appear in another form. I specifically don't want to have to refer to the original form name as it has passed, because I...
3
by: Sean | last post by:
Have you ever wanted to add the great features inherent in Google Maps? Here is how you do it. ============== == STEP ONE == ============== Create a new MS Access form called frmGoogleMap....
1
by: gm | last post by:
Hi; I have written a database that tracks all the installation we have ever done. I have a small heating company. I have recently started keeping a directory of digital photographs of the...
8
by: Sid | last post by:
I hope someone could help me with this. I am trying to setup a criteria to decide when to allow/not allow user to click on the check box. logically it looks simple but I am not able to...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
1
by: Autostrad | last post by:
Below; I have 2 classes (forms), Namely, "PrintOneEmployee" and "DeleteOne". In class "PrintOneEmployee"; I want to declare a variable with name of "fileName" to contain the name of the file...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.