读书人

为何找不到这个字段?解决方案

发布时间: 2012-03-26 15:46:56 作者: rapoo

为何找不到这个字段?

procedure Tryxxfrm.Button1Click(Sender: TObject);
var
ygbh,ygxm,sqltext1,sqltext2: string;
begin
if(combobox1.text='按编号查询')then
begin
ygbh:=edit1.text;
if(radiobutton1.Checked=true)then
with datamodulemis.adoqueryyg do
begin
close;
sql.Clear;
sqltext1:='select ygbh as '+'''员工编号'+''''+',ygxm as'+'''员工姓名'+''''+',bmm as'+'''部门'+''''+',zwmc as'+'''职务'+''''+',lxdh as'+'''联系电话'+''''+',dzyj as'+'''电子邮件'+''''+',yg.bz as '+'''备注'+'''';
sqltext2:='from yg ,zw ,bm where ygbh='+''''+ygbh+''''+'and yg.bmbh=bm.bmbh and yg.zwbh=zw.zwbh';
sql.Add(sqltext1+sqltext2);
open;
if recordcount<>1 then
showmessage('没有这个编号的员工!');
end

else
with datamodulemis.adoqueryyg do
begin
close;
sql.Clear;
sqltext1:='select ygbh as '+'''员工编号'+''''+',ygxm as'+'''员工姓名'+''''+',bmm as'+'''部门'+''''+',zwmc as'+'''职务'+''''+',lxdh as'+'''联系电话'+''''+',dzyj as'+'''电子邮件'+''''+',yg.bz as '+'''备注'+'''';
sqltext2:='from yg,zw ,bm where ygbh like'+QuotedStr('%'+ygbh+'%')+'and yg.bmbh=bm.bmbh and yg.zwbh=zw.zwbh';
sql.Add(sqltext1+sqltext2);
open;
if recordcount=0 then
showmessage('没有这个编号的员工!');
end ;

end

else
begin
ygxm:=edit1.text;
if(radiobutton1.Checked=true)then
with datamodulemis.adoqueryyg do
begin
close;
sql.Clear;
sqltext1:='select ygbh as '+'''员工编号'+''''+',ygxm as'+'''员工姓名'+''''+',bmm as'+'''部门'+''''+',zwmc as'+'''职务'+''''+',lxdh as'+'''联系电话'+''''+',dzyj as'+'''电子邮件'+''''+',yg.bz as '+'''备注'+'''';
sqltext2:= 'from yg ,zw ,bm where ygxm='+''''+ygxm+''''+'and yg.bmbh=bm.bmbh and yg.zwbh=zw.zwbh';
sql.Add(sqltext1+sqltext2);
open;
if recordcount<>1 then
showmessage('没有这个名字的员工!');
end
else


with datamodulemis.adoqueryyg do
begin
close;
sql.Clear;
sqltext1:='select ygbh as '+'''用户编号'+''''+',ygxm as'+'''用户姓名'+''''+',bmm as'+'''部门'+''''+',zwmc as'+'''职务'+''''+',lxdh as'+'''联系电话'+''''+',dzyj as'+'''电子邮件'+''''+',yg.bz as '+'''备注'+'''';
sqltext2:= 'from yg,zw ,bm where ygxm like'+QuotedStr('%'+ygxm+'%')+'and yg.bmbh=bm.bmbh and yg.zwbh=zw.zwbh';
sql.Add(sqltext1+sqltext2);

open;
if recordcount=0 then
showmessage('没有这个名字的员工!');
end ;
end;
edit2.Text:= datamodulemis.ADOQueryyg.FieldByName('ygbh').asstring;//问题出在这里
我只是想把表格里的一条记录的每个字段逐个读取到edit里,但是它提示“field 'ygbh' not found”其实在dbgrid里我已经查出了结果,为何它还说找不到呢?



[解决办法]
注意你这句ygbh as '+ ' ' '用户编号,你已经把Ygbh重命名为“用户编号”了,你怎么还会找到呢,试试下面这句


edit2.Text:= datamodulemis.ADOQueryyg.FieldByName( '用户编号 ').asstring
[解决办法]
字段都用英文的
可能服务器就是英文版或繁体版的,容易出问题
[解决办法]
"hys_427"为正解,也可:
edit2.Text:= datamodulemis.ADOQueryyg.Fields[0].asstring;
//这种方式只需知道字段的位置,不需知道字段的名称,不过存在一定弊端

读书人网 >.NET

热点推荐