有这样一段程序,其中目的之一是判断SQL语句中的一个字段值是否为‘002’,并给出用户提示信息,但运行时,出现错误信息:Access violation at address 000xxxx
源码如下:
.
.
.
with query1 do
begin
close;
sql.clear;
vstr:='select a.code,b.mpid from db1 a left join db2 b on a.code=b.code'
sql.add(vstr);
open;
first;
do while not eof do
begin
AdoDataSource1.insert;
AdoDataSource1Code.Value:=Fields[0].Value;
If Fields[1].value='002' then
Application.MessageBox('存在002类产品','信息提示',0);
next;
end;
end;
.
.
.
源码如下:
.
.
.
with query1 do
begin
close;
sql.clear;
vstr:='select a.code,b.mpid from db1 a left join db2 b on a.code=b.code'
sql.add(vstr);
open;
first;
do while not eof do
begin
AdoDataSource1.insert;
AdoDataSource1Code.Value:=Fields[0].Value;
If Fields[1].value='002' then
Application.MessageBox('存在002类产品','信息提示',0);
next;
end;
end;
.
.
.
一般是數據驅動有問題了, 我覺得!
如果是找不到數據庫之類, 只會簡單報個錯!
with query1 do
begin
close;
sql.clear;
vstr:='select a.code,b.mpid from db1 a left join db2 b on a.code=b.code'
sql.add(vstr);
open;
first;
end 先执行
Application.MessageBox('存在002类产品','信息提示',0);关键是增加了这一句,就出错了。
如果没这句话,不会有报错,运行正常哦!
with query1 do
begin
close;
sql.clear;
vstr:='select a.code,b.mpid from db1 a left join db2 b on a.code=b.code'
sql.add(vstr);
open;
first;
while not eof do // 原来多了一个do
begin
AdoDataSource1.insert;
AdoDataSource1Code.Value:=Fields[0].Value;
If Fields[1].value='002' then
Application.MessageBox('存在002类产品','信息提示',0);
next;
end;
end;
Fields[1].asstring 试试简单方法:with query1 do
begin
close;
sql.clear;
vstr:='select a.code,b.mpid from db1 a left join db2 b on a.code=b.code where b.mpid=002'
sql.add(vstr);
open;
if not eof then
begin
Application.MessageBox('存在002类产品','信息提示',0);
end;
Close;
end;
do while not eof do //存在笔误其实,我在这个循环中,要完成对查询结果的插入到另外一个表的同时,需要检查字段MPID的内容
如果没这个if语句,程序运行是正常的。
出现错误信息:Access violation at address 000xxxx
改成:
if query1.FieleByName('mpid').asstring='002'
不知道好不好?