?
Solved

Read a text file

Posted on 2003-03-25
9
Medium Priority
?
183 Views
Last Modified: 2010-05-01
Hello,

Well I have a *.txt file, that contains just 1 line, but its size is more then 26500 bytes, so when I read it into a text field it doesnt work.  Or at least, not the whole file is read.  So is there a way to read the whole file?  I need all the contents of that single line and not just the first 26500 bytes (I think 26500 was the number of bytes)

Please help me

0
Comment
Question by:Stofferken
9 Comments
 
LVL 9

Accepted Solution

by:
GivenRandy earned 200 total points
ID: 8206182
To (quickly) read a file into a single string:

---
    Dim F As Integer
    Dim S As String

    F = FreeFile
    Open "YourFileName" For Binary As #F
    S = Space$(LOF(F))
    Get #F, , S
    Close #F
---

To read a text file and look at each line:

---
    Dim F As Integer
    Dim S As String

    On Error Resume Next
    F = FreeFile
    Open "YourFileName" For Input As #F
    If (Err.Number = 0) Then
        Do While (Not EOF(F))
            Line Input #F, S
            ' do something with S
        Loop
    End If
    Close #F
---

To read a tab-delimited (or other delimiter) file and look at each item in each line:

---
Private Sub Form_Load()
    Dim F As Integer
    Dim S As String
    Dim sArray() As String
    Dim Counter As Long

    On Error Resume Next
    F = FreeFile
    Open "YourFileName" For Input As #F
    If (Err.Number = 0) Then
        Do While (Not EOF(F))
            Line Input #F, S
            sArray = Split(S, vbTab) ' tab delimited, others are possible
            For Counter = 0 To UBound(sArray)
                Debug.Print sArray(Counter) ' do something with each element
            Next
        Loop
    End If
    Close #F
End Sub
---
0
 

Author Comment

by:Stofferken
ID: 8206241
@ GivenRandy
Well, my file is too big to do that, thats why I need help.
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8206287
a string can hold up to 2 billion characters
0
Industry Leaders: 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!

 
LVL 38

Expert Comment

by:PaulHews
ID: 8206443
How big is your file?
0
 
LVL 9

Expert Comment

by:dancebert
ID: 8206478
I use the TextStream object to handle text files instead of the 'Classic VB' Freefile - Open - Close statements.  However, I have no idea if TextStream has greater capacity then the classic way.  Worth a try.

You must include a refrence to Microsoft Scripting Runtime in your project to see the TextStream object.
0
 
LVL 3

Expert Comment

by:DocM
ID: 8207172

Use the RichTextBox instead of the TextBox. It has a much larger capacity.

The coding is simple:
RichTextBox1.LoadFile "FileName"
0
 

Expert Comment

by:Sandwich
ID: 8207687
try this one =P

Dim cPath as string
Dim cLineData as string

cPath = <Path> + <filename>

Open cPath For Input As #1
    While Not EOF(1)
        Line Input #1, cLineData
        With frm
            If Trim(.rtb.Text = "") Then
                .rtb.Text = cLineData + vbCrLf
            Else
                .rtb.Text = .rtb.Text + cLineData + vbCrLf
            End If
        End with
    Wend
Close #1

*rtb - RichTextbox control
0
 

Expert Comment

by:statto
ID: 8211987
You're trying to write it into a text field, ie Text1.Text, right? Or a string, ie stringname? or a Label?
0
 

Author Comment

by:Stofferken
ID: 8212026
Ok, it now works with a string
thx
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

615 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