Redim causes Subscript Out of Range (Error 9) 
Author Message
 Redim causes Subscript Out of Range (Error 9)

Hi All

Could someone help me with this - any assistance much appreciated!
I dont know how many entries I will be getting from the user so I
created an array in a Module in VB :- Public varSection2() As Variant
In the General, Declarations section of my form I declare this
variable:- Private intListBoxIndex As Integer - I then set it to 0 in
the Form_Load section.

I then run this code:

If intListBoxIndex = 0 Then
   ' First access to the array.
    ReDim varSection2(intListBoxIndex, 0)
Else
    ' Preserve the entries but increase size of array
    ReDim Preserve varSection2(UBound(varSection2) + 1, 0) ' Fails Here
End If

intListBoxIndex = intListBoxIndex + 1

BUT

When I did this in a different project and it worked OK:

In the General, Declarations section :- Public intCounter As Integer

    If intCounter = 0 Then
        ' Counter has just been initialised
        ReDim varMyArray(intCounter, 1)
    Else
        ' Counter is more than 1
        ReDim Preserve varMyArray(intCounter, 1)
    End If

    intCounter = intCounter + 1

The only difference is that I declare the array in the Form_Load
section of the form.

Any ideas ?

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Redim causes Subscript Out of Range (Error 9)

You can only   Redim Preserve   the  last element in an array in VB5

Make the thing an array of UDTs

Quote:

>Hi All

>Could someone help me with this - any assistance much appreciated!
>I dont know how many entries I will be getting from the user so I
>created an array in a Module in VB :- Public varSection2() As Variant
>In the General, Declarations section of my form I declare this
>variable:- Private intListBoxIndex As Integer - I then set it to 0 in
>the Form_Load section.

>I then run this code:

>If intListBoxIndex = 0 Then
>   ' First access to the array.
>    ReDim varSection2(intListBoxIndex, 0)
>Else
>    ' Preserve the entries but increase size of array
>    ReDim Preserve varSection2(UBound(varSection2) + 1, 0) ' Fails Here
>End If

>intListBoxIndex = intListBoxIndex + 1

>BUT

>When I did this in a different project and it worked OK:

>In the General, Declarations section :- Public intCounter As Integer

>    If intCounter = 0 Then
>        ' Counter has just been initialised
>        ReDim varMyArray(intCounter, 1)
>    Else
>        ' Counter is more than 1
>        ReDim Preserve varMyArray(intCounter, 1)
>    End If

>    intCounter = intCounter + 1

>The only difference is that I declare the array in the Form_Load
>section of the form.

>Any ideas ?

>Sent via Deja.com http://www.deja.com/
>Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Redim causes Subscript Out of Range (Error 9)
Hi there,

As Jerry has pointed out, you can ReDim Preserve only the last dimension
of dynamically allocated array.

In your case you have two-dimensional array. Swap your rows and columns:

Use:        ReDim Preserve varSection2(0, UBound(varSection2, 2) + 1)

Instead of: ReDim Preserve varSection2(UBound(varSection2) + 1, 0)

It will work

With rspect

Alex


Quote:

> You can only   Redim Preserve   the  last element in an array in VB5

> Make the thing an array of UDTs


> >Hi All

> >Could someone help me with this - any assistance much appreciated!
> >I dont know how many entries I will be getting from the user so I
> >created an array in a Module in VB :- Public varSection2() As Variant
> >In the General, Declarations section of my form I declare this
> >variable:- Private intListBoxIndex As Integer - I then set it to 0 in
> >the Form_Load section.

> >I then run this code:

> >If intListBoxIndex = 0 Then
> >   ' First access to the array.
> >    ReDim varSection2(intListBoxIndex, 0)
> >Else
> >    ' Preserve the entries but increase size of array
> >    ReDim Preserve varSection2(UBound(varSection2) + 1, 0) ' Fails
Here
> >End If

> >intListBoxIndex = intListBoxIndex + 1

> >BUT

> >When I did this in a different project and it worked OK:

> >In the General, Declarations section :- Public intCounter As Integer

> >    If intCounter = 0 Then
> >        ' Counter has just been initialised
> >        ReDim varMyArray(intCounter, 1)
> >    Else
> >        ' Counter is more than 1
> >        ReDim Preserve varMyArray(intCounter, 1)
> >    End If

> >    intCounter = intCounter + 1

> >The only difference is that I declare the array in the Form_Load
> >section of the form.

> >Any ideas ?

> >Sent via Deja.com http://www.deja.com/
> >Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.


Wed, 18 Jun 1902 08:00:00 GMT  
 Redim causes Subscript Out of Range (Error 9)
Thats strange cos when I tried almost the exact same code in a seperate
project - see below, it worked ! I'll be happy to send the file to
anyone to have a look at :)

Any ideas why this happens ?

Cheers

Edxx

Quote:

> You can only   Redim Preserve   the  last element in an array in VB5

> Make the thing an array of UDTs


> >Hi All

> >Could someone help me with this - any assistance much appreciated!
> >I dont know how many entries I will be getting from the user so I
> >created an array in a Module in VB :- Public varSection2() As Variant
> >In the General, Declarations section of my form I declare this
> >variable:- Private intListBoxIndex As Integer - I then set it to 0 in
> >the Form_Load section.

> >I then run this code:

> >If intListBoxIndex = 0 Then
> >   ' First access to the array.
> >    ReDim varSection2(intListBoxIndex, 0)
> >Else
> >    ' Preserve the entries but increase size of array
> >    ReDim Preserve varSection2(UBound(varSection2) + 1, 0) ' Fails
Here
> >End If

> >intListBoxIndex = intListBoxIndex + 1

> >BUT

> >When I did this in a different project and it worked OK:

> >In the General, Declarations section :- Public intCounter As Integer

> >    If intCounter = 0 Then
> >        ' Counter has just been initialised
> >        ReDim varMyArray(intCounter, 1)
> >    Else
> >        ' Counter is more than 1
> >        ReDim Preserve varMyArray(intCounter, 1)
> >    End If

> >    intCounter = intCounter + 1

> >The only difference is that I declare the array in the Form_Load
> >section of the form.

> >Any ideas ?

> >Sent via Deja.com http://www.deja.com/
> >Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Redim subscript out of range?

2. Subscript out of range error

3. Subscript out of range error

4. Subscript out of range error

5. AddShapeRecordDetected Error: Subscript out of range

6. Defining an array and getting Subscript Out of Range Error

7. help with subscript out of range error

8. Problems With FlexGrid And Subscript Out Of Range Error

9. runtime error 9, subscript out of range

10. Subscript out of range (Error 9)

11. Subscript out of range error problem

12. Out of subscript range error!

 

 
Powered by phpBB® Forum Software