Solved

Writing to text files

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now