如何把两个TADOQUER表查询的内容显示到一个TADOQUER里或GRID里 (续;急等待)

BarryW 2008-05-01 07:17:50
一个AdoQuery1 查询出了内容,并显示在GRID1里,
一个AdoQuery2 查询出了内容也显示在GRID2里;

通过Tedit控件输入方式作为两个表里都有的字段;就是在TEDIT里输入的内空代表在
AdoQudoquery1跟AdoQuery2表里都有这个字段;
1.TEDIT是我输入两个表字段名,我的意思通过TEDIT输入的内容就为两
个表里有这个字段来查询出相同的内容;而不是内容条件;
2.在表AdoQuery2有的字段在AdoQuery1里都有,查询出
AdoQuery3的内容字段以AdoQuery2为依据;也就是把AdoQuery2表字段
TEDIT的内空跟在表AdoQuery1字字段TEDIT相同内空查找出来;

问题:要把在AdoQuery2表字段(Tedit)里的内容跟AdoQuery1表字段(Tedit)里的内容一样的数据查询到另一个AdoQuery3或GRID3里,应如何实现呢?

如下:
ADOQUERY1查询并可显示在GRID1里字段、内容:
prd_no, name, spc, knd, ut,。。。
002 B02 23*3 4 KG
004 KHL M*23 2 M

ADOQUERY2查询并可显示在GRID2里字段、内容(此表字段是不固定):
prd_no, name, spc
001 B01 20*2
002 B02 23*3
003 B03 Y*Y7
004 KHL M*23
ADOQUERY2查询并显示在GRID2里字段是不固定的;

现在我在edit里输入"prd_no", 点BUTTON后就在上面两查出的表里按prd_no来对应,把ADOQUERY2
在ADOQUERY1里有的内容查出来显示到ADOQUERY3或GRID3里得到结果:
prd_no, name, spc
002 B02 23*3
004 KHL M*23




...全文
377 65 打赏 收藏 转发到动态 举报
写回复
用AI写文章
65 条回复
切换为时间正序
请发表友善的回复…
发表回复
BarryW 2008-05-03
  • 打赏
  • 举报
回复
我汗啊,查了半天还是没有查出这个循环错误在哪里?

我也不想啊
GDTOPONE 2008-05-03
  • 打赏
  • 举报
回复
不要永远活在别人代码的阴影之下.
GDTOPONE 2008-05-03
  • 打赏
  • 举报
回复
楼主去看看其它的贴,那有这贴这么的长,没有人会帮你全写代码的,关键还是要靠自已,我提供的代码已经足够你完成任务了,感觉楼主有很多最基本的代码都不会,要多学习再编程.
BarryW 2008-05-03
  • 打赏
  • 举报
回复
“for i :=0 to Fields.Count-1 do
StringGrid1.Cells[i,0]:='a'+inttostr(i);

while not eof do begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
for i :=0 to StringGrid1.ColCount-1 do”


把上面的“while not eof do begin”去掉就可以过滤重复的数据出来了,

但是还有一点小问题:
点第一下Button事件可以出来,再接着点就什么都不出来了;

BarryW 2008-05-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 blazingfire 的回复:]
好象不难吧?!先给个法子先哦!
ADOQuery1指向的表假设为T1
ADOQuery2指向的表假设为T2

