?
Solved

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

Posted on 2005-05-08
8
Medium Priority
?
169 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:mbart
  • 3
  • 3
  • 2
8 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13955011
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
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 13955016
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
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 13955022
Oops, my bad, didn't see your response, and I forgot the Preserve lol

Regards...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 37

Expert Comment

by:Harisha M G
ID: 13955026
No problem ;-)
0
 

Author Comment

by:mbart
ID: 13955086
Thanks for the quick help. Let me try it.
0
 

Author Comment

by:mbart
ID: 13955163
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?
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 1200 total points
ID: 13955206
Private Sub Form_Load()
Dim IDNr As Integer
Dim TotalScore As Integer
Dim StudentAvg As Double
Dim StudentID
IDNr = CInt(txtID)
Dim arr() As Integer
ReDim arr(0)                                          '<-- Add this line
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = StudentID
Debug.Print arr(UBound(arr))

End Sub
0
 

Author Comment

by:mbart
ID: 13955239
Worked!! Thanks for the help
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question