Solved

Array subscript out of range in vba

Posted on 2014-10-04
9
271 Views
Last Modified: 2014-10-04
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
Comment
Question by:PeterBaileyUk
  • 6
  • 2
9 Comments
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 250 total points
Comment Utility
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
 
LVL 24

Assisted Solution

by:Bitsqueezer
Bitsqueezer earned 250 total points
Comment Utility
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
 

Author Comment

by:PeterBaileyUk
Comment Utility
ok i will delete the bit i have and create a recordset and look at getrows method
0
 

Author Comment

by:PeterBaileyUk
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:PeterBaileyUk
Comment Utility
string in might be ="black shadow [professional blue"
0
 

Author Comment

by:PeterBaileyUk
Comment Utility
I am on a doubleclick event of a subform that passes the value of the row
0
 

Author Comment

by:PeterBaileyUk
Comment Utility
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
 

Author Closing Comment

by:PeterBaileyUk
Comment Utility
ive shared points as youve both put me in the right direction.
0
 
LVL 15

Expert Comment

by:WalkaboutTigger
Comment Utility
Thank you - always happy to assist or, at the very least, be a wall to bounce ideas off of.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now