0

我试图让这段代码工作,但它一直为 RecordCount 返回 -1。select 语句确实找到了值,我去了数据库并在那里运行它。但我无法在我的程序中获得任何值。

Dim quotedPubs As ADODB.Connection
    Set quotedPubs = New ADODB.Connection
    quotedPubs.Open "PROVIDER=SQLOLEDB; DATA SOURCE=*****;INITIAL CATALOG=*****; User ID=****; Password=****"

    Dim hoursPubs As ADODB.Connection
    Set hoursPubs = New ADODB.Connection
    hoursPubs.Open "PROVIDER=SQLOLEDB; DATA SOURCE=*****;INITIAL CATALOG=*****; User ID=****; Password=****"

    Dim lsPubs As ADODB.Recordset
    Set lsPubs = New ADODB.Recordset

    With lsPubs
        .ActiveConnection = quotedPubs

        .Open "SELECT ProjectNumber FROM hours h"

        ' WHERE h.lead = " & sEmpNum & ""
        If (lsPubs.RecordCount > 0) Then
            arr = lsPubs.GetRows(lsPubs.RecordCount)
            .Close
4

2 回答 2

0

您正在打开光标,但没有在其中移动,因此 .RecordCount 的结果将始终为 -1。检查这个。

于 2013-06-28T14:43:39.523 回答
0

您正确地将记录拉入 Recordset,但lspubs.RecordCount由于 Recordset 的光标仍位于第一条记录上,因此无法正常工作。尝试更换

(lsPubs.RecordCount > 0)

(not(lsPubs.EOF))

于 2013-06-28T15:19:03.730 回答