Glen Morse
Glen Morse

Reputation: 2603

ADO Connection one or four?

I am trying to connect to a mdb (access 2000) My App has 1 main screen with 4 buttons. each button will open a new form.

I need access to this database on all 4 of the sub forms, Do i have to add a ADOConnection to each form? Can i make the connection on the main form and thats it? OR is there even a better way?

Upvotes: 0

Views: 1547

Answers (3)

You must use a unique TADOCOnnection for the application (serving all forms). Each form can use TADOQuery, TADOTable,... with the main connection.

There's no reason to use 4 connections (TADOConnection).

Upvotes: 1

TDC
TDC

Reputation: 397

Put the TADOConnection on a Data Module and USE the Data Module in all the forms to share the connection.

Open your project:

File -> New -> Other Delphi Projects -> Delphi Files Data Module

Then:

Project -> Options Forms Move the Data Module to the top of the Auto-create forms

Upvotes: 4

Marjan Venema
Marjan Venema

Reputation: 19356

You only need one ADOConnection that you can use from all your forms. Easiest way to share that connection is to create a data module, put the ADOConnection on that, then use the data module and its connection from all your forms. Make sure the data module is created before the forms are.

You can also put the query and table components on one or more data modules. I have found it helps promote separation between UI en data access, but that is entirely up to you.

The only reason for multiple connections is when you access a database from multiple threads. In that case you would need one connection for each thread from which you access the database. You are doing everything from the main thread and thus only need a single connection.

Upvotes: 7

Related Questions