请教一个RecordSet的问题

honjo 2003-08-19 03:47:57
请问那位知道在VB中的RS中判断数据库中是否有某条记录而执行相关操作时,怎么去判断呢,我用的是
if MyRs.Eof then
...
else
...
endif
但是当数据库中没有符合条件的记录时会在(if MyRs.Eof then)出错,说对象或变量With块未设置,即MyRs没有记录集,但有时就不出错,不知是什么原因,不知那位高手能指点一二。
...全文
55 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wdwd05 2003-08-20
  • 打赏
  • 举报
回复
to: honjo(斌)
不错不错~~但好象和楼主的意思不太吻合,虽然里面包括,但不是简单明了,哈哈~

TO: 楼主
你是判断是否有数据集对吧~可以用很多种方法~
例如:if rs.bof and rs.eof then
msgbox "没有数据"
end if
或者
if rs.recordcount < 1 then
msgbox "没有数据"
end if
或者在SQL语句里面判断~就如 czwwh(江·沙·月) 说的
select count(*) from table where ...
honjo 2003-08-20
  • 打赏
  • 举报
回复
我的代码是这样的,请高手指正
If CheckSTORAGE(Left(ProdList.Text, InStr(ProdList, "|") - 1)) Then
'库存中Update
strSql = "Update T_PRODUCT_STORAGE Set sl=sl+" & txt(1) & ",je=je+" & txt(2) & " Where ProdId=" & Left(ProdList.Text, InStr(ProdList, "|") - 1) & " And State=0"
Else
'库存中Insert
strSql = "Insert Into T_PRODUCT_STORAGE (Whid,Pc,ProdId,sl,je,State) Values(" & Left(WhList.Text, InStr(WhList.Text, "|") - 1) & "," & txt(0) & "," & Left(ProdList.Text, InStr(ProdList, "|") - 1) & "," & txt(1) & "," & txt(2) & ",0)"
End If
Set MyRs = ExSQL(strSql, strMsg)

Private Function CheckSTORAGE(ByVal Pid As Integer) As Boolean
Dim MyRs As ADODB.Recordset, strSql As String
strSql = "Select Id From T_PRODUCT_STORAGE Where Id=" & Pid
Set MyRs = ExSQL(strSql)

If Not MyRs.EOF Then CheckSTORAGE = True

MyRs.Close
End Function

Public Function ConStr() As String
ConStr = "DSN=Sale;UID=sa;PWD="
End Function
'返回一个Recordset和执行相关操作
Public Function ExSQL(ByVal SQL As String, Optional strMsg As String) As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String

On Error GoTo ExecuteSQL_Error

sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConStr

If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
cnn.Execute SQL
strMsg = sTokens(0) & "操作成功"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
'rst.MoveLast 'get RecordCount
'If Not rst.EOF Then
Set ExSQL = rst
strMsg = "查询到" & rst.RecordCount & " 条记录 "
'Else
' strMsg = "无记录"
'End If
End If

ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function

ExecuteSQL_Error:
strMsg = "查询错误: " & Err.Description
Resume ExecuteSQL_Exit
End Function
honjo 2003-08-20
  • 打赏
  • 举报
回复
To:wdwd05
请问“虽然里面包括,但不是简单明了,哈哈”这句话是什么意思?
为什么说不是简单明了呢,请指正好吗?
wumy_ld 2003-08-19
  • 打赏
  • 举报
回复
前提是把连接对象的游标设为客户端:
con.CursorLocation = adUseClient
wumy_ld 2003-08-19
  • 打赏
  • 举报
回复
如果想知道有无记录,完全可以用rs.RecordCount
viena 2003-08-19
  • 打赏
  • 举报
回复
“最后一条记录满足条件,结果会怎样?”,不管是哪一条记录,都一样,如果找到,指针到那条记录,否则到文件尾(EOF,不知向任何记录)
viena 2003-08-19
  • 打赏
  • 举报
回复
if not(rs.eof and rs.bof) then
rs.movefirst
rs.find 查找条件(字符串)
if rs.eof then
msgbox "没有找到"
else
msgbox "找到"
end if
end if

楼主写endif是错的,应该是end if
czwwh 2003-08-19
  • 打赏
  • 举报
回复
楼上的方法我也想过,但是如果恰好是最后一条记录满足条件,结果会怎样?
我是用 select count(*) from table where ... 来判断有没有记录
viena 2003-08-19
  • 打赏
  • 举报
回复
“对象或变量With块未设置”不是没有记录集,可能是书写有误,检查一下你的程序,能贴出来吗?
没有符合条件的记录时,MyRs.Eof=true,不会出错的。楼上说的find方法是可以的,但前提是有记录,否则rs.movefirst会出错
miranda516 2003-08-19
  • 打赏
  • 举报
回复

if not(rs.eof and rs.bof) then
rs.movefirst
Do While Not snTemp.EOF
执行语句
rs.movenext
Loop
end if
victorycyz 2003-08-19
  • 打赏
  • 举报
回复
判断是否有某条记录,用find方法。

rs.movefirst
rs.find 查找条件
if rs.eof then
msgbox "没有找到"
else
msgbox "找到"
end if
baiyga 2003-08-19
  • 打赏
  • 举报
回复
if rs.eof then

有记录的情况
else

.
.
.
end if

1,216

社区成员

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

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