shared Dataset



I have seen some posts on this subject but I still cannot make my app work right.

I have a MDI application with a parent form and two child forms.
The parent form is called Mainform
The child forms are called invoiceForm and customerForm

I have a dataset containing customer info which I would like to make available to all forms.
The dataset is called dsCustomers

So in my parent form code in the general declarations area I have this
Public Shared WithEvents DSCust As New InvoiceWhiz.dsCustomers
Public custForm As New customerForm
Public invForm as new invoiceForm

I open each form from MainForm like this
invForm.MdiParent = Me
custForm.MdiParent = Me

I reference the dataset from the children like this


datagrid.datasource = Mainform.DSCust.customer


All of this builds with no errors and runs fine at first then VS gives an error like this

C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\InvoiceWhiz\customerForm.vb(70): The variable 'DSCust' is either undeclared or was never assigned.

And then erases my reference in the child code.

Cany anyone help me out on this?


Posted via Group




well instead of making it public availlable you could probably better pass
it to the other form as a parameter if the data is required for he life of
the object you could pass it in the constructor


Michel Posseth

Phil G.

Hi Kratz,

I am going to give the best help that 'I' can! It may not answer your
question(s) but I guess it should work. Maybe others with more db knowledge
will post something more descriptive.

I am not sure how you actually get your data into the dataset, whether you
code the connection perhaps and use the Fill method of the dataadapter, or
whether you are dragging connection components onto your form from within
the IDE.

Not sure why you use WithEvents. Datasets only have one event which is
MergeFailed (other than dispose!). Perhaps you are using this!!

I would probably do something like


Public dsMain as New Dataset
(MainForm Load Event)

'Declare and instantiate data components
da.Fill(dsMain, "Results")


dgMdi.DataSource = MainForm.dsMain
dgMdi.DataMember = "Results"

HTH, Phil


Just a suggestion but I would create a class for my database procedures and
set up properties and methods for setting and returning data from/to my
database and/or dataset. I think you will find this a lot easier to keep up
your database/dataset iaccess procedures if they are in one class. You can
instantiate this class in your mainform and access it
"MainForm.myDBClass.myProperty or make it global.


Thanks to all for the suggestions. I decided to try Michel's suggestions
first. So in my ParentForm code I have

Private DSCust As New xxxxx.dsCustomers
Public custForm As New customerForm(DSCust)
Public invForm As New invoiceform(DSCust)

in the general declarations area.

Then in each childForm code I have

Public Sub New(ByRef DsCust As xxxxx.dsCustomers)

but when I try to reference the dataset in child code like this

datagrid.datasource = DsCust.customer

I get errors saying "Name 'DsCust' is not declared"

What am I doing wrong?
Thanks again,


well i would go for an aproach like this

Public Class clsMain
Private dsMain As New DataSet
Friend Sub justamethod()
Dim cchild As New ClsChild(dsMain)
dsMain = cchild.dsRet
End Sub
End Class

Friend Class ClsChild
Private _ds As DataSet
Friend ReadOnly Property dsRet() As DataSet
Return _ds
End Get
End Property
Friend Sub New(ByVal ds As DataSet)
_ds = ds
End Sub
Friend Sub PerformActionsOnDS()
'poerform you actions on the dataset
End Sub
End Class

clschild can "live" on its own when the required dataset is passed




Michel thanks so much for the help. I got it working. I still have one
question though. When you have a property like this
Public class2
Public Property ds as TypedDataset
End Property

If you set the property from another class like

class2.ds = me.datasetWithData

does it just do a copy of the structure of the dataset and leave out the

thanks again,


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question