简体   繁体   English

VBA动态数组仅分配一维

[英]VBA dynamic array assigns only one dimension

I have some strange behavior with a dynamic array. 我对动态数组有一些奇怪的行为。 It seems like VBA denies assigning a value to a variable. 似乎VBA拒绝将值分配给变量。 Here is the code: 这是代码:

Private Sub Report_Load()
    Dim db As Database
    Dim reportArray() As Variant
    Dim structre As Recordset
    Dim columns, rows, i As Integer

    'Open recordset
    Set db = CurrentDb
    Set structure = db.OpenRecordset("SELECT * FROM [tblstructure] ORDER BY [Rank]")

    'Change array size dynamically
    rows = structure.RecordCount
    columns = 4
    ReDim reportArray(rows, columns)

    'Populate array
    i = 0
    Do Until structure.EOF
        reportArray(i, 0) = structure![Name]
        i = i + 1
        structure.MoveNext
    Loop
End Sub 

When I open the report I get an error that subscript is out of range. 当我打开报告时,出现下标超出范围的错误。 When I debug my code I can see that value of i in the loop is 2, so my array must be smaller that that. 当我调试代码时,我可以看到循环中的i值为2,因此我的数组必须小于该值。 When I hover over rows variable I see that its value is 1. So indeed I'm trying to access something that is out of range. 当我将鼠标悬停rows变量上时,我看到它的值为1。因此,实际上,我正在尝试访问超出范围的内容。 But the strange part is that the value of structure.RecordCount is 23. A screenshot: 但是奇怪的部分是structure.RecordCount值为23。屏幕截图: 在此处输入图片说明

Even if I use the code like this: 即使我使用这样的代码:

ReDim reportArray(structure.RecordCount, columns)

I get an array of size (1, 4). 我得到一个大小为(1、4)的数组。 Why isn't VBA assigning value 23 to variable "rows" or why ReDim assigns the right value to second dimension, but not the first? VBA为什么不将值23分配给变量“行”,或者为什么ReDim却将正确的值分配给第二维,而不是第一维?

It's been a while since I've used Access, but I seem to recall that RecordCount doesn't work properly unless you've actually enumerated the entire resultset doing something like structure.MoveLast first (just remember to do structure.MoveFirst before looping through). 它是因为我已经使用访问一段时间,但我好像记得,除非你确实列举整个结果做这样的事情总记录无法正常工作structure.MoveLast第一(只记得做structure.MoveFirst通过循环前)。 Of course, that requires a recordset capable of moving in both directions. 当然,这需要一个能够双向移动的记录集。

Alternatively, you could put the ReDim in your loop, and just increase it by one each time. 或者,您可以将ReDim放入循环中,并且每次只将其增加一。

Read this: https://support.microsoft.com/en-us/kb/105976 阅读此: https : //support.microsoft.com/en-us/kb/105976

CAUSE 原因

For recordsets and snapshots, Microsoft Access does not automatically return the number of records that exist in the recordset. 对于记录集和快照,Microsoft Access不会自动返回记录集中存在的记录数。 Rather, it returns the number of records accessed. 而是返回访问的记录数。

RESOLUTION 解析度

To determine the exact number of records in a recordset or snapshot, use the MoveLast method before checking the RecordCount property. 若要确定记录集或快照中的确切记录数,请在检查RecordCount属性之前使用MoveLast方法。

STATUS 状态

This behavior is by design. 此行为是设计使然。

As per microsoft , RecordCount returns the number of records accessed and not the number of total records. 根据microsoft,RecordCount返回访问的记录数,而不是总记录数。

So, in your case, this should work 所以,在您的情况下,这应该可行

 structure.MoveLast
 rows = structure.RecordCount
structure.MoveFirst

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

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