Results 1 to 8 of 8

Thread: [RESOLVED] write to txt file

  1. #1

    Thread Starter
    Addicted Member scottlafoy's Avatar
    Join Date
    Feb 2009
    Location
    Calgary Alberta, Canada
    Posts
    148

    Resolved [RESOLVED] write to txt file

    I am trying to write my text1 box to a txt file, except I want the last line entered to be the first line in the txt file. The newest will always be on top. so if I entered 1 the txt file would say,
    1
    if I then enter 2 the txt file should say,
    2
    1

    Open App.Path & "\list1.txt" For Append As #1
    Print #1, Text1.Text
    Close #1
    how could I alter this code to fit my needs?
    C:\DOS
    C:\DOS\RUN
    RUN\DOS\RUN

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: write to txt file

    You can declare an array and save the entered data into the array
    Then when there are no more data to enter, using For-Next from last cell to first, write to Your txt file
    Last edited by jggtz; Mar 15th, 2009 at 02:23 PM.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: write to txt file

    More information on how your text is entered into the textbox would be helpful. If each line is has a carriage return, then
    1. Split() the Textbox text on carriage returns: Split(Text1.Text, vbCrLf)
    2. Loop thru & write the array returned by Split, in reverse order

    There are API approaches too, but maybe not needed.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: write to txt file

    (1) Open the text file in binary mode and read all the data as a string.

    (2) Append this data string to the last line in the text box string.

    (3) Write the concatenated string back to file at byte 1.
    Doctor Ed

  5. #5

    Thread Starter
    Addicted Member scottlafoy's Avatar
    Join Date
    Feb 2009
    Location
    Calgary Alberta, Canada
    Posts
    148

    Re: write to txt file

    I will give a little more info on what I am trying to do, and go try some of these ideas. I have a text box I will enter short names. The text box get written to the txt file with the code I had above. Then a list box is filled from the names in the txt file. The names are folder names, and I would like to put the last name entered in the text box on the top of the list. Each time the program is used the names are loaded into the listbox from the txt file. Below is the code I use to load the list box.
    Code:
    Dim ff As Long
    Dim line As String
    ff = FreeFile
    List2.Clear
    Open App.Path & "\list1.txt" For Input As #ff
    Do While Not EOF(ff)
    Line Input #ff, line
    If Len(line) Then List2.AddItem line
    Loop
    Close #ff
    C:\DOS
    C:\DOS\RUN
    RUN\DOS\RUN

  6. #6
    Addicted Member
    Join Date
    Jul 2007
    Posts
    228

    Re: write to txt file

    Perhaps a better way would be to:

    1. Add the names to the list box (list1.additem text1.text, 0) setting the index each time to be on top of the list.

    2. Save the list box items when exiting your program. Then when needed they will be loaded in the order you desire.

    There really is no need to write the file every time an entry is made.

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: write to txt file

    1. This is the way to append new data on top of the file, but it should not be use so often, perhaps just on app closing.
    Code:
       Dim sFName As String
       Dim sOldData As String
       Dim f As Integer
       
       sFName = App.Path & "\list1.txt"
       If Dir(sFName) <> "" Then
          f = FreeFile
          Open sFName For Input As #f
          sOldData = Input(LOF(f), f)
          Close #f
       End If
       f = FreeFile
       Open sFName For Output As #f
       Print #f, Text1.Text
       If sOldData <> "" Then
          Print #f, sOldData;
       End If
       Close #f
    2. Append new text at the bottom of the file as normal then to fill your listbox in reverse order next time from the text file (as Tom Moral mentioned):
    Code:
       Dim sFName As String
       Dim ff As Integer
       Dim sLine As String
       
       sFName = App.Path & "\list1.txt"
       ff = FreeFile
       List2.Clear
       Open sFName For Input As #ff
       Do While Not EOF(ff)
          Line Input #ff, sLine
          If Len(sLine) Then List2.AddItem sLine, 0 '-- add new line on top
       Loop
       Close #ff
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8

    Thread Starter
    Addicted Member scottlafoy's Avatar
    Join Date
    Feb 2009
    Location
    Calgary Alberta, Canada
    Posts
    148

    Re: write to txt file

    Thank you all, I got my listbox working just how I want it!
    C:\DOS
    C:\DOS\RUN
    RUN\DOS\RUN

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