Accessing a Database in Multiple Forms

DonnaF

Active member
Joined
Mar 20, 2003
Messages
30
I have a database with multiple tables. I want to be able to update different tables on different forms (using VB.Net). Do I need to set up an connection, data adapter and dataset for each form? I would guess that if Im accessing different tables, that I would at least need to set up different dataset on each form. What about the connection and data adapter? Also, if I access data from one table on a form, can I pass that data to another form? Im fairly new to processing with databases, so I wasnt sure how this would work.

Thanks, Donna
 
Donna

It is enough that you declare the dataset and the dataadapoters as public in Form1 and then access them from the other forms on condition you open the other forms from form1.

From within form1 when you want to open form2

Dim f as new Form2()
Me.AddOwnedForm(f)
f.showDialog() or f.Show()

Now from within form2 you can access the dataset and the data adapters by :
Ctype(Me.Owner,Form1).theDataSetNameYouChose
Ctype(Me.Owner,Form1).theDataAdapterNameYouChose

Hope this helps
 
When I dragged the data adapter, and dataset onto the form, it generated the following in the Windows Form Designer generated code:

Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbUpdateCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbDeleteCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter
Friend WithEvents DataSet1 As System.Data.DataSet
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

In order to declare the dataset and data adapter as public, do I need to change Friend WithEvents to Public WithEvents?

Im not familiar with how databases and multiple forms work, so I need some help understanding this.

Also, you said I needed to open the other forms from form1, in order to access the database. What would happen if I opened a form3 from form2, and wanted to access the database?

Thanks for your help, Donna
 
As you said simply change the Friend to Public...

As for being not familiar with databases and multiple forms...

This is a scenario of a database with one form (in this case form1) you are only accessing the data adapters of form1 from other forms, so when you need to update the adapter to the database it is the same as having it in one form.

You would access the dataset from form2 as i showed you
Ctype(Me.Owner,Form1).DataSet1
Ctype(Me.Owner,Form1).OleDbDataAdapter1

Now if you want to open form3 from form2 then you would open form 3 as you opened form2 from form1
In form2 type this when you want to open form3
Dim f as new Form3()
Me.AddOwnedForm(f)
f.showDialog() or f.Show()

Now in order to access the dataset and the dataadapters from form3 you have to reference its owner (Form2) and then the owner of Form2 which is form1.

Ctype(Me.Owner,Form2).Ctype(Me.Owner,Form1).DataSet1
Ctype(Me.Owner,Form2).Ctype(Me.Owner,Form1).OleDbDataAdapter1
 
Back
Top