Help & Manual authoring tool
How to get the list of all tables and queries in a database (Paradox, Access, etc.) using the BDE

Getting the list of all tables and queries in a database using the BDE

Copyright © 2000 Ernesto De Spirito

Pascal Newsletter. Free ezine for Delphi (and Kylix) programmers with articles, news, reviews, tips, trinks, and links to new Delphi content on the web!

DbiOpenTableList

If you need to know the names of all tables and/or queries (views) in a database opened with the BDE, you can use DbiOpenTableList to create a cursor* with the table and query names, and then you can read it calling DbiGetNextRecord as shown in the example below that stores the names in a ListBox, indicating whether they correspond to a table or query.

(*) A cursor is a "logical table". For example when we open a table or query, what we get is a cursor, that is, a set of data organized in rows and columns, independently of whether this data comes from a physical table, part of table, many tables or any other data source.

To try the example, simply place a ListBox and a Button on a form, and generate the button's OnClick event handler:

uses db, dbtables, bde;

procedure TForm1.Button1Click(Sender: TObject);
var
  db1: TDatabase;
  hCursor: hDBICur;        // Cursor for table and query names
  ListDesc: TBLBaseDesc;   // Record of the cursor
begin
  ListBox1.Clear;
  db1 := nil;
  try
    db1 := TDatabase.Create(nil);
    // Set the neccessary properties to open your database
    db1.DatabaseName := 'Access_ODBC_Test';
    db1.LoginPrompt := False;

    db1.Connected := True;

    // Generates a cursor with all table and query names
    Check(DbiOpenTableList(db1.Handle, False, False, '*', hCursor));

    // Move thru the records of the cursor to get the names
    while (DbiGetNextRecord(hCursor, dbiNOLOCK, @ListDesc, nil)
          = dbiErr_None) do
    if ListDesc.bView then     // Is it a query?
      ListBox1.Items.Add(ListDesc.szName + ' (Query)')
      else
        ListBox1.Items.Add(ListDesc.szName + ' (Table)');

    // Close the cursor
    dbiCloseCursor(hCursor);

    db1.Connected := False;
  except
    db1.Free;
    raise;
  end;
  db1.Free;
end;

Instead of a variable of type TDatabase created for the purpose, you can use a TDatabase control placed on a form, or the Database property of a TTable or TQuery control.

JfControls Library - for Delphi and C++ Builder
Copyright © 2000/2006 Ernesto De Spirito.   All rights reserved.