Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

runtime error 9 subscript out of range

Posted on 2007-11-26
4
Medium Priority
?
573 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 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

773 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