Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 - Split function - "Subscript out of range" 2

Status
Not open for further replies.

sankalpa

Instructor
May 12, 2003
8
GB
My program:
(I don't have the code with me but as I remember it:

Dim myarray()As String
Dim Part1, Part2, Part3 As String

...... openfile (containing lines e.g.: 'word1|Word1|werd1')
.......Start Loop .. (While not EOF)
.............read line into Inputstring

myarray() = split(Inputstring,"|")
Part1 = myarray(0)
Part2 = myarray(1)
Part3 = myarray(2)

....... then do things with Part1, Part2, Part3
.......Loop (While)

results in Error 9 "Subscript out of range"
(I haven't got Option Base set, and use the 0 subscript elsewhere in the program)

I can probably make a workaround using the Midstr function
but I am curious to know why the above is happening. A web search turned this up as a known problem but without any
answers to speak of.
 
Try looping through the generated array like so:

for x=lbound(myarray) to ubound(myarray)
next x

to prevent passing the boundaries.

Greetings,
Rick
 
I tried it like this:

Dim myarray() As String
Dim Part1, Part2, Part3, InputString As String

InputString = "word1|Word1|werd1"
myarray() = Split(InputString, "|")
Part1 = myarray(0)
Part2 = myarray(1)
Part3 = myarray(2)

And it ran fine. What line does your error come up on?

Robert
 
Thanks Rick and Robert

Using a loop - for x=lbound(myarray) to ubound(myarray)
won't fit the logic in my prog, since I need to get at each part of the line at different times - out of sequence.
As Robert's program shows, it should work just accessing the subscripts - there are always three parts.
Robert,
I did do a similar test before using the code. The problem is when it's placed inside a loop. The loop is a 'while not eof'. The program and loop worked fine with VB4 code in it, and that code worked fine when I transferred it to VB6 - The problem occurred when the Split function was added - and as it happens it works most of the time at home, but none of the time at work. I have files with different word sizes e.g. with lines such as: 4chars|4chars|4chars.
At home, the program works for all files except with the file where the lines are 5chars|5chars|5chars - but here it doesn't work at all. I guess it's time for me to learn how to use the debugger .. - I just hoped someone else had come across this problem.
 
Once you start debugging I think you may well find that the problem is caused by the fact that some lines are not always three parts.
 
Stongm

thankyou .. I believe you are right. I did check a number of times - but have now used a different set of files, and what do you know? No problem!

My apologies to Rick and Robert if I wasted their time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top