//Edit中的字段再T1, T2表中都存在时才执行查询
if (ADOQuery1.FindField(Edit1.Text) <> nil) and
(ADOQuery2.FindField(Edit1.Text) <> nil) then
with ADOQuery3 do
begin
Close;
SQL.Text := Format('select distinct T2.* from T2,T1 where T1.%s=T2.%s', [Edit1.Text, Edit1.Text…
[/Quote]


您好,我就是不知道如何指向;您可以按我的意思上面两个ADOQuery1、ADOQuery2写个完整如何指向变量T1、T2吗,谢谢了!
BarryW 2008-05-02
  • 打赏
  • 举报
回复
zxf_feng

基本上就是这样子
现在我就是没有办法adoquery1、adouery2来对比;
是否可以设定两变量形式来对比呢
阿三 2008-05-02
  • 打赏
  • 举报
回复
1.要将一个EXCEL文件的资料导入到一个数据库中的表去.
2.导入时要检查重复值,重复的在GRID3中显示出来,不重复的就导入.
3.EXCEL文件的字段名不是固定的,所以要用一个EDIT控件供输入对比字段.例如:数据库中的表是X_A这个字段,当在
EDIT控件中输入VL,也就是说用EXCEL文件中的VL字段与数据库表中的X_A作对比.

你的问题目就分这么几步吧.我的相法是这样
1.你先把Excel文件的资料导到一个临时表中,这个根把你Excel文件中的数据可以办到的
2.你的adoquery1是查询一个表,adouery2也就是取excel文件中的数据,你可以查询1中的临时表
3.然后根据你的edit中输入的内容来进行相关的重复值的判断,得到重复的显示在dbgrid中.
大体就这样吧,不知道我理解的对不对
BarryW 2008-05-02
  • 打赏
  • 举报
回复
上面显示的没有问题了,但是把EXCEL里QUERY2源里的数据都抓取过来了
BarryW 2008-05-02
  • 打赏
  • 举报
回复
2.stringGrid1显示出来的数据是从第五行开始显示,前面五行为空白,
但是我看上面的也没有看明白写从哪一行开始显示;
---->"StringGrid1.ColCount:=0;{这里默认为五列}
StringGrid1.RowCount:=0;{这里默认为五行}"

现在就差stringGrid1显示问题了:
1.stringGrid1前五行显示为空白,这个没有搞懂哪里可以设定从第一行开始就显示数据;
2.列从A0就开始显示数据,什么方法可以从A1列开始显示数据;


上面两上解决了就终于可以结案了;

GDTOPONE 2008-05-02
  • 打赏
  • 举报
回复
送上最后一段:

procedure TForm1.Button4Click(Sender: TObject);
var
s:string;
begin
if trim(edit1.Text)<>'' then begin
if adoquery1.FieldList.IndexOf(edit1.Text)>=0 then begin {-如果ADOQUERY1中有edit1中输入的字段-}
{---提取adoquery1的SQL语-----}
s := UpperCase(adoquery1.sql.Text);
if Pos('WHERE',s)>0 then
s := Copy(s,1,Pos('WHERE',s)-1) else
if Pos('ORDER',s)>0 then
begin
s := Copy(s,1,Pos('ORDER',s)-1);
end;

StringGrid1.ColCount:=0;{这里默认为五列}
StringGrid1.RowCount:=0;{这里默认为五行}

with adoquery2 do begin
while not eof do begin
adolook.Close;
adolook.SQL.Text:=s+' where '+trim(edit1.Text)+'='+QuotedStr(adoquery2.FieldValues[trim(edit1.Text)]);
adolook.open;
if not adolook.IsEmpty then begin

{重复时写StringGrid用作显示}
StringGrid1.ColCount:=Fields.Count;
{--标题只是举例你看--}
for i :=0 to Fields.Count-1 do
StringGrid1.Cells[i,0]:='a'+inttostr(i);

while not eof do begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
for i :=0 to StringGrid1.ColCount-1 do
StringGrid1.Cells[i,StringGrid1.RowCount-1]:=Fields[I].AsString;
next;
end;
end else begin

{导入的操作}

end;
next;
end;

end;
end;
end;
end;


GDTOPONE 2008-05-02
  • 打赏
  • 举报
回复
下班了,你慢慢调试,祝你好运!
GDTOPONE 2008-05-02
  • 打赏
  • 举报
回复
2.stringGrid1显示出来的数据是从第五行开始显示,前面五行为空白,
但是我看上面的也没有看明白写从哪一行开始显示;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
with adoquery1 do begin
StringGrid1.ColCount:=0;{这里默认为五列}
StringGrid1.RowCount:=0;{这里默认为五行}
close;
sql.Text:='select x_a,x_b,x_c from x_eng where x_c=''建丰一组''';
open;
if not IsEmpty then begin
StringGrid1.ColCount:=Fields.Count;
{--标题--}
for i :=0 to Fields.Count-1 do
StringGrid1.Cells[i,0]:='a'+inttostr(i);

{--数据集内容--}
while not eof do begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
for i :=0 to StringGrid1.ColCount-1 do
StringGrid1.Cells[i,StringGrid1.RowCount-1]:=Fields[I].AsString;
next;
end;
end;
end;
end;


GDTOPONE 2008-05-02
  • 打赏
  • 举报
回复

procedure TForm1.Button4Click(Sender: TObject);
var
s:string;
begin
if trim(edit1.Text)<>'' then begin
if adoquery1.FieldList.IndexOf(edit1.Text)>=0 then begin {-如果ADOQUERY1中有edit1中输入的字段-}
{---提取adoquery1的SQL语-----}
s := UpperCase(adoquery1.sql.Text);
if Pos('WHERE',s)>0 then
s := Copy(s,1,Pos('WHERE',s)-1) else
if Pos('ORDER',s)>0 then
begin
s := Copy(s,1,Pos('ORDER',s)-1);
end;

with adoquery2 do begin
while not eof do begin
adolook.Close;
adolook.SQL.Text:=s+' where '+trim(edit1.Text)+'='+QuotedStr(adoquery2.FieldValues[trim(edit1.Text)]);
adolook.open;
if not adolook.IsEmpty then begin

{重复时写StringGrid用作显示}
StringGrid1.ColCount:=Fields.Count;
{--标题只是举例你看--}
for i :=0 to Fields.Count-1 do
StringGrid1.Cells[i,0]:='a'+inttostr(i);

while not eof do begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
for i :=0 to StringGrid1.ColCount-1 do
StringGrid1.Cells[i,StringGrid1.RowCount-1]:=Fields[I].AsString;
next;
end;
end else begin

{导入的操作}

end;
next;
end;

end;
end;
end;
end;


BarryW 2008-05-02
  • 打赏
  • 举报
回复
procedure TForm1.Button4Click(Sender: TObject);
var
s:string;
i,j:integer;
begin
J := 0;
if trim(edit1.Text) <>'' then begin
if adoquery1.FieldList.IndexOf(edit1.Text)>=0 then begin {-如果ADOQUERY1中有edit1中输入的字段-}
{---提取adoquery1的SQL语-----}
s := UpperCase(adoquery1.Text);
if Pos('WHERE',s)>0 then
s := Copy(s,1,Pos('WHERE',s)-1) else
if Pos('ORDER',s)>0 then
begin
s := Copy(s,1,Pos('ORDER',s)-1);
end;

with adoquery2 do begin
while not eof do begin
adolook.Close;
adolook.SQL.Text:=s+' where 'trim(edit1.Text)+'='+QuotedStr(adoquery2.FieldValues[trim(edit1.Text)]);
adolook.open;
if not adolook.IsEmpty then begin
with adolook do begin
StringGrid1.ColCount:=adolook.Fields.Count;
while not Eof do begin
stringGrid1.RowCount:=StringGrid1.RowCount+1;
j:=StringGrid1.RowCount;{忘当了是这样还是j:=StringGrid1.RowCount-1;}
for i:=0 to StringGrid1.ColCount-1 do
StringGrid1.Cells[I,J]:=Fields[I].AsString;
adolook.Next;
end;
end;
end else begin

{导入的操作}

end;
next;
end;

end;
end;
end;
end;


是可以显示出重复的到stringGrid1里了,但是还是搞不明白:
1.stringGrid1的列头是空的;
2.stringGrid1显示出来的数据是从第五行开始显示,前面五行为空白,
但是我看上面的也没有看明白写从哪一行开始显示;
3.stringGrid1里显示的数据列是从最左边的项开始显示数据;



GDTOPONE 2008-05-02
  • 打赏
  • 举报
回复
给你参考一下,随意写的,因为要加班,所以没太多时间帮.

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
with adoquery1 do begin
StringGrid1.ColCount:=0;
StringGrid1.RowCount:=0;
close;
sql.Text:='select x_a,x_b,x_c from x_eng where x_c=''建丰一组''';
open;
if not IsEmpty then begin
StringGrid1.ColCount:=Fields.Count;
{--标题--}
for i :=0 to Fields.Count-1 do
StringGrid1.Cells[i,0]:='a'+inttostr(i);

{--数据集内容--}
while not eof do begin
StringGrid1.RowCount:=StringGrid1.RowCount+1;
for i :=0 to StringGrid1.ColCount-1 do
StringGrid1.Cells[i,StringGrid1.RowCount-1]:=Fields[I].AsString;
next;
end;
end;
end;
end;
BarryW 2008-05-02
  • 打赏
  • 举报
回复
还有StringGrid1的列名也不能跟随adolook显示相对应的列名;
BarryW 2008-05-02
  • 打赏
  • 举报
回复
加上去一条语句也显示不出来了,StringGrid1也从来没有用过;

还是不行;晕死我了;
BarryW 2008-05-02
  • 打赏
  • 举报
回复
这论坛是有点问题,发的贴子有时是看不见!
BarryW 2008-05-02
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zxf_feng 的回复:]
你的贴子发重了
[/Quote]

谢谢您帮我ding一下
GDTOPONE 2008-05-02
  • 打赏
  • 举报
回复
从来没试过搞过一个这么长的贴.头一回!
加载更多回复(44)

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