Solved

runtime error 9 subscript out of range

Posted on 2007-11-26
4
562 Views
Last Modified: 2008-02-01
I get this error "runtime error 9 subscript out of range"  when I run my vb6 code.  I am accessing an sql database and it is returning an error at "Com(21) = rec.RecordCount"

Here is some of the surrounding code:

'---------------------------Youth Female Participant 19 to 30-----------

esql = "select frmRegistrationCSWText2,frmRegistrationCSWText4 From frmRegistrationCSW Where frmRegistrationCSWText6 >= 19 And frmRegistrationCSWText6 <= 30 And frmRegistrationCSWCheck2 = 1 And frmRegistrationCSWCheck3 = 1 And frmRegistrationCSWCheck10 =1 And frmRegistrationCSWText15  >= convert( datetime, " & ravi & BeginDate & ravi & " , 101 )And frmRegistrationCSWText15  <= convert( datetime, " & ravi & EndDate & ravi & " , 101 ) "

If Not Combo1.Text = "-All-" Then
esql = esql & " And frmRegistrationCSWText1 = " & ravi & Program & ravi
End If

rec.Open (esql), conn, adOpenStatic, adLockOptimistic

Com(21) = rec.RecordCount


rec.Close


'---------------------------Youth Male Participant 31 to 35-----------

esql = "select frmRegistrationCSWText2,frmRegistrationCSWText4 From frmRegistrationCSW Where frmRegistrationCSWText6 >= 31 And frmRegistrationCSWText6 <= 35 And frmRegistrationCSWCheck1 = 1 And frmRegistrationCSWCheck3 = 1 And frmRegistrationCSWCheck10 =1 And frmRegistrationCSWText15  >= convert( datetime, " & ravi & BeginDate & ravi & " , 101 )And frmRegistrationCSWText15  <= convert( datetime, " & ravi & EndDate & ravi & " , 101 ) "

If Not Combo1.Text = "-All-" Then
esql = esql & " And frmRegistrationCSWText1 = " & ravi & Program & ravi
End If

rec.Open (esql), conn, adOpenStatic, adLockOptimistic

Com(22) = rec.RecordCount


rec.Close
0
Comment
Question by:al4629740
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
EDDYKT earned 500 total points
ID: 20351201
what do you define Com as?
0
 

Author Comment

by:al4629740
ID: 20351306
Found the problem while I was in the bathroom.  I didn't declare a large enough Com scope.  It was Com (1 to 20) and needed to increase it.  Thanks.
0
 
LVL 2

Expert Comment

by:andrewneely
ID: 20359284
The best approach I've found with arrays is to use dynamic array bounds.  This way, you code gracefully handles larger subscripts without having to guess at the larget subscript.

I declare a counter variable, increment it, and to a redim preserve.

The following code reads a text file into an array of strings.
dim inputBufferString() as string
'the -1 is important so that 0 is our first subscript
dim bufferCountInteger as integer=-1
if io.file.exists("myfile.txt") then
    dim myStreamReader as new io.streamreader("myfile.txt")
    do while mystreamreader.peek <> -1
        bufferCountInteger +=1
        redim preserve inputBufferString (bufferCountInteger)
        inputBufferString(bufferCountInteger)=myStreamReader.readline
    loop
myStreamReader.close
'now the contents of the file is stored in our string array.
0
 

Author Comment

by:al4629740
ID: 20360251
Thank you for that.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using Access 8 68
VBA: Select SQL query based on a config Sheet v2 11 44
Zip Folders Using Chilkat Routines 1 40
which modules are active in VB6 project? 6 12
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 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