Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Array subscript out of range in vba

I had a piece of code from ID: 40357636, which has started failing.

The code rotates around a recordset and stores the words it finds in an instr to an array.

I modded to a multidimensional array.

The data is small so pasted here

if it finds "[Business" (example) it stores both [Business and "[Bus" to the array elements.
thats all it needs do. it does the first element 0,0 then it halts after that.

I cannot see why its failing as the indexing looks simple

0,0
0,1
1,0
1,1 etc


    With RstAlias

        If RstAlias.BOF And RstAlias.EOF Then
        Else
        .MoveLast
        y = .RecordCount
        .MoveFirst
        'cycle through the recordset
        Do Until RstAlias.EOF
            
            If InStr(Me.[DecodeResult].Value, .Fields("sClientDescGroup").Value) <> 0 Then
            
                ReDim Preserve AliasArray(index+1, 1) As String
              
                If .Fields("sDesc").Value = " " Or .Fields("sDesc").Value = "" Then
                
                Else
                    AliasArray(index, 0) = .Fields("sDesc").Value
                    AliasArray(index, 1) = .Fields("sClientDescGroup").Value
                    index = index + 1
                End If
            Else
            
            
            End If
        

        
        
        .MoveNext
        Loop
        
     End If
    End With

Open in new window



sDesc            sClientDescGroup
[Bus         [Business
[Prof      [Professional
Med]      Media]
Lux             Luxury
Med            Media]
Med]      Media]
Auto[Pro            [Professional
Auto[Bus      [Business
Auto[Prof      [Professional
Bus                 Business
Busines         Business
Prof               [Professional
Pro              [Professional
Professional      [Professional
0
PeterBaileyUk
Asked:
PeterBaileyUk
  • 6
  • 2
2 Solutions
 
WalkaboutTiggerCommented:
So this code, as represented here, has worked flawlessly for some time and has only recently started failing.  Correct?

At first glance, I would presume the data is somehow malformed or corrupted.

I do question the lack of code in two of the If then/else constructs after the "then".
Your code reads:

IF sky="Blue" THEN
ELSE
  some code here
END IF

As opposed to
IF sky<>"Blue" Then
  some code here
END IF

Otherwise, I would recommend looking at your data.
0
 
BitsqueezerCommented:
Hi,

If you read the documentation about the Redim command you would see that you can only redimension one part of an x-dimensional array, always the last one. So if you change the array into "...(1, Index + 1)" then it works.

Beside what you've made with your If's like WalkaboutTigger reported above, you should also add an "Option Explicit" to your module (always, all modules) and declare all variables first.

Next is that it would surely be more simple to use a SELECT command which creates a recordset that only contains the needed values. In this case you can simply use the "GetRows" method of the recordset to download the wanted values into an array (which is much faster).

It's also the question if the recordset and the array is needed at all, mostly the things can be done with SQL (which is in 99% better than using VBA).

Cheers,

Christian
0
 
PeterBaileyUkAuthor Commented:
ok i will delete the bit i have and create a recordset and look at getrows method
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterBaileyUkAuthor Commented:
I am not seeing how to use getrows. I have a recordset that produces the 14 rows you see in the post and i have a string and i need to see if field sclientdescgroup.value is in the string and only store those words.

so if stringin  has "[Professional" in then the following gets put into an array.
element 1     element 2
Auto[Prof      [Professional
0
 
PeterBaileyUkAuthor Commented:
string in might be ="black shadow [professional blue"
0
 
PeterBaileyUkAuthor Commented:
I am on a doubleclick event of a subform that passes the value of the row
0
 
PeterBaileyUkAuthor Commented:
ok i think ive got it in sql, its abstract but does return the records:

Ive put the string of words as an expression in the query itself:

I may not need an array at all now


SELECT TblModelAlias.sDesc, TblModelAlias.sClient, TblModelAlias.sModel, TblModelAlias.sClientDescGroup, InStr("Business [Professional Media]",[sClientDescGroup])<>0 AS Expr1
FROM TblModelAlias
WHERE (((TblModelAlias.sClient)='Cap') AND ((TblModelAlias.sModel)='3Series'));

Open in new window

0
 
PeterBaileyUkAuthor Commented:
ive shared points as youve both put me in the right direction.
0
 
WalkaboutTiggerCommented:
Thank you - always happy to assist or, at the very least, be a wall to bounce ideas off of.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now