Data Controls and Open Connections

Question:
I have five data controls on a form that access five tablesfrom the same database. I want to open the database just once.Does having five data controls mean that the databse is openedfive times? Is there any way to open the databse programatically and assign the resultant recordset to data controls? Does this ensure thatthe database is opened just once?

Answer:
For each data control you use (or dynaset), VB creates a connection to the database. You would have 5 active connections to the database in this case. This can become a problem if you have a database that monitors the number of connections in place of checking distinct users. (Oracle is one of the vendors whose database works like this.)

Under VB 4, you can open a database object using the OpenDatabase method. You can then create a recordset with OpenRecordset, and assign it to the Recordset property of the data control. Here’s an example:

‘ dcData is a data control on the form.Dim db as DatabaseDim rs as RecordsetSet db = OpenDatabase(“filename.ext”)Set rs = db.OpenRecordset(“Table1”, dbOpenDynaset)Set dcData.Recordset = rs
That’s all there is to it under VB4. Note that under VB3, you were not able to assign recordsets to data controls.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

More From DevX