主题:查询问题请教
本人是刚开始学delphi,在开发以一各小程序时碰到以下问题,使用的数据库是
sql sever,建立了一个表,它包涵了多个字段,在实现查询功能的时候我在窗体上添加了如下组件,三个combobox,三个datasource 控件 和一个dbgrid 和一个speedbutton组件,另有一个与数据库联接的data 窗体,
现想实查询是,可以根据不同的查询条件,都能在dbgrid中显示要查询到的那条记录,
我写的程序代码是:
unit frmselect;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, Buttons, StdCtrls, DB, ADODB, DBTables;
type
Tselect = class(TForm)
Label1: TLabel;
Label2: TLabel;
SpeedButton1: TSpeedButton;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ComboBox1: TComboBox;
Label3: TLabel;
Label4: TLabel;
ComboBox2: TComboBox;
DataSource2: TDataSource;
DataSource3: TDataSource;
ComboBox3: TComboBox;
procedure SpeedButton1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
select: Tselect;
implementation
uses frmdata;
{$R *.dfm}
procedure Tselect.SpeedButton1Click(Sender: TObject);
var a:string;
b:string;
c:string;
begin
a:=Trim(combobox3.Text);
data.ADOQuery1.Close;
data.ADOQuery1.SQL.Clear;
data.ADOQuery1.SQL.Add('select * from 耗材领取表 where 所领工厂='''+a+'''');
data.ADOQuery1.Open;
dbgrid1.DataSource:=datasource1;
b:=Trim(combobox1.Text);
data.ADOQuery2.Close;
data.ADOQuery2.SQL.Clear;
data.ADOQuery2.SQL.Add('select * from 耗材领取表 where 耗材名称='''+b+'''');
data.ADOQuery2.Open;
dbgrid1.DataSource:=datasource2;
c:=Trim(Combobox2.Text);
data.ADOQuery3.Close;
data.ADOQuery3.SQL.Clear;
data.ADOQuery3.SQL.Add('select * from 耗材领取表 where 部门='''+c+'''');
data.ADOQuery3.Open;
Dbgrid1.datasource:=datasource3;
end;
procedure Tselect.FormShow(Sender: TObject);
begin
data.ADOQuery2.Open;
while not data.ADOQuery2.Eof do
begin
combobox1.Items.Add(data.ADOQuery2.Fields[0].asstring);
data.ADOQuery2.Next;
end;
data.ADOQuery3.Open;
while not data.ADOQuery3.Eof do
begin
combobox2.Items.Add(data.ADOQuery3.Fields[0].asstring);
data.ADOQuery3.next;
end;
data.ADOQuery1.Open;
while not data.ADOQuery1.Eof do
begin
combobox3.Items.Add(data.ADOQuery1.Fields[0].asstring);
data.ADOQuery1.Next;
end;
end;
end.
现只能实现选择耗材名称时能实现查询的效果,选择部门和工厂时,dbgrid中显示的总是空记录。
请教各位高手问题出在哪里,怎样才能实现,选择三者中的任何一个都能实现查询,或三个全选时能实现更具体的查询。
sql sever,建立了一个表,它包涵了多个字段,在实现查询功能的时候我在窗体上添加了如下组件,三个combobox,三个datasource 控件 和一个dbgrid 和一个speedbutton组件,另有一个与数据库联接的data 窗体,
现想实查询是,可以根据不同的查询条件,都能在dbgrid中显示要查询到的那条记录,
我写的程序代码是:
unit frmselect;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, Buttons, StdCtrls, DB, ADODB, DBTables;
type
Tselect = class(TForm)
Label1: TLabel;
Label2: TLabel;
SpeedButton1: TSpeedButton;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ComboBox1: TComboBox;
Label3: TLabel;
Label4: TLabel;
ComboBox2: TComboBox;
DataSource2: TDataSource;
DataSource3: TDataSource;
ComboBox3: TComboBox;
procedure SpeedButton1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
select: Tselect;
implementation
uses frmdata;
{$R *.dfm}
procedure Tselect.SpeedButton1Click(Sender: TObject);
var a:string;
b:string;
c:string;
begin
a:=Trim(combobox3.Text);
data.ADOQuery1.Close;
data.ADOQuery1.SQL.Clear;
data.ADOQuery1.SQL.Add('select * from 耗材领取表 where 所领工厂='''+a+'''');
data.ADOQuery1.Open;
dbgrid1.DataSource:=datasource1;
b:=Trim(combobox1.Text);
data.ADOQuery2.Close;
data.ADOQuery2.SQL.Clear;
data.ADOQuery2.SQL.Add('select * from 耗材领取表 where 耗材名称='''+b+'''');
data.ADOQuery2.Open;
dbgrid1.DataSource:=datasource2;
c:=Trim(Combobox2.Text);
data.ADOQuery3.Close;
data.ADOQuery3.SQL.Clear;
data.ADOQuery3.SQL.Add('select * from 耗材领取表 where 部门='''+c+'''');
data.ADOQuery3.Open;
Dbgrid1.datasource:=datasource3;
end;
procedure Tselect.FormShow(Sender: TObject);
begin
data.ADOQuery2.Open;
while not data.ADOQuery2.Eof do
begin
combobox1.Items.Add(data.ADOQuery2.Fields[0].asstring);
data.ADOQuery2.Next;
end;
data.ADOQuery3.Open;
while not data.ADOQuery3.Eof do
begin
combobox2.Items.Add(data.ADOQuery3.Fields[0].asstring);
data.ADOQuery3.next;
end;
data.ADOQuery1.Open;
while not data.ADOQuery1.Eof do
begin
combobox3.Items.Add(data.ADOQuery1.Fields[0].asstring);
data.ADOQuery1.Next;
end;
end;
end.
现只能实现选择耗材名称时能实现查询的效果,选择部门和工厂时,dbgrid中显示的总是空记录。
请教各位高手问题出在哪里,怎样才能实现,选择三者中的任何一个都能实现查询,或三个全选时能实现更具体的查询。