|
-
Mar 15th, 2009, 02:11 PM
#1
Thread Starter
Addicted Member
[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
-
Mar 15th, 2009, 02:19 PM
#2
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.
-
Mar 15th, 2009, 06:48 PM
#3
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.
-
Mar 15th, 2009, 08:53 PM
#4
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.
-
Mar 15th, 2009, 11:02 PM
#5
Thread Starter
Addicted Member
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
-
Mar 15th, 2009, 11:36 PM
#6
Addicted Member
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.
-
Mar 16th, 2009, 06:49 AM
#7
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
-
Mar 17th, 2009, 10:21 AM
#8
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|