Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA insert a line into a text file 2

Status
Not open for further replies.

misubud

IS-IT--Management
May 29, 2002
25
NZ
I've tried different things but can't get this to work.

I have a text file (call it "file1.txt")
"file1.txt" has the following text lines;

line ref:aaa
line ref:bbb
line ref:fff

I have a check string in an forms textbox;
i.e. ddd

I want to be able to open the text file and add the check string into the file at the appropriate alphabetical place.
i.e.

the files 4 lines would read as follows;

line ref:aaa
line ref:bbb
line ref:ddd
line ref:fff


hope this makes sense and someone can help me out.





 
there is no way around this.

read the whole file, line by line, and write each record into a temporary file.

At the stage where you would need you NEW record inserted you do insert this one, and then keep reading the remaining and writing to the new file.
At the end you copy the new temp file into the old file.

(or delete the old one and rename the new one)

Another alternative (if the file is not too big) is to load the whole file into a memory array, and then write it from there.

There have been a few threads about this same subject recently. Please search the forums if you need some code examples.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
misubud,
IS/IT--Management? Here is a starting point.

SkipVought,
If your refering to the thread I'm thinking of.
Response yes: Answer, not really.

Code:
[navy]Option Compare Text[/navy]

[navy]Sub [/navy] InsertLine(YourFileName [navy]As String[/navy], YourNewString [navy]As String[/navy])
[navy]Dim[/navy] intInput [navy]As Integer[/navy], inttOutput [navy]As Integer[/navy]
[navy]Dim[/navy] strBuffer [navy]As String[/navy]

[green]'Create a temporary file that hAs the data since we will[/green]
[green]'overwrite the original[/green]
FileCopy YourFileName, YourFileName & ".tmp"

[green]'Open the Input file[/green]
intInput = FreeFile
[navy]Open[/navy] YourFileName & ".tmp" [navy]For Input As[/navy] #intInput

[green]'Open the output file[/green]
intOutput = FreeFile
[navy]Open[/navy] YourFileName [navy]For Output As[/navy] #intOutput

[green]'Loop through the Input file[/green]
Do
  [green]'Get a line from the temporary file[/green]
  [navy]Line Input[/navy] #intInput, strBuffer
  
  [green]'Test here To see If you write the Input data or your new data[/green]
  [navy]If[/navy] YourNewString < strBuffer [navy]Then[/navy]
    [green]'Write the Input String[/green]
    [navy]Print[/navy] #intOutput, strBuffer
  [navy]Else[/navy]
    [green]'Write the new String, Then write the Next String[/green]
    [navy]Print[/navy] #intOutput, YourNewString
    [navy]Print[/navy] #intOutput, strBuffer
  [navy]End If[/navy]
[navy]Loop Until[/navy] EOF(intInput)

[green]'Close both files[/green]
Reset
[green]'Delete the temporary file[/green]
Kill YourFileName & ".tmp"
[navy]End Sub [/navy]

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Thanks guys..
"there is no way around this.

read the whole file, line by line, and write each record into a temporary file.
"

That's what I needed someone to tell me.
I was under the false impression that you could insert into an existing file.

Some well worded and helpful responses.
Thanks

 
I was under the false impression that you could insert into an existing file.
This is not a false impression. You CAN insert into an existing file. However, you want to insert it into a specific place using a sort of the existing lines. This is quite a different kettle of fish.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top