Runtime Error 9

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amolbehl
    New Member
    • May 2007
    • 63

    #1

    Runtime Error 9

    I have a VB 6.0 pgm and an important function in it I am trying to put a set of strings into an array. I have put important parts of the code below

    Dim sortArray() As String
    Set rsRoute = gdbFML.OpenReco rdset(strSQL, dbOpenDynaset)

    ctr = rsRoute.RecordC ount
    ReDim sortArray(0 To ctr)
    i = 0

    If Not rsRoute.EOF Then
    rsRoute.MoveFir st
    Do While Not rsRoute.EOF
    :
    :

    tempStr = LVS_item & LVS_desc & LVS_uspd & LVS_activity

    sortArray(i) = tempStr
    List1.AddItem tempStr, i
    List1.Selected( List1.NewIndex) = True
    rsRoute.MoveNex t
    i = i + 1
    Loop

    When i put the line in bold, i.e., sortArray(i) = tempStr, the code stops and says 'subscript out of range' I am not able to figure it out. Could someone plz help me out.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Intead of this...

    ctr = rsRoute.RecordC ount

    ...try putting this...

    [CODE=vb]If rsRoute.RecordC ount Then
    rsRoute.MoveLas t
    rsRoute.MoveFir st
    ctr = rsRoute.RecordC ount
    End If[/CODE]You see, in some circumstances, the records aren't actually counted until you "reach" them. The RecordCount will initially be either 0 (nothing found) or 1 (something found) but won't tell you how many. The MoveLast forces... um... the Jet Engine, or VB or whatever... to count them, thus updating the value in RecordCount.

    Comment

    Working...