How to populate a dynamic array in MS Access?

时间:2019-04-17 00:15:46

标签: ms-access access-vba

I have a dynamic array that I want to append values to. The number of values to be appended is not fixed

I was trying to do something like this:

Dim array() As Integer
ReDim Preserve array(UBound(array)+1)
bulkJob(UBound(array) + 1) = Me.ID

I get subscript out of range error at ReDim Preserve array(UBound(array)+1). Is there a way to do this?

1 个答案:

答案 0 :(得分:1)

不太清楚您要做什么,但这可以为您带来一些想法:

Public Function BuildJobs(Id As Integer)

    Static bulkJob()    As Integer
    Dim Upper           As Integer

    On Error Resume Next
    Upper = UBound(bulkJob) + 1
    On Error GoTo 0

    ReDim Preserve bulkJob(Upper)
    ' Fill in value.
    bulkJob(Upper) = Id

    ' Do something.
    Debug.Print UBound(bulkJob), bulkJob(Upper)

End Function

“重新启动”数组,如下所示:

ReDim bulkJob(0)
bulkJob(0) = 0