Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Private Sub Combofullname_LostFocus()
Dim temp As Variant
If Text2.Text = Empty Then Exit Sub
temp = Split(Combo14.Text, " ")
   If temp(0) <> Empty Then
   
   If UBound(temp) Then
        Text2.Text = temp(0)
 Else
 If UBound(temp) Then
       Text3.Text = temp(1)
 Else
 If UBound(temp) Then
        Text4.Text = temp(2)
    End If
    End If
    End If
    End If
End Sub

When I run this code it shows error subscript out of range.
Please help
Posted
Updated 4-Jan-13 20:21pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Jan-13 2:21am    
Any specific reason to use VB6? This is a complete lame which Microsoft tried to get forgotten, working hard for it. And Variant is extreme lame, also long gone, pretty much. Do you like the dead? :-)
—SA

Variant… Oh, gosh!

Anyway, here is your bug:

The call to Split may give an array with just one element. Than temp(1) or temp(2) will address non-existing array element.

—SA
 
Share this answer
 
Comments
surkhi 5-Jan-13 2:43am    
Should I use string instead of variant data-type?
Sergey Alexandrovich Kryukov 5-Jan-13 19:38pm    
Certainly, you should use strings, integer types, floating-point types, and so on; always the types which represent the data in the best ways for its purpose.
—SA
You need to make sure that the index is correct and in range. Make sure, post split, you do have 0,1,2 indexes. Just DEBUG and see where and when it fails.

Here, have a look at these:
MSDN: Subscript out of range (Error 9)[^]
MSDN: Subscript out of range (Visual Basic) [^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900