简体   繁体   English

VB6 ADODB.Recordset Record.Count 不起作用/EOF 和 BOF 不可用

[英]VB6 ADODB.Recordset Record.Count doesn't work / EOF & BOF not available

I've got a question regarding ADODB recordset in VB6, which has confused me for a few weeks.我有一个关于 VB6 中的 ADODB 记录集的问题,这让我困惑了几个星期。 I've written the recordset into worksheets to achieve some results that I can't get from recordset directly.我已将记录集写入工作表以实现一些我无法直接从记录集中获得的结果。 But as the data set builds up, writing the recordset into worksheet would slow the program down, I wonder if someone could resolve the recordset puzzle for me.但是随着数据集的建立,将记录集写入工作表会减慢程序的速度,我想知道是否有人可以为我解决记录集难题。

Below is the problem I have - 1) xRst.Recordcount always returns -1 2) Error message, "Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another", pops up on set (A).cursorlocation to either adUseClient or adUseServer, and (B).LockType 3) Unable to.getrows on recordset => I believe it's the same cause as xRst.Recordcount returning -1?以下是我遇到的问题 - 1) xRst.Recordcount 总是返回 -1 2) 错误消息,“参数类型错误,超出可接受范围,或相互冲突”,在集合上弹出(A ).cursorlocation 到 adUseClient 或 adUseServer,以及 (B).LockType 3) Unable to.getrows on recordset => 我相信这与 xRst.Recordcount 返回 -1 的原因相同?

Below is part of my code.以下是我的代码的一部分。 Could above issues caused by limitation of the provider?上述问题可能是由提供商的限制引起的吗?

xConnstring = "Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties='Excel 12.0'; Data Source =" & Thisworkbook.fullname
xCnn.Open xConnstring
xSqlstring = " SELECT * FROM [APRI$] "
Set xRst = xCnn.Execute(xSqlstring)
Msgbox(xRst.RecordCount)
Do Until xRst.EOF
     ......
     xRst.MoveNext
Loop

For the recordset, I've also tried two open methods, but doesn't work either.对于记录集,我也尝试了两种开放方法,但也不起作用。

Set xRst.ActiveConnection = xCnn
xRst.Source = xSqlstring
xRst.CursorType = adOpenDynamic
------Error Message Occurs On Below Two Lines------
xRst.CursorLocation = adUseServer
xRst.LockType = adLockOptimistic
xRst.Open

Below code will encounter an error, but it will pass through when the two last parameters are removed下面的代码会遇到错误,但是去掉最后两个参数的时候会通过

xRst.Open xSqlstring, xCnn, adOpenKeyset, adUseServer, adLockoptimistic

Could someone please kindly advise how I can get the 1) recordset.recordcount, 2) recordset.movenext work?有人可以请教我如何获得 1)recordset.recordcount,2)recordset.movenext 工作吗?

Thanks heaps in advance.提前致谢。

Default cursortype is adOpenForwardOnly.默认光标类型是 adOpenForwardOnly。 With adOpenForwardOnly or adOpenUnspecified the record count is always returned as -1.对于 adOpenForwardOnly 或 adOpenUnspecified,记录计数始终返回为 -1。 Use either adOpenKeySet or adOpenStatic.使用 adOpenKeySet 或 adOpenStatic。 ie: (I assume sheet name APRI is correct and not APRIL - and there is a worksheet named Dummy to list the results for test):即:(我假设工作表名称 APRI 是正确的,而不是 APRIL - 并且有一个名为 Dummy 的工作表来列出测试结果):

Dim xCnn As ADODB.Connection
Dim xRst As ADODB.Recordset
Dim xConnString As String

xConnString = "Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties='Excel 12.0'; Data Source =" & ThisWorkbook.FullName
Set xCnn = New ADODB.Connection
xCnn.Open xConnString
xSqlstring = " SELECT * FROM [APRI$] "
Set xRst = New ADODB.Recordset
xRst.Open xSqlstring, xCnn, adOpenStatic
MsgBox (xRst.RecordCount)
Dim row As Integer
row = 1
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Dummy")
Do Until xRst.EOF
    '...
    ws.Cells(row, 1).Value = xRst.Fields(0).Value
    row = row + 1
     xRst.MoveNext
Loop

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

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