Template program for Delphi

<< Click to Display Table of Contents >>

Navigation:  Application development > Introduction to BDE programming > BDE Programming in Delphi >

Template program for Delphi

Previous pageReturn to chapter overviewNext page
hmtoggle_plus1See also

Below is the complete program that puts all of the individual steps together. It demonstrates each of the basic steps described in the "Basic procedure" for BDE application development. You can execute the template program and step through it to see how it works: it opens a BDE sample table, visits one record, and retrieves the value from one field in that record. Use this template program as a skeleton on which to build your own BDE programs.

In this example program, the handler for the OnClick event of the TButton component Button1 executes the BDE code. The BDE code accesses a Paradox table and retrieves the value from a field in that table. The retrieved field value is assigned as the value of the Caption property of the TLabel component Label1.

unit BDEProg1;

 

interface

 

uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, BDE;

 

type

  TForm1 = class(TForm)

    Button1: TButton;

    Label1: TLabel;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

end;

 

var

  Form1: TForm1;

 

implementation

 

{$R *.DFM}

 

procedure TForm1.Button1Click(Sender: TObject);

var

  hDB: hDBIDb; // Database handle

  hCur: hDBICur; // Handle to the data cursor

  szTableName: array [0..DBIMAXNAMELEN] of Char; // Table name

  CursorProps: CURProps; // Data cursor properties

  RecordBuffer: pBYTE; // Buffer into which to load a record

  Company: array [0..30] of Char;  // Variable for Company field

  IsBlank: BOOL; // Variable for blank fields

begin

  Check(DbiInit(nil));

  Check(DbiOpenDatabase(nil, nil, dbiREADONLY, dbiOPENSHARED, nil, 0, nil, nil, hDB));

  Check(DbiSetDirectory(hDB, 'd:\Program Files\Borland\Borland Shared\Data'));

  Check(DbiSetPrivateDir('c:\Temp'));

  szTableName := 'Customer';

  Check(DbiOpenTable(hDB, szTableName, szPARADOX, nil, nil, 0, dbiREADONLY, dbiOPENSHARED, xltFIELD, False, nil, hCur));

  Check(DbiGetCursorProps(hCur, CursorProps));

  RecordBuffer := AllocMem(CursorProps.iRecBufSize * SizeOf(BYTE));

  Check(DbiSetToBegin(hCur));

  Check(DbiGetNextRecord(hCur, dbiNOLOCK, RecordBuffer, nil));

  Check(DbiGetField(hCur, 2, RecordBuffer, pBYTE(@Company), IsBlank));

  FreeMem(RecordBuffer);

  if not (hCur = nil) then Check(DbiCloseCursor(hCur));

  if not (hDB = nil) then Check(DbiCloseDatabase(hDB));

  Check(DbiExit);

  Label1.Caption := StrPas(Company);

end;

 

end.