Solved

Array subscript out of range in vba

Posted on 2014-10-04
9
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 250 total points
ID: 40361330
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
ID: 40361337
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
ID: 40361417
ok i will delete the bit i have and create a recordset and look at getrows method
0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

Author Comment

by:PeterBaileyUk
ID: 40361442
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
 

Author Comment

by:PeterBaileyUk
ID: 40361444
string in might be ="black shadow [professional blue"
0
 

Author Comment

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

Author Comment

by:PeterBaileyUk
ID: 40361463
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
ID: 40361467
ive shared points as youve both put me in the right direction.
0
 
LVL 15

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

623 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