Link to home
Start Free TrialLog in
Avatar of jm_jackson
jm_jackson

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jacamar
Jacamar

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.
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
Avatar of jm_jackson

ASKER

Thanks guys. All good methods. Used vinnyd79's method in the end so points go to you! Thanks again.