reading parts of text files into a list box...

Hi, i've got the code below to read lines from a text/.dat file into a list box.

  Open "file.dat" For Input As #1
  Do Until intLoop = 20
    intLoop = intLoop + 1
    ReDim Preserve strOption(intLoop)
    Line Input #1, strOption(intLoop)
    lstDefinitions.AddItem Left(strOption(intLoop), 40)
  Loop
  Close #1

This obviously reads the first 20 lines into the listbox, but how would i for example read the second 20 (lines 21 - 40) into the listbox?
jm_jacksonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
vinnyd79Connect With a Mentor Commented:
probably a better way,but this should work:

Open "file.dat" For Input As #1
 Do Until intLoop = 40
   intLoop = intLoop + 1
   ReDim Preserve strOption(intLoop)
   Line Input #1, strOption(intLoop)
    If intLoop > 20 Then
        lstDefinitions.AddItem Left(strOption(intLoop), 40)
    End If
 Loop
 Close #1
0
 
JacamarCommented:
Well, first off, you don't need to redim preserve your variable everytime, since it seems like you have a set number of lines (40).  If you are going to be unaware of the number of lines, but would still like any lines after 20 to be added, that is still possible.  here are two examples.

1. Known number of items

Open "file.dat" For Input as #1
Redim strOption(1 to 40)
For intLoop = 1 to 40
  Line Input #1, strOption(intLoop)
  if intLoop > 20 then lstDefinitions.AddItem Left(strOption(intLoop), 40)
Next intLoop
Close #1

2. Unknown number of items

Open "file.dat" For Input as #1
Do Until EOF(1)
  intLoop = intLoop + 1
  Redim Preserve strOption (1 to intLoop)
  Line Input #1, strOption(intLoop)
  if intLoop > 20 then lstDefinitions.AddItem Left(strOption(intLoop), 40)
Loop
Close #1


I hope these are of help.
0
 
MrDarkCommented:
Why don't you make it in a separate procedure and passes it the number of line to begin and the number of the end so you code will be like this

Private Sub read_file(from_line As Integer, to_line As Integer)
Open "c:\file.dat" For Input As #1
 Do Until from_line = to_line
   from_line = to_line + 1
   ReDim Preserve strOption(from_line)
   Line Input #1, strOption(from_line)
   lstDefinitions.AddItem Left(strOption(from_line), 40)
 Loop
 Close #1
End Sub
0
 
jm_jacksonAuthor Commented:
Thanks guys. All good methods. Used vinnyd79's method in the end so points go to you! Thanks again.
0
All Courses

From novice to tech pro — start learning today.