main.pas
上传用户:fh681027
上传日期:2022-07-23
资源大小:1959k
文件大小:3k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit main;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   DBCtrls, Db, DBTables, Grids, DBGrids, ExtCtrls, StdCtrls, Variants;
  6. type
  7.   TMainForm = class(TForm)
  8.     Panel1: TPanel;
  9.     DBGrid1: TDBGrid;
  10.     DBNavigator1: TDBNavigator;
  11.     btnClose: TButton;
  12.     btnApply: TButton;
  13.     procedure btnCloseClick(Sender: TObject);
  14.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure DBGrid1EditButtonClick(Sender: TObject);
  17.     procedure btnApplyClick(Sender: TObject);
  18.   private
  19.     { Private declarations }
  20.   public
  21.     { Public declarations }
  22.   end;
  23. var
  24.   MainForm: TMainForm;
  25. implementation
  26. uses
  27.   Data, depts, jobs;
  28. {$R *.DFM}
  29. resourcestring
  30.   CloseMsg = 'Data has been modified.'#13#13'Save changes?';
  31. procedure TMainForm.btnCloseClick(Sender: TObject);
  32. begin
  33.   Close;
  34. end;
  35. procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  36. var
  37.   Res: Integer;
  38. begin
  39.   with dmData do
  40.     if qryEmployee.UpdatesPending then
  41.     begin
  42.       Res := MessageDlg(CloseMsg, mtInformation, mbYesNoCancel, 0);
  43.       if Res = mrYes then
  44.         AppDB.ApplyUpdates([qryEmployee]);
  45.       CanClose := Res <> mrCancel;
  46.     end;
  47. end;
  48. procedure TMainForm.FormCreate(Sender: TObject);
  49. begin
  50.   dmData.qryEmployee.Open;
  51. end;
  52. procedure TMainForm.DBGrid1EditButtonClick(Sender: TObject);
  53. begin
  54.   // Department?
  55.   if DBGrid1.SelectedField = dmData.qryEmployeeDEPARTMENT then
  56.     with TfrmDepartments.Create(Self) do
  57.     try
  58.       dmData.qryDepartment.Locate('DEPT_NO',
  59.         dmData.qryEmployeeDEPT_NO.Value, []);
  60.       if ShowModal = mrOk then
  61.         with dmData do
  62.           begin
  63.             if not (qryEmployee.State in [dsEdit, dsInsert]) then
  64.               qryEmployee.Edit;
  65.             qryEmployeeDEPT_NO.Value :=  qryDepartment.Fields[0].Value;
  66.             qryEmployeeDEPARTMENT.Value := qryDepartment.Fields[1].Value;
  67.           end;
  68.     finally
  69.       Free;
  70.     end
  71.   else
  72.     with TfrmJobs.Create(Self) do
  73.     try
  74.       with dmData do
  75.         qryJob.Locate('JOB_CODE;JOB_GRADE;JOB_COUNTRY',
  76.           VarArrayOf([qryEmployeeJOB_CODE.Value,qryEmployeeJOB_GRADE.Value,
  77.           qryEmployeeJOB_COUNTRY.Value]), []);
  78.       if ShowModal = mrOk then
  79.         with dmData do
  80.         begin
  81.           if not (qryEmployee.State in [dsEdit, dsInsert]) then
  82.             qryEmployee.Edit;
  83.           qryEmployeeJOB_CODE.Value :=  qryJob.Fields[0].Value;
  84.           qryEmployeeJOB_GRADE.Value := qryJob.Fields[1].Value;
  85.           qryEmployeeJOB_COUNTRY.Value := qryJob.Fields[2].Value;
  86.           qryEmployeeJOB_TITLE.Value := qryJob.Fields[3].Value;
  87.           // set salary to min_salary
  88.           qryEmployeeSALARY.Value := qryJob.Fields[4].Value;
  89.         end;
  90.     finally
  91.       Free;
  92.     end;
  93. end;
  94. procedure TMainForm.btnApplyClick(Sender: TObject);
  95. begin
  96.   with dmData do
  97.     if qryEmployee.UpdatesPending then
  98.     begin
  99.       AppDB.ApplyUpdates([qryEmployee]);
  100.       // refresh data
  101.       qryEmployee.Close;
  102.       qryEmployee.Open;
  103.       btnApply.Enabled := False;
  104.     end;
  105. end;
  106. end.