Results 1 to 5 of 5

Thread: Re: Classic VB - How can I read/write a text file?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Classic VB - How can I read/write a text file?

    Hi for some reason when i do this, i put text1.text = Text1.Text = sFileText

    and it only loads the last line ?

    Any ideas (I'm using the first code example you gave!)

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Classic VB - How can I read/write a text file?

    Post moved from the FAQ forum, which is not the place to post your questions.

    This is the thread it was moved from: http://www.vbforums.com/showthread.p...te-a-text-file

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Classic VB - How can I read/write a text file?

    vb Code:
    1. Do While Not EOF(iFileNo)
    2.         Input #iFileNo, sFileText
    3.           'show the text (you will probably want to replace this line as appropriate to your program!)
    4.             Text1.Text = Text1.Text & sFileText
    5.       Loop

    You got "=" and "&" muddled up.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Classic VB - How can I read/write a text file?

    I would suggest a little different approach. Using input # in such a way would be rather slow and depending on the file may not give you the result you are looking for.

    For example if the file you are reading is a CSV file Input # will strip out all the commas, if the file is multiline then it will also strip out of the CRLFs.

    You can use a variant of the Input# method to read the entire file at once and place the whole file in the text box which is much faster and does not strip out any characters.

    You can also use Line Input# method to read each line which wil also strip out CRLFs but it you can easily add them as you go because you will always be able to tell where they should be. This is not the case with the plain Input# method since it breaks up the data on commas and CRLFs there is no way to properly reconstruct a CSV file in a text box unless you know the structure and add all the commas and CRLFs back as you read it.

  5. #5
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Classic VB - How can I read/write a text file?

    what DataMiser wrote is true... however i like to load line by line and is still really fast here is an approach like nightwalker wrote but he forget two details (in red)


    Code:
    Open "C:\TextFile.txt" For Input As #1
        Do Until EOF(1)
          Line Input #1, TextByLine
            Text1.Text = Text1.Text & TextByLine & vbNewLine
        Loop
        Text1.Text = Left(Text1, Len(Text1) - 1) 'Remove Last vbNewLine
      Close #1
    here is something you can try
    you can remove my If Text1.Multiline = False... End if it is not necessary

    Code:
    Private Sub Command1_Click()
    Dim TextByLine As String
      If Text1.MultiLine = False Then
        MsgBox "Please make Text1 Multiline 'True' at design time!", vbCritical, "Error"
        Exit Sub
      End If
    Text1.Text = ""
      Open "C:\TextFile.txt" For Input As #1
        Do Until EOF(1)
          Line Input #1, TextByLine
            Text1.Text = Text1.Text & TextByLine & vbNewLine
        Loop
        Text1.Text = Left(Text1, Len(Text1) - 1) 'Remove Last vbNewLine
      Close #1
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width