The ADOTable Component

Hierarchy: TObject - TPersistent - TComponent - TDataSet - TCustomADODataSet - TADOTable

This component retrieves and operates on a dataset based on an individual table. For operations on multiple tables the TADODataSet or TADOQuery may be better.

Properties

Active

When Active is true the data in the table will be displayed in the data controls linked to it.

Connection

The Connection Object that establishes a connection to the database. The Connection Object will be set up in a unit such as Unit1. The unit that includes the ADOTable that wants to use the Connection Object in unit 1 must include a uses unit1 clause.

CursorLocation
CursorType

ADO cursor types are explored in detail in this section.

The cursor type determines the way a section of code accesses records. There are four main types of cursor:

Further information can be found in this section.

Filter

A filter can be set up to filter data from the table. Further information can be found in this section.

Filtered

Set to to true or false according to whether a filter is active. Further information can be found in this section.

IndexFieldNames
IndexName
LockType

Further information can be found in this section.

MarshallOptions
MasterFields
MasterSource
MaxRecords
ReadOnly
TableName

Setting Up a Connection to a Database

To display and use a database table in OLE JetDB4 (Access 2000 & later) drop an ADO table onto a form. 

Set the Active property in the Object Inspector to true. 

Click on the dieresis in the Connection String property and complete the ADO connection string:

Click on the Build button and select the database:

Set the Table Name property to the name of the table in the database.

To display the data set you will need to add a Data Source from the Data Access Toolbar and also a DB Grid from the Data Controls toolbar. The properties of the data source should be set as follows:

The DataSet property is set to the ADOTable object established earlier. The properties for the DBGrid should be set thus:

The DataSource property is set to the DataSource object (default name DataSource1):

Example One

Here is a sample form:

We can refine the table by editing the fields displayed. To do this double click the table in design view, which opens the field editor:

Click on the third button from the left to add all the fields and then use the mouse to select fields and the delete button to remove them. In this version the Comments memo field has been left out:

Example Two - Reading from an ADOTable into a Combo Box

This fragment of code loads a ComboBox with the contents of a field in an ADO database table:

procedure TForm1.FormCreate(Sender: TObject);
begin
 while not ADOtable1.Eof do
  begin
   combobox1.Items.Add(ADOTable1ClubName.Value);
   adotable1.Next;
  end;
end;

The ComboBox is filled with the values from a field called 'ClubName' (from the Soccer database developed in the Access notes). You could then use the ItemIndex property of the ComboBox to take any required action based on the user's selection, for example:

procedure TForm1.ComboBox1Change(Sender: TObject);
var i:integer;
begin
 case combobox1.ItemIndex of
  0: ADOTable1.First;
  1: begin
       ADOTable1.First; ADOTable1.Next; 
//find record required
      end;
  2: begin
       ADOTable1.First;
       for i:= 1 to 2 do
        ADOTable1.Next; 
//find record required
      end;
3: begin
     ADOTable1.First;
     for i:= 1 to 3 do
      ADOTable1.Next; 
//find record required
    end;
 end; //case
end;

This code takes the itemindex property of the ComboBox and uses it to access the corresponding record in the ADO table. The method of finding the relevant record may be crude but it works.

Example Three

This example reads values from an ADO Table into one combo box and then reads a second value from another Table into another combo box:

The code:

procedure TForm1.FormCreate(Sender: TObject);
begin
 while not adotable1.Eof do
begin
 combobox1.Items.Add(adotable1name.value);
 adotable1.Next;
end;
 adotable1.close;
end;

and:

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
 combobox2.Clear;
 adotable3.Open;
 while not adotable3.eof do
 begin
  if adotable3author.Value = combobox1.Text then
   combobox2.Items.Add(adotable3title.Value);
  adotable3.Next;
 end;
adotable3.Close;
end;

Example Four

In this second example data is read into both combo boxes from the same ADO table:

The code:

procedure TForm1.FormCreate(Sender: TObject);
begin
 while not adotable1.Eof do
begin
 combobox1.Items.Add(adotable1name.Value);
 adotable1.Next;
end;
adotable1.Close;
end;

and:

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
 combobox2.Clear;
 combobox2.text:='Select instrument here';
 adotable1.Open;
 while not adotable1.Eof do
  begin
   if adotable1name.Value = combobox1.Text then
    combobox2.Items.Add(adotable1instrument.Value);
   adotable1.Next;
  end;
adotable1.close;
end;

Back to Tutorial

Back to Palette List