Placing Active Query Builder on a form

Follow the steps below to place a component on a form using Delphi IDE.

  1. Place the TacQueryBuilder component on the form.
  2. Place required metadata and syntax provider components on the form (read more about syntax and metadata providers). Define database connection for the metadata provider.
  3. Link the components above to the TacQueryBuilder by setting MetadataProvider and SyntaxProvider properties.
  4. Place the TacSQLBuilderPlainText component on the form to get SQL code generated by the Query Builder with formatting. Link it to the TacQueryBuilder component by setting it's QueryBuilder property.
  5. Add the TMemo or any other text editing component (for example, TSynEdit) to a form.
  6. Now you should establish connection between the TacSQLBuilderPlainText and the TMemo components.
    Enter the following code to OnExit event of TMemo component:
    acQueryBuilder1.SQL := Memo1.Text;
    Enter the following code to OnSQLUpdated event of TacSQLBuilder component:
    Memo1.Text := acSQLBuilderPlainText1.SQL;
  7. Execute the following code to load metadata and activate Active Query Builder:
    acQueryBuilder1.RefreshMetadata; 
    // syncronous loading; may take a long time in case of large database schema
    or
    acQueryBuilder1.RefreshMetadataAsync; 
    // asyncronous loading; allows to work with the component almost instantly. 
    // database schema tree will be filled after completion of the metadata loading process.
  8. That's all! Now you can run your application.

    Don't forget to activate your database connection component.

Creating a component programmatically

The following sample illustrates programmatic creation of needed objects to work with the component.

unit uActiveQueryBuilder;

uses 
  // ...
  acAST, acQBBase;

interface

type
  TForm1 = class(TForm)
    // ...
  private
    adoConnection: TADOConnection;
    acQueryBuilder: TacQueryBuilder;

    // specify the needed type of metadata provider according to your database connection
    acMetadataProvider: TacADOMetadataProvider;

    // specify the needed type of syntax provider for your database server
    acSyntaxProvider: TacMSAccessSyntaxProvider; 
    acPlainTextSQLBuilder: TacSQLBuilderPlainText;
    // ...
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
begin
  acQueryBuilder := TacQueryBuilder.Create(Self);
  acMetadataProvider := TacADOMetadataProvider.Create(Self);
  acSyntaxProvder := TacMSAccessSyntaxProvider(Self);

  adoConnection := TADOConnection.Create(Self);
  adoConnection.ConnectionString := 'Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;';

  acMetadataProvider.Connection := adoConnection;
  
  acQueryBuilder.MetadataProvider := acMetadataProvider;
  acQueryBuilder.SyntaxProvider := acSyntaxProvider;

  acPlainTextSQLBuilder := TacSQLBuilderPlainText.Create(Self);
  acPlainTextSQLBuilder.QueryBuilder := acQueryBuilder;

  // get the needed metadata to fill the Database Schema Tree:
  acQueryBuilder.RefreshMetadata;

  // specify the initial SQL if needed:
  acQueryBuilder.SQL := 'SELECT * FROM Table1';

  // display the formatted SQL query to the user:
  ShowMessage(acPlainTextSQLBuilder.SQL);
end; 

This article was helpful for 1 person. Is this article helpful for you?