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

发布于 2024-10-11 11:06:10 字数 1078 浏览 2 评论 0原文

这一天我绞尽脑汁解决这个问题......我用谷歌搜索了解决方案,但没有一个解决我的问题......

代码是这样的:

    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

The sql statements is returned null recordset ..when i运行代码...它总是转到“else”条件,即MsgBox“有记录”行

我尝试更改该行:If IsNull(Record)然后

If IsNull(Record.Fields(0)。 Value) 然后

它会抛出这样的错误:-

error: 要么 BOF 或 EOF 为 true,要么当前记录已被删除。请求的操作需要当前记录。

我已检查 http://support.microsoft.com/kb/304267 并使用 eof 和bof 到条件...n仍然得到相同的错误..

请任何人帮助我...

It's been a day I've cracked my head to solve this....I've googled for solutions but none of it resolve my issue...

The code is like this:

    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

The sql statement is returning null recordset ..when i run the code...it always go to the "else" condition which is the line MsgBox "There are records"

I've tried change the line : If IsNull(Record) Then

to

If IsNull(Record.Fields(0).Value) Then

but then it throws an error like this:-

error: Either BOF or EOF is true, or the current record has been deleted. Requested operation requires a current record.

I've checked http://support.microsoft.com/kb/304267 and use eof and bof to the condition...n still get the same error..

please anyone help me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仲春光 2024-10-18 11:06:10

我会使用这样的东西:

' 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

I would use something like this:

' 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
糖果控 2024-10-18 11:06:10

将此更改

If IsNull(Record) Then

If Record.RecordCount = 0 Then

Change this

If IsNull(Record) Then

to

If Record.RecordCount = 0 Then
转瞬即逝 2024-10-18 11:06:10

我认为您可以测试如果不是Record.Eof

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

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

I think you can test for if not Record.Eof.

If I recall correctly (it's been a long time), it only works with one type of cursor, I think it should be adUseServer. (EDIT No, it's actually RecordCount which has this problem)

I'll try and dig out some old code to check.

我一直都在从未离去 2024-10-18 11:06:10

感谢大家的回复:D ...无论如何稍后都会测试它...在我发布此回复时我还没有测试您的所有建议...我测试了这个: –

如果 Record.BOF 和 Record.EOF然后

这有效...

Thanks for the replies guys :D ...will test it later anyway...I havent's tested all of your suggestions at the time I'm posting this reply...I tested this: –

If Record.BOF And Record.EOF Then

and this works...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文