📄 mmaterialquery.pas
字号:
unit mmaterialquery;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ttemplate1, Menus, ComCtrls, ToolWin, StdCtrls, ExtCtrls,
LabelButton, TradeImage, Grids, DBGrids;
type
Tmaterialquery = class(Ttemplateform1)
Panel1: TPanel;
mmquery: TTradeImg;
LabelBtn20: TLabelBtn;
Image1: TImage;
LabelBtn3: TLabelBtn;
Panel2: TPanel;
Panel3: TPanel;
CBE_Field: TComboBoxEx;
CB_Cond: TComboBox;
edit1: TEdit;
LabelBtn4: TLabelBtn;
LabelBtn2: TLabelBtn;
LabelBtn1: TLabelBtn;
Panel4: TPanel;
LV_SQL: TListView;
RG_Cond: TRadioGroup;
tiNew: TTradeImg;
LabelBtn5: TLabelBtn;
sb_del: TTradeImg;
LabelBtn6: TLabelBtn;
CheckBox1: TCheckBox;
procedure mmqueryClick(Sender: TObject);
procedure tiNewClick(Sender: TObject);
procedure sb_delClick(Sender: TObject);
procedure Image1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
materialquery: Tmaterialquery;
implementation
uses datamodule;
{$R *.dfm}
procedure Tmaterialquery.mmqueryClick(Sender: TObject);
var {*****查找按钮单击事件*****}
No,i:integer;
str,buf1,buf2:AnsiString;
begin
with dm.q_materialmanager do
begin
if(checkbox1.Checked=true)then
begin
if (LV_SQL.Items.Count=0) then
begin
ShowMessage('你还没有选择数据记录查找条件,请选择');
edit1.SetFocus; //焦点在查找值输入框
exit;
end;
Close; //关闭数据集
SQL.Clear; //清除SQL语句
No := LV_SQL.Items.Count; //获得当前ListView对象中的记录条数
for i:=0 to No-1 do //以下所有语句都用来进行转换用户选择查找条件为SQL语句
begin
if (i>=1)then //第二条记录开始需要添加查找逻辑
begin
buf1 := LV_SQL.Items.Item[i].Caption; //通过判断添加对应的逻辑条件
if (buf1 = '并且') then
str := str+' AND '
else
str := str+' OR ';
end;
buf1 := LV_SQL.Items.Item[i].SubItems.Strings[0]; //通过查找字段判断添加对应SQL语句
if (buf1 = '物料编号') then
str := str+'MaterialM_ID'
else if (buf1 = '物料名称') then
str := str+'MaterialM_Name'
else if (buf1 = '所在仓库') then
str := str+'MaterialM_SHouse'
else if (buf1 = '供应商') then
str := str+'MaterialM_Provider'
else if (buf1 = '型号') then
str := str+'Material_Type';
buf2 := LV_SQL.Items.Item[i].SubItems.Strings[1]; //通过查找条件判断添加对应SQL语句
str := str+' like ';
buf2 := LV_SQL.Items.Item[i].SubItems.Strings[2]; //通过查找值判断添加对应SQL语句
if(buf1 = '物料编号') then
str := str+''''+'%'+buf2+'%'+''''
//str := str+''''+buf2+''''
else if (buf1 = '物料名称') then //字符串类型字段
//str := str+''''+buf2+'''' //字符串字段两边需要加上'标志
str := str+''''+'%'+buf2+'%'+''''
else if (buf1 = '所在仓库') then //整数类型字段
//str := str+''''+buf2+''''
str := str+''''+'%'+buf2+'%'+''''
else if (buf1 = '供应商') then //字符串类型字段
str := str+''''+'%'+buf2+'%'+''''
// str := str+''''+buf2+'''' //字符串字段两边需要加上'标志
else if (buf1 = '型号') then
str := str+''''+'%'+buf2+'%'+'''';
end;
end
else if (checkbox1.checked=false) then
begin
if (LV_SQL.Items.Count=0) then
begin
ShowMessage('你还没有选择数据记录查找条件,请选择');
edit1.SetFocus; //焦点在查找值输入框
exit;
end;
Close; //关闭数据集
SQL.Clear; //清除SQL语句
No := LV_SQL.Items.Count; //获得当前ListView对象中的记录条数
for i:=0 to No-1 do //以下所有语句都用来进行转换用户选择查找条件为SQL语句
begin
if (i>=1)then //第二条记录开始需要添加查找逻辑
begin
buf1 := LV_SQL.Items.Item[i].Caption; //通过判断添加对应的逻辑条件
if (buf1 = '并且') then
str := str+' AND '
else
str := str+' OR ';
end;
buf1 := LV_SQL.Items.Item[i].SubItems.Strings[0]; //通过查找字段判断添加对应SQL语句
if (buf1 = '物料编号') then
str := str+'MaterialM_ID'
else if (buf1 = '物料名称') then
str := str+'MaterialM_Name'
else if (buf1 = '所在仓库') then
str := str+'MaterialM_SHouse'
else if (buf1 = '供应商') then
str := str+'MaterialM_Provider'
else if (buf1 = '型号') then
str := str+'Material_Type';
buf2 := LV_SQL.Items.Item[i].SubItems.Strings[1]; //通过查找条件判断添加对应SQL语句
if (buf2 = '等于') then
str := str+'='
else if (buf2 = '不等于') then
str := str+'<>'
else if (buf2 = '大于等于') then
str := str+'>='
else if (buf2 = '小于等于') then
str := str+'<=';
buf2 := LV_SQL.Items.Item[i].SubItems.Strings[2]; //通过查找值判断添加对应SQL语句
if(buf1 = '物料编号') then
str := str+''''+buf2+''''
else if (buf1 = '物料名称') then //字符串类型字段
str := str+''''+buf2+'''' //字符串字段两边需要加上'标志
else if (buf1 = '所在仓库') then //整数类型字段
str := str+''''+buf2+''''
else if (buf1 = '供应商') then //字符串类型字段
str := str+''''+buf2+'''' //字符串字段两边需要加上'标志
else if (buf1 = '型号') then
str := str+''''+buf2+''''; //字段两边也需要加上'标志
end;
end;
begin
SQL.Add('SELECT * FROM Material_Main WHERE '); //添加查询语句
SQL.Add(str); //添加查询条件语句
Open; //执行SQL语句,打开数据集
if recordcount=0 then
Messagedlg('没有找到你所需要的记录!',mtInformation,[mbOK],0)
else
materialquery.Close; //关闭数据记录查询窗口
end;
end;
end;
procedure Tmaterialquery.tiNewClick(Sender: TObject);
var {*****增加按钮单击事件*****}
SQL:TListItem;
begin
inherited;
if (CBE_Field.Text='')or(CB_Cond.Text='')or(edit1.Text='') then
begin
ShowMessage('查找字段、查找条件或查找值不能为空');
edit1.SetFocus; //焦点在查找值输入框
exit; //退出此过程
end;
SQL := LV_SQL.Items.Add; //增加一条查找字段、查找条件和查找值到ListView内
if (LV_SQL.Items.Count>1) then
begin
if (RG_Cond.ItemIndex=1) then
SQL.Caption := '或者' //增加查找逻辑关系值
else
SQL.Caption := '并且';
end;
SQL.SubItems.Add(CBE_Field.Text); //追加查找字段
SQL.SubItems.Add(CB_Cond.Text); //追加查找条件
SQL.SubItems.Add(edit1.Text); //追加查找值
lv_sql.setfocus;
end;
procedure Tmaterialquery.sb_delClick(Sender: TObject);
var {*****删除按钮单击事件*****}
No:integer;
begin
inherited;
No := LV_SQL.ItemIndex; //获得用户选择ListView的记录值
if lv_sql.Items.count=0 then
begin
showmessage('没有需要删除的记录');
end
else
LV_SQL.Items.Delete(No); //删除用户选择ListView记录
end;
procedure Tmaterialquery.Image1Click(Sender: TObject);
begin
inherited;
with dm.q_materialmanager do
begin
close;
sql.clear;
sql.add('select * from material_main');
//prepare;
open;
materialquery.Close;
end
end;
procedure Tmaterialquery.FormCreate(Sender: TObject);
begin
inherited;
{*****数据记录查询窗体创建事件*****}
LV_SQL.Items.Clear; //清除ListView对象所有列表项
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -