subscript out of range run time error 9

kuniyo

New member
Local time
Tomorrow, 02:27
Joined
Aug 6, 2008
Messages
4
To all experts in VBA,

Can help to find what wrongs with my code ??? keep giving me run time error 9. What is the solutions to prevent this kind of things to happen again ??

I'm trying populate each of the textbox base on the individuals index selected in the listbox. however, when i selected the sixth index it shows error.. i dun knw what wrongs with the code.


Thank in advance and appreciated your helps.
I couldnt upload images its because i dun have 10 countpost.

Table and fields
field5
f,g,h,t,y,I

field6
1,2,3,4,5

field7
1,2,3,4,5,6

field8
1,2,3,4,5,6

field9
1,2,3,4,5,6

field10
1,2,3,4,5,6

field11
1,2,3,4,5,6



I'm using this code to populate the value base on rowsource of the combo box and sameindex as the listbox
Code:
Private Sub List0_AfterUpdate()
 
With Me
.Text2.Value = Split(.Combo14.Column(1), ",")(.List0.ListIndex)
.Text4.Value = Split(.Combo14.Column(2), ",")(.List0.ListIndex)
.Text6.Value = Split(.Combo14.Column(3), ",")(.List0.ListIndex)
.Text8.Value = Split(.Combo14.Column(4), ",")(.List0.ListIndex)
.Text10.Value = Split(.Combo14.Column(5), ",")(.List0.ListIndex)
.Text12.Value = Split(.Combo14.Column(6), ",")(.List0.ListIndex)
End With
 
End Sub
 
Last edited:
Column count starts with zero, the last column is 5.
 
the first column number is 0 so your range is 0-5

However I do not understand what you are trying to achieve, your code implies you have a rowsource of 6 columns each column of which is a string which can be split into an array

but this bit of code won't work

.Text2.Value = Split(.Combo14.Column(1), ",")

to do so, .Text2.Value would have to be an array and you cannot have an array as a control type.

and this bit

(.List0.ListIndex)

doesn't do anything as far as I can see
 
you're right.. what im trying to achieve.. i have a rowsource of 6 columns each column of which is a string which can be split into an array. so whenever i select an item in listbox, the rest of the textboxes will populate the value accordance to listbox index where .list0.listindex comes into the lead combo arr to populate the same index.

however, field 6 to 11 not always contains 6 array.. sometime might have 4 or 5 or even 0 arr in the field.

combo14.rowsource = "select field5, field6, field7, to field11 from table1 where field5=combo14.value "
 
combo14.rowsource = "select field5, field6, field7, to field11 from table1 where field5=combo14.value "
you can't set a rowsource based on the value from the same control

with my original post you should be able to split the strings and assign to the text boxes

good luck with your project
 
