简体   繁体   English

ADODB 记录集记录计数总是返回 -1

[英]ADODB recordset recordcount always returns -1

I am trying to retrieve data to excel form a database in MS access.我正在尝试在 MS 访问中检索数据以形成数据库。 However the recordcount property for recordset always return -1 though for other purposes the code is working fine.然而,记录集的 recordcount 属性总是返回 -1,但出于其他目的,代码工作正常。

The code I am using is as follows : `Sub datarecordset()我使用的代码如下:`Sub datarecordset()

Dim cn As adodb.Connection
Dim oRs As adodb.Recordset
Set cn = CreateObject("ADODB.Connection")
DBPath = "C:\[databse path]" & "\[database name].accdb"
dbWs = "[excel sheet name]"
scn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBPath
dsh = "[" & "[excel sheet name]" & "$]"
cn.Open scn
Dim sSQL As String

Dim F As Integer

sSQL = "Select 'W',a.[Subledger],NULL,sum(a.[Amount]) from GL_Table a where a.[Opex_Group] = 10003 and year(a.[G/L Date]) = " & Year(Sheets("Repairs").Cells(1, 4)) & " and month(a.[G/L Date]) = " & Month(Sheets("Repairs").Cells(1, 4))
sSQL = sSQL & " group by " & "a.[Subledger],(year(a.[G/L Date])),(month(a.[G/L Date]))"
Set oRs = cn.Execute(sSQL)
Debug.Print oRs.RecordCount
oRs.Close
....... further code to print to excel here

cn.Close
End Sub`

The code will fetch data in recordset and write in excel.该代码将获取记录集中的数据并写入 excel。 But since the recordset property is not returning the recordcount so can't print values of various fields in recordset to different cells of excel worksheet.但是由于记录集属性不返回记录计数,因此无法将记录集中各个字段的值打印到 Excel 工作表的不同单元格。

I searched on google and understood that I need to declare the recordset type and for that I have to use connection.open in place of connection.execute.我在谷歌上搜索并了解到我需要声明记录集类型,为此我必须使用 connection.open 代替 connection.execute。 But I am trying to change the code then it gives the error object variable or With variable not defined.但是我正在尝试更改代码,然后它给出了错误对象变量或未定义变量。

Any quick help will be welcome.欢迎任何快速帮助。 Thanks.谢谢。

The link by @BitAccesser provides a valid solution. @BitAccesser 的链接提供了有效的解决方案。 Quick how-to-implement in your situation: Instead of Set oRs = cn.Execute(sSQL)在您的情况下快速实施:而不是Set oRs = cn.Execute(sSQL)

Set oRS = CreateObject("ADODB.Recordset")
oRS.CursorLocation = adUseClient
oRS.Open sSQL, cn

ADO 's recordcount property returns -1 when ADO cannot determine the number of records or if the provider or cursor type does not support RecordCount. ADO的RecordCount属性返回-1时,ADO无法确定记录,或者如果提供者或游标类型不支持总记录数。 That last one is true for this case.对于这种情况,最后一个是正确的。

To avoid this, there are several solutions.为了避免这种情况,有几种解决方案。 The most simple one is to use a client-side cursor, which I just demonstrated, but more alternative solutions are provided in the links by @BitAccesser最简单的方法是使用我刚刚演示的客户端游标,但@BitAccesser 在链接中提供了更多替代解决方案

You may also specify the CursorType as the third argument to open the RecordSet as follows, which is optional你也可以指定 CursorType 作为第三个参数来打开 RecordSet,如下所示,这是可选的

  • The first two lines, leaving blank or selecting adOpenDynamic, do not give the record count.前两行留空或选择 adOpenDynamic,不提供记录计数。

  • The remaining ones work OK.其余的工作正常。

  • 1-RS.Open SqlStr, Conn 1-RS.Open SqlStr, Conn

  • 2-RS.Open SqlStr, Conn, adOpenDynamic 2-RS.Open SqlStr、Conn、adOpenDynamic

(Erik's solution) - 3-RS.CursorLocation = adUseClient (Erik 的解决方案)- 3-RS.CursorLocation = adUseClient

Other Options also work fine, please note 4- and 6- which do not require a seperate line - 4-RS.Open SqlStr, Conn, adOpenKeyset其他选项也可以正常工作,请注意 4- 和 6- 不需要单独的行 - 4-RS.Open SqlStr、Conn、adOpenKeyset
- 5-RS.Open SqlStr, Conn, adOpenKeyset AND RS.CursorLocation = adUseClient - 5-RS.Open SqlStr, Conn, adOpenKeyset AND RS.CursorLocation = adUseClient
- 6-RS.Open SqlStr, Conn, adOpenStatic AND RS.CursorLocation = adUseClient - 6-RS.Open SqlStr, Conn, adOpenStatic AND RS.CursorLocation = adUseClient
- 7-RS.Open SqlStr, Conn, adOpenStatic - 7-RS.Open SqlStr、Conn、adOpenStatic

BR, Çağlar BR, 恰拉尔

You can still use the Execute method but you need to set the correct cursor type.您仍然可以使用 Execute 方法,但您需要设置正确的游标类型。 The recordset is created automatically with cursor type adOpenForwardOnly .记录集是使用游标类型adOpenForwardOnly自动创建的。 This results in oRs.RecordCount = -1 .这导致oRs.RecordCount = -1 adOpenKeySet is the correct cursor type to correctly show oRs.RecordCount . adOpenKeySet是正确显示oRs.RecordCount的正确游标类型。
Note: The LockType is irrelevant in this case.注意:在这种情况下, LockType无关紧要。

Set oRs = cn.Execute(sSQL)
oRs.Close
oRs.CursorType = adOpenKeyset
oRs.Open
Debug.Print oRs.RecordCount

Closing the recordset, changing the cursor type and reopening the recordset worked fine for me (Access 2016 on Windows 7).关闭记录集、更改光标类型并重新打开记录集对我来说效果很好(Windows 7 上的 Access 2016)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM