mainfrm.pas

来自「《Delphi开发人员指南》配书原码」· PAS 代码 · 共 63 行

PAS
63
字号
unit MainFrm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, DB, DBTables, StdCtrls;

type
  TMainForm = class(TForm)
    dbMain: TDatabase;
    lbFields: TListBox;
    tblParts: TTable;
    dsParts: TDataSource;
    dbgParts: TDBGrid;
    qryParts: TQuery;
    lblFields: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure lbFieldsClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.FormCreate(Sender: TObject);
begin
  { Retrieve a list of field names from the PARTS.DB table so that
    the use can select the field on which to sort the query. These
    fields names are placed into a TListBox. }
  tblParts.Open;
  try
    tblParts.GetFieldNames(lbFields.Items);
  finally
    tblParts.Close;
  end;
end;

procedure TMainForm.lbFieldsClick(Sender: TObject);
{ Define a constant string from which the SQL string will be built }
const
   SQLString = 'SELECT * FROM PARTS ORDER BY %s';
begin
  with qryParts do
  begin
    Close;     // Make sure the query is closed.
    SQL.Clear; // Clear any previous SQL statement.
    { Now add the new SQL statement constructed with the format
      function }
    SQL.Add(Format(SQLString, [lbFields.Items[lbFields.ItemIndex]]));
    Open;  { Now open Query1 with the new statement }
  end;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?