Solved

Writing to text files

Posted on 2004-09-28
4
8,220 Views
Last Modified: 2012-08-14
Hello, Experts.  The code bellow writes 2 line to a text file.

open ("c:\document.txt") for output as #1
print #1, "A line of text"
print #1, "Another line of text"
close #1

How can I make the next time i add text to the file for the text to start on the third (first empty row) line?

Can somebody point me to a good VB/VBA text editing site, i need to write and read plain text and CSV text files.

Thanks
0
Comment
Question by:sublimation
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
bingie earned 300 total points
ID: 12168918
open ("c:\document.txt") for append as #1
print #1, "A line of text"
print #1, "Another line of text"
close #1

Change "output" to "append" and it will append the file next time, otherwise it will over write any data in the file.
0
 
LVL 13

Assisted Solution

by:imarshad
imarshad earned 200 total points
ID: 12168992
Also you may like to have a look at a similar question that I asked.......I got a good Class Library code from sudhakar_koundinya....It is an absolutely brilliant Library for different functions for Text Files......
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21100341.html

Imran Arshad
0
 
LVL 13

Expert Comment

by:imarshad
ID: 12169036
You should add a Class Module in your code and then copy the whole code over there.......

Then you can use the following code......

Private Sub Command1_Click()
Dim o As New TextFile
Dim str As String
o.TextFilePath = "c:\a.txt"
o.WriteNewLine "A line of text"
o.WriteNewLine "Another line of text"
End Sub


with many other features as well........

Imran Arshad
0
 
LVL 9

Expert Comment

by:experts1
ID: 12437129
Try this modification to your code.

This method uses a subroutine to add(append) new text
to the file "document.txt".

Note:
The file is opened for "Append" in subroutine.
     
open ("c:\document.txt") for output as #1
print #1, "A line of text"
print #1, "Another line of text"
close #1

Call AddLine("Some new text")

Private Sub AddLine(ByVal newtext As String)
Open ("c:\document.txt") For Append As #1
  Print #1, newtext
Close #1
End Sub

The above method uses a subroutine to append new text
to the file "document.txt".

Have Fun!!
Experts1
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

803 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