Solved

runtime error 9 subscript out of range

Posted on 2007-11-26
4
558 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

744 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