Code:
[COLOR=#ff0000]combo14.rowsource[/COLOR] = "select field5, field6, field7, to field11 from table1 where field1=[COLOR=red]combo14.value[/COLOR] "

my apology about the where clause. actually is pointed to the PK in the table.

Yep, they are able to split into the textboxes. however, whenever i selected "I" in the listbox the error pop in.:banghead:

when i step into debug mode. error was pointed this line
Code:
.Text2.Value = Split(.combo14.Column(1), ",")(.List0.ListIndex)

BTW i have uploaded the file, incase you cannot follow the flow of it
 

Attachments

Last edited:
error was pointed this line
I've already said that wouldn't work.

If you are using the listbox as a 2 dimensional array then you need to use the column property like this

.Text2.Value = Split(.combo14.Column(1, .List0.ListIndex), ",")
 
To all experts in VBA,

Can help to find what wrongs with my code ??? keep giving me run time error 9. What is the solutions to prevent this kind of things to happen again ??

I'm trying populate each of the textbox base on the individuals index selected in the listbox. however, when i selected the sixth index it shows error.. i dun knw what wrongs with the code.


Thank in advance and appreciated your helps.
I couldnt upload images its because i dun have 10 countpost.

Table and fields
field5
f,g,h,t,y,I

field6
1,2,3,4,5

field7
1,2,3,4,5,6

field8
1,2,3,4,5,6

field9
1,2,3,4,5,6

field10
1,2,3,4,5,6

field11
1,2,3,4,5,6



I'm using this code to populate the value base on rowsource of the combo box and sameindex as the listbox
Code:
Private Sub List0_AfterUpdate()
 
With Me
.Text2.Value = Split(.Combo14.Column(1), ",")(.List0.ListIndex)
.Text4.Value = Split(.Combo14.Column(2), ",")(.List0.ListIndex)
.Text6.Value = Split(.Combo14.Column(3), ",")(.List0.ListIndex)
.Text8.Value = Split(.Combo14.Column(4), ",")(.List0.ListIndex)
.Text10.Value = Split(.Combo14.Column(5), ",")(.List0.ListIndex)
.Text12.Value = Split(.Combo14.Column(6), ",")(.List0.ListIndex)
End With
 
End Sub


I also have same problem with run time error 9 subscript out of range,my code is below

Private Sub Command0_Click()

Dim MyArray As Variant
Dim fso As Variant
Dim objStream As Variant
Dim objFile As Variant
Dim sSQL As String

Dim myString As String


Dim db As Database
Dim rs As DAO.Recordset
Dim sql As String
Dim i As Integer
i = 0



Set db = CurrentDb()
Set rs = db.OpenRecordset("Table1")

sSQL = "SELECT * FROM Table1"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:\Users\herath\Desktop\Online Application\exep.csv") Then
Set objStream = fso.OpenTextFile("C:\Users\herath\Desktop\Online Application\exep.csv", 1, False, 0)
End If



Do While Not objStream.AtEndOfStream

strLine = objStream.ReadLine
ReDim MyArray(0)
MyArray = Split(strLine, ",")


rs.AddNew
rs("experiance") = MyArray(0)



rs.Update
i = i + 1
Loop

MsgBox "Uploaded " & i & " records"



End Sub

need help..thank you..
 
I've tried to re-jigg your code:
Code:
Private Sub Command0_Click()
    
    Dim MyArray     As Variant
    Dim fso         As Object
    Dim objStream   As Object
    Dim db          As DAO.Database
    Dim rs          As DAO.Recordset
    Dim i           As Integer
    
    Const FILE_PATH As String = "C:\Users\herath\Desktop\Online Application\exep.csv"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    If fso.FileExists(FILE_PATH) Then
        Set objStream = fso.OpenTextFile(FILE_PATH, 1, False, 0)
    Else
        Exit Sub
    End If
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT * FROM Table1", dbOpenDynaset)
    
    Do While Not objStream.AtEndOfStream
        MyArray = Split(objStream.ReadLine, ",")
        
        If UBound(MyArray) > 0 Then
            With rs
                .AddNew
                !experiance = MyArray(0)
                .Update
            End With
            i = i + 1
        End If
    Loop
    
    objStream.Close
    rs.Close
    Set objStream = Nothing
    Set rs = Nothing
    Set db = Nothing
    
    MsgBox "Uploaded " & i & " records"

End Sub
 
The main reason is that strLine doesn't contain a "," which cause that MyArray isn't assign anything.
Use the code vbaInet gives you, he has taken care of the above in his code.
 
I've tried to re-jigg your code:
Code:
Private Sub Command0_Click()
    
    Dim MyArray     As Variant
    Dim fso         As Object
    Dim objStream   As Object
    Dim db          As DAO.Database
    Dim rs          As DAO.Recordset
    Dim i           As Integer
    
    Const FILE_PATH As String = "C:\Users\herath\Desktop\Online Application\exep.csv"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    If fso.FileExists(FILE_PATH) Then
        Set objStream = fso.OpenTextFile(FILE_PATH, 1, False, 0)
    Else
        Exit Sub
    End If
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT * FROM Table1", dbOpenDynaset)
    
    Do While Not objStream.AtEndOfStream
        MyArray = Split(objStream.ReadLine, ",")
        
        If UBound(MyArray) > 0 Then
            With rs
                .AddNew
                !experiance = MyArray(0)
                .Update
            End With
            i = i + 1
        End If
    Loop
    
    objStream.Close
    rs.Close
    Set objStream = Nothing
    Set rs = Nothing
    Set db = Nothing
    
    MsgBox "Uploaded " & i & " records"

End Sub
[/QUOT]

Thank you for your response.
When I try with my whole code set still I have same run time error..my whole code set is below,do you have any Idea??


Private Sub Command0_Click()



Dim MyArray As Variant
Dim fso As Object
Dim objStream As Object
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer

Const FILE_PATH As String = "C:\Users\herath\Desktop\Online Application\exep.csv"

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(FILE_PATH) Then
Set objStream = fso.OpenTextFile(FILE_PATH, 1, False, 0)
Else
Exit Sub
End If

Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT * FROM Users", dbOpenDynaset)

Do While Not objStream.AtEndOfStream
MyArray = Split(objStream.ReadLine, ",")

If UBound(MyArray) > 0 Then
With rs
.AddNew
' !indexNo = MyArray(0)
' !experiance = MyArray(1)
!entryNumber = MyArray(0)
!DateCreated = MyArray(1)
!dateUpdated = MyArray(2)
!Campus = MyArray(4)

!StudiedBeforeMCorMLC = MyArray(5)
!MCorMLCNumber = MyArray(6)
!Salutation = MyArray(7)
!Fname = MyArray(8)

!Mname = MyArray(9)
!Lname = MyArray(10)
!Sex = MyArray(11)
!IDNumber = MyArray(12)

' !UploadIDCardCopy = MyArray(13)
!PHome = MyArray(14)
!PMobile = MyArray(15)
!PWork = MyArray(16)

!Email = MyArray(17)
!DOB = MyArray(18)
!guardianFname = MyArray(19)
!guardianLname = MyArray(20)



!GIDNumber = MyArray(21)
!ContactNumber = MyArray(22)
!GWork = MyArray(23)
!GSignature = MyArray(24)

!GCEOLResult = MyArray(25)
!HighestQualification = MyArray(26)
'!UploadCertificates = MyArray(27)
!workExperience = MyArray(28)

!HomeName = MyArray(29)
!ChooseAtoll = MyArray(30)




If (MyArray(31) <> "") Then
!Island = MyArray(31)

ElseIf (MyArray(32) <> "") Then
!Island = MyArray(32)

ElseIf (MyArray(33) <> "") Then
!Island = MyArray(33)

ElseIf (MyArray(34) <> "") Then
!Island = MyArray(34)

ElseIf (MyArray(35) <> "") Then
!Island = MyArray(35)

ElseIf (MyArray(36) <> "") Then
!Island = MyArray(36)

ElseIf (MyArray(37) <> "") Then
!Island = MyArray(37)


ElseIf (MyArray(38) <> "") Then
!Island = MyArray(38)

ElseIf (MyArray(39) <> "") Then
!Island = MyArray(39)

ElseIf (MyArray(40) <> "") Then
!Island = MyArray(40)

ElseIf (MyArray(41) <> "") Then
!Island = MyArray(41)

ElseIf (MyArray(42) <> "") Then
!Island = MyArray(42)

ElseIf (MyArray(43) <> "") Then
!Island = MyArray(43)


ElseIf (MyArray(44) <> "") Then
!Island = MyArray(44)

ElseIf (MyArray(45) <> "") Then
!Island = MyArray(45)

ElseIf (MyArray(46) <> "") Then
!Island = MyArray(46)

ElseIf (MyArray(47) <> "") Then
!Island = MyArray(47)


ElseIf (MyArray(48) <> "") Then
!Island = MyArray(48)


ElseIf (MyArray(49) <> "") Then
!Island = MyArray(49)


End If

!Avah = MyArray(50)

!ResidentialHouseName = MyArray(51)
!ChooseAtoll1 = MyArray(52)
!Island1 = MyArray(53)


If (MyArray(54) = "Checked") Then
!Avah1 = "V"

ElseIf (MyArray(55) = "Checked") Then
!Avah1 = "M"

ElseIf (MyArray(56) = "Checked") Then
!Avah1 = "Ma"

ElseIf (MyArray(57) = "Checked") Then
!Avah1 = "G"

ElseIf (MyArray(58) = "Checked") Then
!Avah1 = "H"

ElseIf (MyArray(59) = "Checked") Then
!Avah1 = "HM"


End If


!CourseCategory = MyArray(59)



If (MyArray(61) <> "") Then
!courseName = MyArray(61)

ElseIf (MyArray(62) <> "") Then
!courseName = MyArray(62)

ElseIf (MyArray(63) <> "") Then
!courseName = MyArray(63)


ElseIf (MyArray(64) <> "") Then
!courseName = MyArray(64)

ElseIf (MyArray(65) <> "") Then
!courseName = MyArray(65)


ElseIf (MyArray(66) <> "") Then
!courseName = MyArray(66)

End If

!promotionCode = MyArray(67)
!Signature1 = MyArray(68)

If (MyArray(69) = "Checked") Then
!agree = "Yes"

ElseIf (MyArray(69) = "") Then
!agree = "No"
End If



.Update
End With
i = i + 1
End If
Loop

objStream.Close
rs.Close
Set objStream = Nothing
Set rs = Nothing
Set db = Nothing

MsgBox "Uploaded " & i & " records"




End Sub

####Error comes with this code. !Avah = MyArray(50)

Thank you.
 
I've tried to re-jigg your code:
Code:
Private Sub Command0_Click()
 
    Dim MyArray     As Variant
    Dim fso         As Object
    Dim objStream   As Object
    Dim db          As DAO.Database
    Dim rs          As DAO.Recordset
    Dim i           As Integer
 
    Const FILE_PATH As String = "C:\Users\herath\Desktop\Online Application\exep.csv"
 
    Set fso = CreateObject("Scripting.FileSystemObject")
 
    If fso.FileExists(FILE_PATH) Then
        Set objStream = fso.OpenTextFile(FILE_PATH, 1, False, 0)
    Else
        Exit Sub
    End If
 
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("SELECT * FROM Table1", dbOpenDynaset)
 
    Do While Not objStream.AtEndOfStream
        MyArray = Split(objStream.ReadLine, ",")
 
        If UBound(MyArray) > 0 Then
            With rs
                .AddNew
                !experiance = MyArray(0)
                .Update
            End With
            i = i + 1
        End If
    Loop
 
    objStream.Close
    rs.Close
    Set objStream = Nothing
    Set rs = Nothing
    Set db = Nothing
 
    MsgBox "Uploaded " & i & " records"
 
End Sub
[/QUOT]

Thank you for your response.
When I try with my whole code set still I have same run time error..my whole code set is below,do you have any Idea??


Private Sub Command0_Click()



Dim MyArray As Variant
Dim fso As Object
Dim objStream As Object
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer

Const FILE_PATH As String = "C:\Users\herath\Desktop\Online Application\exep.csv"

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(FILE_PATH) Then
Set objStream = fso.OpenTextFile(FILE_PATH, 1, False, 0)
Else
Exit Sub
End If

Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT * FROM Users", dbOpenDynaset)

Do While Not objStream.AtEndOfStream
MyArray = Split(objStream.ReadLine, ",")

If UBound(MyArray) > 0 Then
With rs
.AddNew
' !indexNo = MyArray(0)
' !experiance = MyArray(1)
!entryNumber = MyArray(0)
!DateCreated = MyArray(1)
!dateUpdated = MyArray(2)
!Campus = MyArray(4)

!StudiedBeforeMCorMLC = MyArray(5)
!MCorMLCNumber = MyArray(6)
!Salutation = MyArray(7)
!Fname = MyArray(8)

!Mname = MyArray(9)
!Lname = MyArray(10)
!Sex = MyArray(11)
!IDNumber = MyArray(12)

' !UploadIDCardCopy = MyArray(13)
!PHome = MyArray(14)
!PMobile = MyArray(15)
!PWork = MyArray(16)

!Email = MyArray(17)
!DOB = MyArray(18)
!guardianFname = MyArray(19)
!guardianLname = MyArray(20)



!GIDNumber = MyArray(21)
!ContactNumber = MyArray(22)
!GWork = MyArray(23)
!GSignature = MyArray(24)

!GCEOLResult = MyArray(25)
!HighestQualification = MyArray(26)
'!UploadCertificates = MyArray(27)
!workExperience = MyArray(28)

!HomeName = MyArray(29)
!ChooseAtoll = MyArray(30)




If (MyArray(31) <> "") Then
!Island = MyArray(31)

ElseIf (MyArray(32) <> "") Then
!Island = MyArray(32)

ElseIf (MyArray(33) <> "") Then
!Island = MyArray(33)

ElseIf (MyArray(34) <> "") Then
!Island = MyArray(34)

ElseIf (MyArray(35) <> "") Then
!Island = MyArray(35)

ElseIf (MyArray(36) <> "") Then
!Island = MyArray(36)

ElseIf (MyArray(37) <> "") Then
!Island = MyArray(37)


ElseIf (MyArray(38) <> "") Then
!Island = MyArray(38)

ElseIf (MyArray(39) <> "") Then
!Island = MyArray(39)

ElseIf (MyArray(40) <> "") Then
!Island = MyArray(40)

ElseIf (MyArray(41) <> "") Then
!Island = MyArray(41)

ElseIf (MyArray(42) <> "") Then
!Island = MyArray(42)

ElseIf (MyArray(43) <> "") Then
!Island = MyArray(43)


ElseIf (MyArray(44) <> "") Then
!Island = MyArray(44)

ElseIf (MyArray(45) <> "") Then
!Island = MyArray(45)

ElseIf (MyArray(46) <> "") Then
!Island = MyArray(46)

ElseIf (MyArray(47) <> "") Then
!Island = MyArray(47)


ElseIf (MyArray(48) <> "") Then
!Island = MyArray(48)


ElseIf (MyArray(49) <> "") Then
!Island = MyArray(49)


End If

!Avah = MyArray(50)

!ResidentialHouseName = MyArray(51)
!ChooseAtoll1 = MyArray(52)
!Island1 = MyArray(53)


If (MyArray(54) = "Checked") Then
!Avah1 = "V"

ElseIf (MyArray(55) = "Checked") Then
!Avah1 = "M"

ElseIf (MyArray(56) = "Checked") Then
!Avah1 = "Ma"

ElseIf (MyArray(57) = "Checked") Then
!Avah1 = "G"

ElseIf (MyArray(58) = "Checked") Then
!Avah1 = "H"

ElseIf (MyArray(59) = "Checked") Then
!Avah1 = "HM"


End If


!CourseCategory = MyArray(59)



If (MyArray(61) <> "") Then
!courseName = MyArray(61)

ElseIf (MyArray(62) <> "") Then
!courseName = MyArray(62)

ElseIf (MyArray(63) <> "") Then
!courseName = MyArray(63)


ElseIf (MyArray(64) <> "") Then
!courseName = MyArray(64)

ElseIf (MyArray(65) <> "") Then
!courseName = MyArray(65)


ElseIf (MyArray(66) <> "") Then
!courseName = MyArray(66)

End If

!promotionCode = MyArray(67)
!Signature1 = MyArray(68)

If (MyArray(69) = "Checked") Then
!agree = "Yes"

ElseIf (MyArray(69) = "") Then
!agree = "No"
End If



.Update
End With
i = i + 1
End If
Loop

objStream.Close
rs.Close
Set objStream = Nothing
Set rs = Nothing
Set db = Nothing

MsgBox "Uploaded " & i & " records"




End Sub

####Error comes with this code. !Avah = MyArray(50)

Thank you.

It looks like the index of MyArray() is less than 50. Run the code again and when you get the error click on "Debug" and in the watch window add a Watch for "Ubound(MyArray)". If it shows <50, you have a problem. Your code will have to handle the unexpectedly low number of item in the stream.

Best,
Jiri
 
Did Jiri just quote that whole block of text :p

herath381, like Solo712 mentioned you will need code to handle the unexpectedly low number of items in the array and to do this you will need to loop through the array instead. Something like this:
Code:
        If UBound(myArray) > 0 Then
            With rs
                .AddNew
                
                For x = LBound(myArray) To UBound(myArray)
                    .Fields([COLOR="blue"]ResolveFieldName([/COLOR]x[COLOR="Blue"])[/COLOR]) = myArray(x)
                    End Select
                Next
                
                .Update
            End With
            i = i + 1
        End If
Create the ResolveFieldName() function that will return the name of the field as a string or as the number index.

Fyi: this is how we post code on this forum:
http://www.access-programmers.co.uk/forums/showthread.php?t=200247
 

Users who are viewing this thread

Back
Top Bottom