如何访问记录集中的值

发布于 2024-11-07 06:44:45 字数 196 浏览 3 评论 0原文

以这段代码为例:

sSQL = "select CtyMarket from Market where Country = '" & Country.Value & "'"
Set rec = CurrentDb.OpenRecordset(sSQL)

该语句可以返回多个值。我如何访问这些值?

Take for example this code:

sSQL = "select CtyMarket from Market where Country = '" & Country.Value & "'"
Set rec = CurrentDb.OpenRecordset(sSQL)

This statement can return more than one value. How can I access those values?

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

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

发布评论

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

评论(2

欲拥i 2024-11-14 06:44:45

好吧,为了获取所有值,您可以浏览记录集中的字段和记录。它可能看起来像这样:

'You'll need to declare a new variable
Dim i as long

If rec.EOF and rec.BOF then
Else
    do while not rec.EOF
        for i = 0 to rec.fields.count - 1
            debug.print rec.fields(i).value
        next i
        rec.movenext
    loop
endif

获取数据的其他方法是使用记录集对象的 getrows 和/或 getstring 方法,但我不记得这些方法是否可用于 DAO 记录集。您还可以为特定字段上的特定值设置过滤器等

well, in order to get all the values you could browse both fields and records in your recordset. It could look like that:

'You'll need to declare a new variable
Dim i as long

If rec.EOF and rec.BOF then
Else
    do while not rec.EOF
        for i = 0 to rec.fields.count - 1
            debug.print rec.fields(i).value
        next i
        rec.movenext
    loop
endif

Other ways to get your data would be to use the getrows and\or getstring metyhods of the recordset object, but I do not remember if these are available with DAO recordsets. You could also set a filter for a specific value on a specific field, etc

定格我的天空 2024-11-14 06:44:45

我使用此函数在读取记录集时不关心 NULL 值:

Public Function toStr(pVar_In As Variant) As String
    On Error Resume Next
    toStr = CStr(pVar_In)
End Function

永远不要相信 rec.recordcount 的确切数量,但 rec.RecordCount>0 是安全的。这就是为什么在使用记录集时绝不使用for循环。如果您想知道记录计数,您首先必须做的是 rec.movelast 然后是 rec.movefirst

我知道有两种不同的方法:

While not rec.eof
    msgbox toStr(rec!CtyMarket)
    rec.moveNext
Wend

While not rec.eof
    msgbox toStr(rec.fields("CtyMarket").value)
    rec.moveNext
Wend

I use this function to not care about NULL values when reading recordsets:

Public Function toStr(pVar_In As Variant) As String
    On Error Resume Next
    toStr = CStr(pVar_In)
End Function

Never trust the exact amount of rec.recordcount but rec.RecordCount>0 is safe. That's why you should never use a for loop when using a recordset. If you'd like to know the recordcount anyway what you have to do first is rec.movelast and then rec.movefirst

There are two different ways that I know of:

While not rec.eof
    msgbox toStr(rec!CtyMarket)
    rec.moveNext
Wend

or

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