devxlogo

Data Controls and Open Connections

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.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist