Link to home
Start Free TrialLog in
Avatar of mbart
mbart

asked on

How do I set dynamic array with no upper limit which allows additions to the array?

I have a sub which I want to use to track some ID(s) dynamically.  Want to declare array and then be able to add values to it dynamically below is my attempt but keep getting subscript out of range error.

Dim IDNr As Integer
Dim Cnt As Integer
Dim StudentID
StudentID = Array()
IDNr = txtID
Cnt = UBound(StudentID) + 1
StudentID(Cnt) = IDNr

Long on points short on talent.  Need fast help.
Avatar of Harisha M G
Harisha M G
Flag of India image

Hi mbart,
    Dim arr() As Integer

    ReDim Preserve arr(UBound(arr) + 1)
    arr(UBound(arr)) = IDNr1

    ReDim Preserve arr(UBound(arr) + 1)
    arr(UBound(arr)) = IDNr2

    ....

Bye
---
Harish
Hi mbart,

I am not specializing in VB, so I wouldn't know if there is a better way, but why don't you do this?

im IDNr As Integer
Dim Cnt As Integer
Dim StudentID
StudentID = Array()
IDNr = txtID
Cnt = Ubound(StudentID)+1
ReDim StudentID(Cnt)
StudentID(Cnt) = IDNr

I don't know how effective this is, but by searching around, I also found this site which may be interesting: http://www.aspheute.com/english/20001025.asp


Regards,
Zyloch
Oops, my bad, didn't see your response, and I forgot the Preserve lol

Regards...
No problem ;-)
Avatar of mbart
mbart

ASKER

Thanks for the quick help. Let me try it.
Avatar of mbart

ASKER

Here is my code which still throws subscript error

Dim IDNr As Integer
Dim TotalScore As Integer
Dim StudentAvg As Double
Dim StudentID
IDNr = CInt(txtID)
Dim arr() As Integer
ReDim Preserve arr(UBound(arr) + 1)
    arr(UBound(arr)) = StudentID
Debug.Print arr(UBound(arr))

What did i miss?
ASKER CERTIFIED SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mbart

ASKER

Worked!! Thanks for the help