Solved

Writing to text files

Posted on 2004-09-28
4
8,221 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

829 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