📄 employee.pas
字号:
unit Employee;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OperateModul, Grids, DBGrids, ExtCtrls, ComCtrls, ToolWin,
StdCtrls, DBCtrls, Mask, Buttons, ADODB, DB, ExtDlgs;
type
TfrmEmployee = class(TfrmOperateModul)
pageControl: TPageControl;
pgBasic: TTabSheet;
pgOther: TTabSheet;
TreeView1: TTreeView;
sbtnCreateEmployeeNo: TSpeedButton;
sbtnDepartment: TSpeedButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
dbedtEmployeeNo: TDBEdit;
dbedtName: TDBEdit;
dbedtDeparyment: TDBEdit;
dbcboxPost: TDBLookupComboBox;
dbcboxNation: TDBLookupComboBox;
dbrbtnSex: TDBRadioGroup;
dbedtWorkDate: TDBEdit;
dbedtBirthday: TDBEdit;
atbEmployee: TADOTable;
atbNation: TADOTable;
atbPost: TADOTable;
dsEmployee: TDataSource;
dsNation: TDataSource;
dsPost: TDataSource;
aqryDepartment: TADOQuery;
atbEmployeeEMPLOYEENO: TWideStringField;
atbEmployeeEMPLOYEENAME: TWideStringField;
atbEmployeeDEPARTMENTNO: TWideStringField;
atbEmployeeSEX: TWideStringField;
atbEmployeeEMPLOYEEDATE: TDateTimeField;
atbEmployeeWORKDATE: TDateTimeField;
atbEmployeeNATIONNO: TWideStringField;
atbEmployeePOSTNO: TWideStringField;
atbEmployeeOther: TADOTable;
dsEmployeeOther: TDataSource;
DBImage: TDBImage;
DBMemo: TDBMemo;
astpEmployeeNo: TADOStoredProc;
bbtnSave: TBitBtn;
bbtnClear: TBitBtn;
stEmployeeName: TStaticText;
OpenPictureDialog1: TOpenPictureDialog;
sbtnImage: TSpeedButton;
Label9: TLabel;
Label10: TLabel;
aqryTree: TADOQuery;
Label11: TLabel;
atbEmployeelkDepartmentName: TStringField;
atbEmployeelkNationName: TStringField;
atbEmployeelkPostName: TStringField;
procedure bbtnSaveClick(Sender: TObject);
procedure bbtnClearClick(Sender: TObject);
procedure sbtnImageClick(Sender: TObject);
procedure pageControlChange(Sender: TObject);
procedure sbtnCreateEmployeeNoClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure sbtnDepartmentClick(Sender: TObject);
procedure DBGrid1DblClick(Sender: TObject);
private
function funCheck() :boolean;
{ Private declarations }
protected
function funNew() :integer; override;
function funDelete() :integer; override;
function funUpdate() :integer; override;
function funSave() :integer; override;
function funQuery() :integer; override;
function funExit() :integer; override;
public
{ Public declarations }
end;
var
frmEmployee: TfrmEmployee;
//供外界访问该表单的接口函数gfunOpenEmployee
function gfunOpenEmployee() :integer;
implementation
uses DataModual,gpubUnit,QueryEmployee;
{$R *.dfm}
//供外界访问该表单的接口函数gfunOpenEmployee
function gfunOpenEmployee() :integer;
begin
frmEmployee :=TfrmEmployee.Create(nil);
frmEmployee.ShowModal ;
Result :=0;
frmEmployee.Free;
end;
//检查表单上输入的数据是否正确
function TfrmEmployee.funCheck() :boolean;
var dDate :TDateTime;
begin
//检查是否有员工编号
if trim(dbedtEmployeeNo.Text) ='' then
begin
application.MessageBox('没有输入员工编号','提示',mb_ok) ;
dbedtEmployeeNo.SetFocus ;
Result := False;
exit;
end;
//检查是否有工作日期
if trim(dbedtWorkDate.Text) ='' then
begin
application.MessageBox('没有输入工作日期','提示',mb_ok) ;
dbedtWorkDate.SetFocus ;
Result := False;
exit;
end;
//检查日期格式是否正确
try
dDate := StrToDateTime(trim(dbedtWorkDate.Text));
except
application.MessageBox('工作日期格式不对','提示',mb_ok) ;
dbedtWorkDate.SetFocus ;
Result := False;
exit;
end;
//检查是否有员工名称
if trim(dbedtName.Text) ='' then
begin
application.MessageBox('没有输入员工名称','提示',mb_ok) ;
dbedtName.SetFocus ;
Result := False;
exit;
end;
//检查是否有员工部门
if trim(dbedtDeparyment.Text) ='' then
begin
application.MessageBox('没有选择员工部门','提示',mb_ok) ;
TreeView1.SetFocus ;
Result := False;
exit;
end;
Result := True;
end;
//新增函数
function TfrmEmployee.funNew() :integer;
begin
Result :=0;
DBGrid1.ReadOnly := True;
if not atbEmployee.Active then exit;
atbEmployee.Append;
atbEmployee.FieldByName('DepartmentNo').AsString :='001';
atbEmployee.FieldByName('PostNo').AsString :='004';
atbEmployee.FieldByName('NationNo').AsString :='001';
atbEmployee.FieldByName('sex').AsString :='男';
//数据控件发生变化
dbedtEmployeeNo.ReadOnly := False;
dbedtName.ReadOnly := False;
dbrbtnSex.ReadOnly := False;
dbcboxPost.ReadOnly :=False;
dbcboxNation.ReadOnly := False;
dbedtBirthday.ReadOnly := False;
dbedtWorkDate.ReadOnly := False;
DBGrid1.Enabled := false;
//使按钮发生变化
sbtnDepartment.Enabled := true;
tbtnSave.Enabled :=True;
tbtnDelete.Enabled := False;
tbtnUpdate.Enabled :=False;
sbtnCreateEmployeeNo.Enabled :=True;
end;
//删除函数
function TfrmEmployee.funDelete() :integer;
var sString :string;
begin
Result :=0;
//首先检查是否选中了要删除的人员,然后询问是否确信删除
if trim(atbEmployee.FieldByName('EmployeeNo').AsString) ='' then
begin
application.MessageBox('没有选择要删除的员工','提示',mb_ok);
exit;
end;
sString := trim(atbEmployee.FieldByName('EmployeeName').AsString);
if application.MessageBox(pchar('是否确信删除员工--'+sString),'提示',mb_YesNo)=IDYes then
atbEmployee.Delete ;
end;
//更新函数
function TfrmEmployee.funUpdate() :integer;
begin
Result :=0;
//将相应的数据控件变为可以修改
dbedtEmployeeNo.ReadOnly := False;
dbedtName.ReadOnly := False;
dbrbtnSex.ReadOnly := False;
dbcboxPost.ReadOnly :=False;
dbcboxNation.ReadOnly := False;
dbedtBirthday.ReadOnly := False;
DBGrid1.Enabled := false;
//使按钮发生变化
sbtnDepartment.Enabled := true;
tbtnSave.Enabled :=True;
tbtnDelete.Enabled := False;
tbtnNew.Enabled :=False;
sbtnCreateEmployeeNo.Enabled := True;
end;
//保存函数
function TfrmEmployee.funSave() :integer;
begin
Result :=0;
//保存后要修改相应的按钮状态
if atbEmployee.State = dsBrowse then exit;
if not funCheck() then exit;
try
atbEmployee.Post;
//使数据控件不可用
dbedtEmployeeNo.ReadOnly := True;
dbedtName.ReadOnly := True;
dbrbtnSex.ReadOnly := True;
dbcboxPost.ReadOnly :=True;
dbcboxNation.ReadOnly := True;
dbedtBirthday.ReadOnly := True;
dbedtWorkDate.ReadOnly := True;
DBGrid1.Enabled :=True;
//使按钮发生变化
tbtnSave.Enabled := False;
tbtnDelete.Enabled := True;
tbtnUpdate.Enabled := True;
tbtnNew.Enabled :=True;
sbtnDepartment.Enabled := False;
sbtnCreateEmployeeNo.Enabled :=False;
except
application.MessageBox('保存数据出错','提示',mb_ok) ;
atbEmployee.Cancel ;
raise;
exit;
end;
end;
//查询函数
function TfrmEmployee.funQuery() :integer;
var sEmployeeNo,sEmployeeName :string;
begin
sEmployeeNo :='';
sEmployeeName :='';
gfunOpenQueryEmployee(sEmployeeNo,sEmployeeName,1);
if trim(sEmployeeNo)='' then exit;
//没有选择员工,不要定位
atbEmployee.Locate('EmployeeNo',sEmployeeNo,[loCaseInsensitive]);
Result :=0;
end;
//退出函数
function TfrmEmployee.funExit() :integer;
begin
Result :=0;
close;
end;
//保存图片到数据库中
procedure TfrmEmployee.bbtnSaveClick(Sender: TObject);
begin
inherited;
if atbEmployeeOther.State <> dsBrowse then
atbEmployeeOther.Post;
end;
//清除选中的图片
procedure TfrmEmployee.bbtnClearClick(Sender: TObject);
begin
inherited;
if not atbEmployeeOther.Active then exit;
if application.MessageBox(pchar('是否确信将员工--'+trim(stEmployeeName.Caption)+
'的图片清除'),'提示',mb_YesNo)=IDYes then
begin
if atbEmployeeOther.State = dsBrowse then
atbEmployeeOther.Edit;
atbEmployeeOther.FieldByName('EmployeeImage').Value :=null;
end;
end;
//查找图片后,显示并装入数据库中
procedure TfrmEmployee.sbtnImageClick(Sender: TObject);
begin
inherited;
//TJPEGImage为JPG文件 ,TBitmap为BMP文件
OpenPictureDialog1.DefaultExt :=GraphicExtension(TBitmap); //仅查找bmp类型文件
OpenPictureDialog1.Filter :=GraphicFilter(TBitmap); //非bmp文件过滤掉
if OpenPictureDialog1.Execute then //正确得到图片
begin
if atbEmployeeOther.State = dsBrowse then //将atbEmployeeOther对象设置为编辑状态
atbEmployeeOther.Edit ;
try
(atbEmployeeOther.FieldByName('EmployeeImage') as
TBlobField).LoadFromFile(OpenPictureDialog1.FileName) ; //将图片显示并装入数据库中
except
application.MessageBox('装入员工照片出错','提示',mb_ok) ;
exit;
end;
end;
end;
//当改变页面到pgOther时不能使用快速工具栏
procedure TfrmEmployee.pageControlChange(Sender: TObject);
begin
inherited;
if PageControl.ActivePage = pgOther then
begin
ToolBar1.Enabled := False;
DBGrid1.ShowHint := True;
end
else
begin
ToolBar1.Enabled :=True;
DBGrid1.ShowHint :=False;
end;
end;
//自动产生员工编号
procedure TfrmEmployee.sbtnCreateEmployeeNoClick(Sender: TObject);
var sYear :string;
begin
inherited;
if trim(dbedtWorkDate.Text) ='' then
begin
application.MessageBox('没有输入工作日期','提示',mb_ok) ;
dbedtWorkDate.SetFocus ;
exit;
end;
sYear := copy(trim(dbedtWorkDate.Text),1,4); //获取参加工作的年份
if astpEmployeeNo.Active then astpEmployeeNo.close;
astpEmployeeNo.Parameters.Clear ; //将存储过程的参数清空
//调用存储过程的代码
// astpEmployeeNo.ProcedureName :='PROEMPLOYEENO';
// astpEmployeeNo.Parameters.CreateParameter('SYEAR',ftString,pdInput,4,null) ;
// astpEmployeeNo.Parameters.CreateParameter('RETURNEMPLOYEENO',
// ftString,pdOutput,8,null) ;
//调用函数的代码
astpEmployeeNo.ProcedureName :='FUNEMPLOYEENO';
astpEmployeeNo.Parameters.CreateParameter('Param1',ftString,pdReturnValue,8,null) ;
astpEmployeeNo.Parameters.CreateParameter('SYEAR',ftString,pdInput,4,null) ;
//设置参数,过程与函数是一样的
astpEmployeeNo.Parameters.ParamByName('SYEAR').Value := sYear;
//执行存储过程或函数
try
astpEmployeeNo.ExecProc ;
if atbEmployee.State = dsBrowse then atbEmployee.Edit ;
atbEmployee.FieldByName('EmployeeNo').Value :=
astpEmployeeNo.Parameters.ParamByName('Param1').Value;
//过程调用
//atbEmployee.FieldByName('EmployeeNo').Value :=
// astpEmployeeNo.Parameters.ParamByName('RETURNEMPLOYEENO').Value ;
except
Application.MessageBox(pchar('产生函数有错,请重试'), '提示', MB_OK);
exit;
end;
end;
//打开表单
procedure TfrmEmployee.FormShow(Sender: TObject);
begin
inherited;
//将所有的数据集打开
if aqryDepartment.Active then aqryDepartment.Close;
try
aqryDepartment.Open ;
except
application.MessageBox('打开部门表出错','提示',mb_ok) ;
close;
end;
if atbNation.Active then atbNation.Close;
try
atbNation.Open;
except
application.MessageBox('打开民族表出错','提示',mb_ok) ;
close;
end;
if atbPost.Active then atbPost.Close;
try
atbPost.Open;
except
application.MessageBox('打岗位表出错','提示',mb_ok) ;
close;
end;
if atbEmployee.Active then atbEmployee.Close;
try
atbEmployee.Open;
except
application.MessageBox('打岗位表出错','提示',mb_ok) ;
close;
end;
if atbEmployeeOther.Active then atbEmployeeOther.Close;
try
atbEmployeeOther.Open;
except
application.MessageBox('打开员工其它表出错','提示',mb_ok) ;
close;
end;
//设置第一页为激活页
pageControl.ActivePage := pgBasic;
//建立部门树
if not gfunCreateTree(TreeView1) then close;
//将数据控件设置为只读
dbedtEmployeeNo.ReadOnly := true;
dbedtDeparyment.ReadOnly := True;
dbedtName.ReadOnly :=true;
dbrbtnSex.ReadOnly := true;
dbcboxPost.ReadOnly :=True;
dbcboxNation.ReadOnly := true;
dbedtBirthday.ReadOnly := true;
dbedtWorkDate.ReadOnly := true;
//将按钮sbtnDepartment、sbtnCreateEmployeeNo变灰
sbtnDepartment.Enabled :=false;
sbtnCreateEmployeeNo.Enabled :=False;
//将按钮tbtnSave变灰
tbtnSave.Enabled :=false;
end;
//当表单关闭时断开所有与数据库连接的数据集
procedure TfrmEmployee.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
inherited;
if atbEmployee.Active then atbEmployee.Close;
if atbNation.Active then atbNation.Close;
if atbPost.Active then atbPost.Close;
if aqryDepartment.Active then aqryDepartment.Close;
if astpEmployeeNo.Active then astpEmployeeNo.Close;
if atbEmployeeOther.Active then atbEmployeeOther.Close;
end;
//为员工修改所属部门
procedure TfrmEmployee.sbtnDepartmentClick(Sender: TObject);
var
i :integer;
sDepartmentNo,sString :string;
begin
inherited;
if not atbEmployee.Active then exit;
if TreeView1.Items.Count <= 0 then exit;
i :=TreeView1.Selected.AbsoluteIndex ;
if i<0 then exit;
sString :=trim(TreeView1.Items[i].Text) ;
i:= Pos('--',sString);
sDepartmentNo := Copy(sString,1,i-1);
if atbEmployee.State = dsBrowse then
atbEmployee.Edit;
atbEmployee.FieldByName('DepartmentNo').AsString := sDepartmentNo;
end;
//双击DBGrid1网格,显示选中的员工的名称、照片和备注信息
procedure TfrmEmployee.DBGrid1DblClick(Sender: TObject);
var sEmployeeNo,sEmployeeName :string;
begin
inherited;
sEmployeeNo := trim(atbEmployee.FieldByName('EmployeeNo').AsString) ;
sEmployeeName := trim(atbEmployee.FieldByName('EmployeeName').AsString);
if sEmployeeNo='' then
begin
application.MessageBox('没有选择员工','提示',mb_ok) ;
exit;
end;
stEmployeeName.Caption := sEmployeeName;
atbEmployeeOther.Filter :='EmployeeNo='+sEmployeeNo ;
atbEmployeeOther.Filtered :=true;
//若没有找到该员工数据,则插入一条空记录
if atbEmployeeOther.RecordCount <1 then
begin
atbEmployeeOther.Append ;
atbEmployeeOther.FieldByName('EmployeeNo').AsString := sEmployeeNo;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -