vba 如何从 ADO 查询中检查记录集的所有字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5402131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:16:23  来源:igfitidea点击:

How to check all fields of the recordset from ADO query?

vbaado

提问by lamwaiman1988

I would like to see if there is a command to show the whole record(row) at once. By now I only find method to show individual columns. I am using a ADO connection to the ms access's mdb. Thanks. By the way, I don't know how can I print a message in MS Access's VB form.......does vb provide a console to show that? Debug.Print don't give me anything, I only success with MsgBox...

我想看看是否有一个命令可以一次显示整个记录(行)。到目前为止,我只找到显示单个列的方法。我正在使用 ADO 连接到 ms access 的 mdb。谢谢。顺便说一句,我不知道如何在 MS Access 的 VB 表单中打印消息....... vb 是否提供了一个控制台来显示?Debug.Print 不要给我任何东西,我只成功使用 MsgBox ...

   With cmdCommand
    .ActiveConnection = conConnection
    .CommandText = "SELECT * from tableA"
    .CommandType = adCmdText
   End With

   With rstRecordSet
    .CursorType = adOpenStatic
    .CursorLocation = adUseClient
    .LockType = adLockReadOnly
    .Open cmdCommand
   End With

   If rstRecordSet.EOF = False Then
        rstRecordSet.MoveFirst
        Do

            MsgBox rstRecordSet.Fields(0) & " " & rstRecordSet.Fields(1)

            rstRecordSet.MoveNext
        Loop Until rstRecordSet.EOF = True
   End If

回答by RolandTumble

First off, Debug.Printprints to the Immediate Window in the VB[A] Editor. If it's not showing, press Ctrl-G.

首先,Debug.Print打印到 VB[A] 编辑器中的立即窗口。如果未显示,请按 Ctrl-G。

Second, there is no single command to show the whole record, you'll have to assemble it the way that Xavinou does in his (her?) answer. Here's the VB syntax, ignoring recordset creation & EOF check (Note that I've declared the variables--you are using Option Explicit, yes?):

其次,没有单个命令可以显示整个记录,您必须按照 Xavinou 在他(她?)的回答中的方式进行组合。这是 VB 语法,忽略记录集创建和 EOF 检查(请注意,我已经声明了变量——您正在使用 Option Explicit,是吗?):

Dim fld As Field
Dim msg As String

    For Each fld In rstRecordSet.Fields
        msg = msg & fld.Value & "|"
    Next

Debug.Print msg    'or MsgBox msg 

I think the pipe ("|") makes a better separator than a space, since it's less likely to occur in your data.

我认为管道(“|”)比空格更适合分隔符,因为它不太可能出现在您的数据中。

回答by Xavinou

For the output console, I don't know (since I don't know VB), but for showing the whole record at once, you can use a foreachloop on rstRecordSet.Fields.

对于输出控制台,我不知道(因为我不知道 VB),但是为了一次显示整个记录,您可以foreachrstRecordSet.Fields.

In C#, I would write it like :

在 C# 中,我会这样写:

string msg = "";
foreach (Field f in rstRecordSet.Fields)
{
    msg += f.Value + " ";
}
MessageBox.Show(msg);

Now, you just have to find the VB syntax...

现在,您只需要找到 VB 语法...

回答by Big McLargeHuge

Instead of building your own string, piece by piece, you can use the GetString method of the Recordset object:

您可以使用Recordset 对象GetString 方法,而不是逐个构建自己的字符串:

Debug.Print records.GetString(adClipString, 1)

An unfortunate side effect of this method is that it seems to remove the record from the record set.

这种方法的一个不幸的副作用是它似乎从记录集中删除了记录。