当查询结果为空时出错,应怎么办?

szheto 2003-09-10 12:01:17
当查询结果为空时出错,应怎么办?
...全文
118 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
haipingma 2003-10-14
  • 打赏
  • 举报
回复
查询时:
select isnull(a,'')...
sunnyBelt 2003-10-14
  • 打赏
  • 举报
回复
你要加个判断语句进来
Dim str As String
Dim rs As ADODB.Recordset
str = "select sum(inp_money) from input where pur_date>= '" & DTPicker1.Value & "' and pur_date<= '" & DTPicker2.Value & "'"

Set rs = ExecuteSQL(str)
if not rs.eof then
Label4.Caption = rs.Fields(0).Value
else
msgbox "没有相关记录"
end if
Jackile 2003-10-14
  • 打赏
  • 举报
回复
严格来说:未得到查询结果
if rst.eof then
msgbox"未找到相关记录"
endif
如果看是否为空,用IsNULL()函数
if IsNull(rst.fields(0)) then
text1.text=""
endif

主要是空值引起的错误

楼主,我给了两种方法,你都可以试一下!
Gelim 2003-10-14
  • 打赏
  • 举报
回复
如:str = "select sum(inp_money) from input where pur_date>= '" & DTPicker1.Value & "' and pur_date<= '" & DTPicker2.Value & "'"
set rs=executesql(str)

if rs.bof then
msgbox "结果为空"
else
text1.text=rs.fields(0).value
end if


mfcprogrammer 2003-10-13
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2351/2351901.xml?temp=.7641107
mfcprogrammer 2003-10-13
  • 打赏
  • 举报
回复
Label4.Caption = rs.Fields(0).Value & ""

chenyu5188 2003-09-11
  • 打赏
  • 举报
回复
Dim str As String
Dim rs As New ADODB.Recordset
str = "select sum(inp_money) from input where pur_date>= '" & DTPicker1.Value & "' and pur_date<= '" & DTPicker2.Value & "'"

Set rs = ExecuteSQL(str)

Label4.Caption = rs.Fields(0).Value & ""

fly_rain 2003-09-11
  • 打赏
  • 举报
回复
Dim str As String
Dim rs As ADODB.Recordset
str = "select sum(isnull(inp_money,0)) from input where pur_date>= '" & DTPicker1.Value & "' and pur_date<= '" & DTPicker2.Value & "'"

Set rs = ExecuteSQL(str)
if not(rs.eof and rs.bof) then
if trim(cstr(rs.fields(0)))<>"" then
Label4.Caption = cstr(rs.Fields(0)).Value
end if
endif

你这样试试
找寻蓝色的海 2003-09-11
  • 打赏
  • 举报
回复
ExecuteSQL() 函数是什么类型?我想错误应该是在你提供的代码之外
如果要是在你提供的代码内的话问题应早解决了!
把你的ExecuteSQL()函数定义拿出来看看?
liul17 2003-09-11
  • 打赏
  • 举报
回复
再补一下:
Label4.Caption = rs.Fields(0).Value
改为

if isnull(rs.Fields(0).Value) then
label4.caption = ""
else
Label4.Caption = rs.Fields(0).Value
endif
liul17 2003-09-11
  • 打赏
  • 举报
回复
改为:
Dim str As String
Dim rs As new ADODB.Recordset '要new 的
str = "select sum(inp_money) from input where pur_date>= '" & DTPicker1.Value & "' and pur_date<= '" & DTPicker2.Value & "'"

Set rs = ExecuteSQL(str)

Label4.Caption = rs.Fields(0).Value

liul17 2003-09-11
  • 打赏
  • 举报
回复
改为:
Dim str As String
Dim rs As new ADODB.Recordset '要new 的
str = "select sum(inp_money) from input where pur_date>= '" & DTPicker1.Value & "' and pur_date<= '" & DTPicker2.Value & "'"

Set rs = ExecuteSQL(str)

Label4.Caption = rs.Fields(0).Value

newste 2003-09-11
  • 打赏
  • 举报
回复
我的代码就在上面.
重复一次:

Dim str As String
Dim rs As ADODB.Recordset
str = "select sum(inp_money) from input where pur_date>= '" & DTPicker1.Value & "' and pur_date<= '" & DTPicker2.Value & "'"

Set rs = ExecuteSQL(str)
If not isnull(rs.fields(0)) then
Label4.Caption = rs.Fields(0).Value
endif
BBp319 2003-09-11
  • 打赏
  • 举报
回复
是用ADO方事吗?用ecordCount判断就行。
Dim rs2 As Recordset

Set rs2 = New ADODB.Recordset
strsql = "select * from DATA Where"
rs2.Open strsql, dbcn, adOpenStatic, adLockOptimistic
If rs2.RecordCount > 0 Then
rs2.Fields(0) = data2
rs2.Fields(1)=data1
rs2.Update
Else
With rs2
.AddNew
.Fields(0) = data0
.Fields(1) = data1
.Update
End With
End If
szheto 2003-09-10
  • 打赏
  • 举报
回复
李先生的答案也是行不通!!
还有人回答我吗?
szheto 2003-09-10
  • 打赏
  • 举报
回复
错误信息为"对象变量或with块变量未设置"
我试过:
if rs.eof or rs.bof

if rs.recordcount<=0

都不行.
lihonggen0 2003-09-10
  • 打赏
  • 举报
回复
或者:
text1.text=rs.fields(0).value & ""
lihonggen0 2003-09-10
  • 打赏
  • 举报
回复
If not isnull(rs.fields(0)) then
text1.text=rs.fields(0).value
endif
szheto 2003-09-10
  • 打赏
  • 举报
回复
我试过了,还是不行!
Hbtang1980 2003-09-10
  • 打赏
  • 举报
回复
你看看我前面说的
那样完全可以避免
我已经试过了
加载更多回复(19)

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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