无法在VB6中处理null或空记录集

时间:2011-01-13 06:14:41

标签: vb6 null

这是我解决这个问题的一天......我已经搜索了解决方案,但没有解决我的问题......

代码是这样的:

    Private Sub guh()
Dim oConn As Connection
Dim Record As Recordset
Dim SqlStr As String

SqlStr = "select * from dbo.Msg_History where Client_ID='2' AND Update_Msg='4'"
Set oConn = New Connection

With oConn
.CursorLocation = adUseClient
.CommandTimeout = 0
.Open "Provider=SQLOLEDB;Server=127.0.0.1;Initial Catalog=Table_Msg;UID=Admin;PWD="

End With

Set Record = oConn.Execute(SqlStr)

If IsNull(Record) Then
    MsgBox "There are no records"

    Else
    MsgBox "There are records"

End If

oConn.Close
Set oConn = Nothing
End Sub

sql语句返回空记录集..当我运行代码时...它总是转到“else”条件,这是行MsgBox“有记录”

我尝试过更改行:如果是IsNull(Record)那么

如果IsNull(Record.Fields(0).Value)那么

然后它会抛出这样的错误: -

错误:BOF或EOF为真,或者当前记录已被删除。请求的操作需要当前记录。

我已经检查http://support.microsoft.com/kb/304267并使用eof和bof来处理这个问题... n仍然会收到相同的错误..

请有人帮助我......

4 个答案:

答案 0 :(得分:3)

更改此

If IsNull(Record) Then

If Record.RecordCount = 0 Then

答案 1 :(得分:3)

我会用这样的东西:

' returns true if there is non empty recordset
Function isRSExists(rs) AS boolean
  ' has to exists as object
  If Not rs Is Nothing Then
    ' has to be opened with recordset (could be empty)
    If rs.State > 0 Then
      ' has to have some records
      If Not rs.EOF Then
         isRSExists = true
      End If
    End If
  End If
End Function

答案 2 :(得分:1)

我认为你可以测试if not Record.Eof

如果我没记错(这已经很久了),它只适用于一种类型的光标,我认为它应该是adUseServer。 (编辑 不,它实际上是具有此问题的RecordCount

我会尝试挖出一些旧代码来检查。

答案 3 :(得分:0)

感谢各位回复人员:D ...无论如何都会在以后测试...我在发布此回复时没有测试过你的所有建议......我测试了这个: -

如果Record.BOF和Record.EOF那么

这有效......