jedu
jedu

Reputation: 1331

How to populate a dynamic array in MS Access?

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?

Upvotes: 0

Views: 867

Answers (1)

Gustav
Gustav

Reputation: 55806

Not quite clear what you are trying to do, but this could get you some ideas:

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

"Restart" the array like this:

ReDim bulkJob(0)
bulkJob(0) = 0

Upvotes: 1

Related Questions