How do you open a file for appending data?

M

M Shafaat

Hi,
I am trying to write data from SEVERAL tables in an Access application into
a SINGLE text file, using the VBA code and the keyword "ForAppending",
without success.



The following code with the keyword "ForAppending" does not just function,
despite the VBA Help presents it as example:



Sub OpenTextFileTest

Const ForReading = 1, ForWriting = 2, ForAppending = 3

Dim fs, f



Set fs = CreateObject("Scripting.FileSystemObject")



' HERE IS THE STATEMENT THAT DOESN'T FUNCTION::

Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)



f.Write "Hello world!"

f.Close

End Sub



How can I go around this problem?
I am using Windows XP Pro SP2 and MS Access 2003 SP2



Ragards
M Shafaat
 
J

John Nurick

1) Either the help page you found is wrong or you misread it. You're
passing the wrong arguments in the OpenTextFile() call.

You've defined ForAppending wrongly, haven't defined TristateFalse,
and are passing a tristate constant as the "Create" argument. See
http://msdn2.microsoft.com/en-us/library/314cz14s.aspx for more.

2) The fact you've got away without declaring TristateFalse suggests
that you're not declaring Option Explicit at the beginning of the
module. Do so. It saves a lot of trouble.

3) Many experts prefer to use the native VB/VBA file I/O statements
rather than the Microsoft Scripting Runtime library. The code is
usually simpler and more direct:

Dim FN As Long

FN = FreeFile()
Open "C:\TestFile.txt" For Append As #FN
Print #FN, "Hello World"; ' semicolon suppresses newline
' like using f.Write rather than
' f.WriteLine
Close FN


And next time, please give details of the error messsage your getting.
it makes things easier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top