Solved

Array subscript out of range in vba

Posted on 2014-10-04
9
277 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
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
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.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

813 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

12 Experts available now in Live!

Get 1:1 Help Now