Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

runtime error 9 subscript out of range

Posted on 2007-11-26
4
Medium Priority
?
570 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

718 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