Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Array subscript out of range in vba

Posted on 2014-10-04
9
Medium Priority
?
294 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 1000 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 1000 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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