?
Solved

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

Posted on 2003-03-22
4
Medium Priority
?
126 Views
Last Modified: 2010-05-01
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?
0
Comment
Question by:jm_jackson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 400 total points
ID: 8188182
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
 
LVL 2

Expert Comment

by:Jacamar
ID: 8188913
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
 

Expert Comment

by:MrDark
ID: 8190273
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
 

Author Comment

by:jm_jackson
ID: 8190320
Thanks guys. All good methods. Used vinnyd79's method in the end so points go to you! Thanks again.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question