运行Select后,为什么我的记录集会为空?

时间:2013-06-28 14:10:01

标签: sql excel excel-vba excel-2007 vba

我试图让这段代码工作,但它一直为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

2 个答案:

答案 0 :(得分:0)

您正在打开游标,但没有移动它,因此.RecordCount的结果将始终为-1。 Check this.

答案 1 :(得分:0)

您正确地将记录拉入Recordset,但lspubs.RecordCount将无效,因为Recordset的光标仍在第一条记录中。尝试替换

(lsPubs.RecordCount > 0)

(not(lsPubs.EOF))