VBScript 中的 MoveFirst 不适用于查询上的查询?

MoveFirst in VBScript does not work with a query on a query?

我有一段查询 MS Access 数据库的 VBScript。当记录集查询在 table 上时,我可以遍历我的记录集并执行 rs.MoveFirst 返回到开头。但是在查询记录集查询时,rs.MoveFirst 失败并出现错误“此类对象不支持操作”代码:800004005.
这是一个已知的限制吗?我可以通过以不同的方式打开记录集来解决这个问题吗? 我试过 rs.Open 就像许多在线示例一样,但是 rs.Open strQuery, Cn, adOpenDynamic, adLockPessimistic, adCmdText 失败并显示“参数类型错误,超出了接受 table 范围,或者相互冲突。”。 =14=]

此代码有效,因为 MyTable 是 table:

Set rs = CreateObject("ADODB.Recordset")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyAccessDB.accdb;Mode=Read;"
connection.Open strConnection
Set rs = connection.Execute("SELECT * FROM MyTable")

MsgBox(rs.fields(1))
rs.MoveNext
rs.MoveFirst
MsgBox(rs.fields(1))

此代码失败,因为 MyQuery 是数据库中的查询

Set rs = CreateObject("ADODB.Recordset")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyAccessDB.accdb;Mode=Read;"
connection.Open strConnection
Set rs = connection.Execute("SELECT * FROM MyQuery")

MsgBox(rs.fields(1))
rs.MoveNext
rs.MoveFirst
MsgBox(rs.fields(1))

使用rs.Open 和定义常量不起作用。这在 rs.movefirst 命令上显示相同的错误“此类对象不支持操作”。

const adOpenDynamic = 2
const adLockPessimistic = 2
const adCmdText = 1

Set connection = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyAccessDB.accdb;Mode=Read;"
connection.Open strConnection
strsql = "SELECT * FROM MyQuery"
rs.Open strsql, connection, adOpenDynamic, adLockPessimistic, adCmdText

Do While Not rs.EOF
    msgbox(rs.fields(1))
    rs.movenext
    msgbox(rs.fields(1))
    rs.movefirst
    msgbox(rs.fields(1))
Loop

这会起作用。
您不需要声明常量、变量等等。
您只需要设置对 ADODB 的引用,在您的例子中是对 Microsoft Activex Data Objects 2.8 Library 的引用。
没有理由这行不通。

Set Connection = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Dim strConnection
Dim sql

strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyAccessDB.accdb;Mode=Read;"
Connection.Open strConnection

sql = "SELECT * FROM MyQuery"

rs.Open sql, Connection, adOpenStatic, adLockReadOnly, adCmdText

MsgBox (rs.Fields(1))
rs.MoveNext
MsgBox (rs.Fields(1))
rs.MoveFirst
MsgBox (rs.Fields(1))

rs.Close
Set rs = Nothing
Connection.Close
Set Connection = Nothing

编辑:我忽略了你写了“一段vbscript”的事实。如果您在 vbs 文件中使用此代码,则需要声明常量

Const adOpenStatic = 3
Const adLockReadOnly = 1
Const adCmdText = &H0001

有一种更简单的方法来处理这个问题,那就是完全否定 ADODB.Recordset 而不必担心游标和锁定支持。值得一提的是,这仅适用于读取数据。

使用 GetRows() 检索 two-dimensional 数组并使用它来导航数据。

Dim strConnection, connection, rs, data
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\MyAccessDB.accdb;Mode=Read;"
Call connection.Open(strConnection)
Set rs = connection.Execute("SELECT * FROM MyTable")
If Not rs.EOF Then data = rs.GetRows()

'Release recordset as it's no longer needed.
Call rs.Close()
Set rs = Nothing

Dim row, rows
Const fld_field1 = 1

If IsArray(data) Then
    rows = UBound(data, 2) 'Number of rows
    row = 0 
    Call MsgBox(data(fld_field1, row) 'Second column of First Row
    row = 1
    Call MsgBox(data(fld_field1, row) 'Second column of Second Row
    row = 0
    Call MsgBox(data(fld_field1, row) 'Second column of First Row

    'If you want to loop the data
    For row = 0 To rows
        Call MsgBox(data(1, row) 'Second Column of N Row
    Next
End If