📄 main.pas
字号:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls, DB, Grids, DBGrids, ADODB, ComCtrls, ToolWin,
Menus, ImgList;
type
TMainForm = class(TForm)
DBGrid: TDBGrid;
DataSource: TDataSource;
ADOQuery: TADOQuery;
ToolBar1: TToolBar;
newButton: TToolButton;
DeleteButton: TToolButton;
EditButton: TToolButton;
SearchButton: TToolButton;
ToolButton5: TToolButton;
ReportButton: TToolButton;
LoadButton: TToolButton;
ToolButton9: TToolButton;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton6: TToolButton;
ImageList1: TImageList;
procedure SetFieldWidth();
procedure newButtonClick(Sender: TObject);
procedure DeleteButtonClick(Sender: TObject);
procedure EditButtonClick(Sender: TObject);
procedure SearchButtonClick(Sender: TObject);
procedure ToolButton5Click(Sender: TObject);
procedure ReportButtonClick(Sender: TObject);
procedure LoadButtonClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ToolButton6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses new, Search, LEdit, report, Load, About;
{$R *.dfm}
procedure TMainForm.SetFieldWidth ();
var i:integer;
begin
for i:=0 to DBGrid.FieldCount-1 do
DBGrid.Fields[i].DisplayWidth:=8;
DBGrid.Fields[1].DisplayWidth:=18;
DBGrid.Fields[5].DisplayWidth:=36;
end;
procedure TMainForm.newButtonClick(Sender: TObject);
begin
newForm.XMEdit.Text :='';
newForm.SFZedit.Text :='';
newForm.SCZedit.Text :='';
newForm.DJedit.Text :='';
newForm.XZedit.Text:='';
newForm.DHedit.Text :='';
newForm.DZedit.Text :='';
newForm.JHCheckBox.Checked :=true;
newForm.Caption :='新增人员记录';
newForm.ShowModal ;
if newForm.QueDin then
begin
DataSource.DataSet.Insert;
DataSource.DataSet.FieldByName('姓名').AsString:=newForm.XMEdit.Text;
DataSource.DataSet.FieldByName('身份证号').AsString:=newForm.SFZEdit.Text;
DataSource.DataSet.FieldByName('伤残证号').AsString:=newForm.SCZEdit.Text;
DataSource.DataSet.FieldByName('等级').AsString:=newForm.DJEdit.Text;
DataSource.DataSet.FieldByName('性质').AsString:=newForm.XZEdit.Text;
DataSource.DataSet.FieldByName('地址').AsString:=newForm.DZEdit.Text;
DataSource.DataSet.FieldByName('电话').AsString:=newForm.DHEdit.Text;
DataSource.DataSet.FieldByName('激活').AsBoolean:=newForm.JHCheckBox.Checked;
DataSource.DataSet.Post;
end;
end;
procedure TMainForm.EditButtonClick(Sender: TObject);
begin
newForm.XMEdit.Text:=DataSource.DataSet.FieldByName('姓名').AsString;
newForm.SFZEdit.Text:=DataSource.DataSet.FieldByName('身份证号').AsString;
newForm.SCZEdit.Text:=DataSource.DataSet.FieldByName('伤残证号').AsString;
newForm.DJEdit.Text:=DataSource.DataSet.FieldByName('等级').AsString;
newForm.XZEdit.Text:=DataSource.DataSet.FieldByName('性质').AsString;
newForm.DZEdit.Text:=DataSource.DataSet.FieldByName('地址').AsString;
newForm.DHEdit.Text:=DataSource.DataSet.FieldByName('电话').AsString;
newForm.JHCheckBox.Checked:=DataSource.DataSet.FieldByName('激活').AsBoolean;
newForm.Caption :='修改人员记录';
newForm.ShowModal ;
if newForm.QueDin then
begin
DataSource.DataSet.Edit;
DataSource.DataSet.FieldByName('姓名').AsString:=newForm.XMEdit.Text;
DataSource.DataSet.FieldByName('身份证号').AsString:=newForm.SFZEdit.Text;
DataSource.DataSet.FieldByName('伤残证号').AsString:=newForm.SCZEdit.Text;
DataSource.DataSet.FieldByName('等级').AsString:=newForm.DJEdit.Text;
DataSource.DataSet.FieldByName('性质').AsString:=newForm.XZEdit.Text;
DataSource.DataSet.FieldByName('地址').AsString:=newForm.DZEdit.Text;
DataSource.DataSet.FieldByName('电话').AsString:=newForm.DHEdit.Text;
DataSource.DataSet.FieldByName('激活').AsBoolean:=newForm.JHCheckBox.Checked;
DataSource.DataSet.Post;
end;
end;
procedure TMainForm.DeleteButtonClick(Sender: TObject);
begin
if messageBox(handle,'是否要删除人员记录?','删除询问',MB_YESNO or MB_ICONWARNING)=IDYES then
if not Datasource.DataSet.Eof or not DataSource.DataSet.Bof then
DataSource.DataSet.Delete;
end;
procedure TMainForm.SearchButtonClick(Sender: TObject);
var str:string;
begin
SearchForm.ShowModal;
if SearchForm.QueDin then
begin
str:='';
if SearchForm.XMCheckBox.Checked then
if SearchForm.MHCheckBox.Checked then
str:=str+'姓名 like "%'+SearchForm.XMEdit.Text+'%"'
else
str:=str+'姓名="'+SearchForm.XMEdit.Text+'"';
if SearchForm.SFZCheckBox.Checked then
begin
if str<>'' then str:=str+' and ';
str:=str+'身份证号="'+SearchForm.SFZEdit.Text+'"';
end;
if SearchForm.SCZCheckBox.Checked then
begin
if str<>'' then str:=str+' and ';
str:=str+'伤残证号="'+SearchForm.SCZEdit.Text+'"';
end;
if SearchForm.DJCheckBox.Checked then
begin
if str<>'' then str:=str+' and ';
str:=str+'等级="'+SearchForm.DJEdit.Text+'"';
end;
if SearchForm.XZCheckBox.Checked then
begin
if str<>'' then str:=str+' and ';
str:=str+'性质="'+SearchForm.XZEdit.Text+'"';
end;
if SearchForm.JHCheckBox.Checked then
begin
if str<>'' then str:=str+' and ';
if SearchForm.YesRadioButton.Checked then
str:=str+'激活=true'
else
str:=str+'激活=false';
end;
if str<>'' then
begin
if AdoQuery.Active then AdoQuery.Close;
AdoQuery.SQL.Text :='SELECT * FROM people WHERE '+Str;
AdoQuery.Open;
end else
begin
if AdoQuery.Active then AdoQuery.Close;
AdoQuery.SQL.Text :='SELECT * FROM people';
AdoQuery.Open;
end;
SetFieldWidth();
end;
end;
procedure TMainForm.ToolButton5Click(Sender: TObject);
begin
LEditForm.ShowModal;
if LEditForm.QueDin then
begin
DataSource.DataSet.First;
While not DataSource.DataSet.Eof do
begin
DataSource.DataSet.Edit;
if LEditForm.LEdit.Text ='激活' then
begin
if LowerCase(LEditForm.ZEdit.Text)='true' then
DataSource.DataSet.FieldByName('激活').AsBoolean:=true;
if LowerCase(LEditForm.ZEdit.Text)='false' then
DataSource.DataSet.FieldByName('激活').AsBoolean:=false;
end else
begin
DataSource.DataSet.FieldByName(LEditForm.LEdit.Text).AsString:=LEditForm.ZEdit.Text;
end;
DataSource.DataSet.Post;
DataSource.DataSet.Next;
end;
end;
end;
procedure TMainForm.ReportButtonClick(Sender: TObject);
var ExcelApp:Variant;
i,j,num,t:integer;
begin
ReportForm.ShowModal;
if ReportForm.QueDin then
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
except
MessageBox(handle,'您的机器里未安装Microsoft Excel。','警告',MB_OK or MB_ICONWARNING);
Exit;
end;
try
ExcelApp.workBooks.Add;
DataSource.DataSet.First;
num:=ReportForm.numerEdit.Value;
ExcelApp.ActiveSheet.Rows[1].Font.italic:= True;
ExcelApp.ActiveSheet.Rows[1].Font.Bold:= True;
i:=1;j:=1;
for t:=1 to num do
begin
if ReportForm.XMCheckBox.Checked then
begin
ExcelApp.Cells(j,i):='姓名';
inc(i);
end;
if ReportForm.SFZCheckBox.Checked then
begin
ExcelApp.Cells(j,i):='身份证号';
inc(i);
end;
if ReportForm.SCZCheckBox.Checked then
begin
ExcelApp.Cells(j,i):='伤残证号';
inc(i);
end;
if ReportForm.DJCheckBox.Checked then
begin
ExcelApp.Cells(j,i):='等级';
inc(i);
end;
if ReportForm.XZCheckBox.Checked then
begin
ExcelApp.Cells(j,i):='性质';
inc(i);
end;
if ReportForm.DZCheckBox.Checked then
begin
ExcelApp.Cells(j,i):='地址';
inc(i);
end;
if ReportForm.DHCheckBox.Checked then
begin
ExcelApp.Cells(j,i):='电话';
inc(i);
end;
if ReportForm.JECheckBox.Checked then
begin
ExcelApp.Cells(j,i):='金额';
inc(i);
end;
end;
j:=2;
While not DataSource.DataSet.Eof do
begin
i:=1;
for t:=1 to num do
begin
if ReportForm.XMCheckBox.Checked then
begin
ExcelApp.Cells(j,i):=DataSource.DataSet.FieldByName('姓名').AsString;
inc(i);
end;
if ReportForm.SFZCheckBox.Checked then
begin
ExcelApp.Cells(j,i):=DataSource.DataSet.FieldByName('身份证号').AsString;
inc(i);
end;
if ReportForm.SCZCheckBox.Checked then
begin
ExcelApp.Cells(j,i):=DataSource.DataSet.FieldByName('伤残证号').AsString;
inc(i);
end;
if ReportForm.DJCheckBox.Checked then
begin
ExcelApp.Cells(j,i):=DataSource.DataSet.FieldByName('等级').AsString;
inc(i);
end;
if ReportForm.XZCheckBox.Checked then
begin
ExcelApp.Cells(j,i):=DataSource.DataSet.FieldByName('性质').AsString;
inc(i);
end;
if ReportForm.DZCheckBox.Checked then
begin
ExcelApp.Cells(j,i):=DataSource.DataSet.FieldByName('地址').AsString;
inc(i);
end;
if ReportForm.DHCheckBox.Checked then
begin
ExcelApp.Cells(j,i):=DataSource.DataSet.FieldByName('电话').AsString;
inc(i);
end;
if ReportForm.JECheckBox.Checked then
begin
ExcelApp.Cells(j,i):=DataSource.DataSet.FieldByName('金额').AsString;
inc(i);
end;
DataSource.DataSet.Next;
if DataSource.DataSet.Eof then Break;
end;
inc(j);
end;
ExcelApp.Visible := True;
ExcelApp:=Unassigned;
except
MessageBox(handle,'不能正确操作Excel文件。可能是该文件已被其他程序打开, 或系统错误。','警告',MB_OK or MB_ICONWARNING);
ExcelApp.Quit;
ExcelApp:=Unassigned;
end;
end;
end;
procedure TMainForm.LoadButtonClick(Sender: TObject);
var DJ,XZ:string;
begin
LoadForm.ShowModal;
if LoadForm.QueDin then
begin
if ADOQuery.Active then ADOQuery.Close;
ADOQuery.Open;
DataSource.DataSet.First;
While not DataSource.DataSet.Eof do
begin
DJ:=DataSource.DataSet.FieldByName('等级').AsString;
XZ:=DataSource.DataSet.FieldByName('性质').AsString;
if LoadForm.ADOQuery.Active then LoadForm.ADOQuery.Close;
LoadForm.ADOQuery.SQL.Text :='SELECT * FROM money1 WHERE 等级="'+DJ+'" and 性质="'+XZ+'"';
LoadForm.ADOQuery.Open;
DataSource.DataSet.Edit ;
DataSource.DataSet.FieldByName('金额').AsString:= LoadForm.DataSource.DataSet.FieldByName('金额').AsString;
DataSource.DataSet.Post ;
DataSource.DataSet.Next;
end;
SetFieldWidth();
end;
end;
procedure TMainForm.FormShow(Sender: TObject);
var Path,Str:String;
begin
Path:=ExtractFileDir(Application.ExeName);
if pchar(Path)[Length(Path)-1]<>'\' then Path := Path + '\';
Path:=Path+'SCJ.mdb;';
Str:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+Path+'Persist Security Info=False';
if ADOQuery.Active then ADOQuery.Close;
ADOQuery.ConnectionString:=Str;
ADOQuery.SQL.Text :='SELECT * FROM people';
ADOQuery.Open;
SetFieldWidth();
end;
procedure TMainForm.ToolButton6Click(Sender: TObject);
begin
AboutForm.ShowModal;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -