Logfile

R

requeth

Ok, so I'm creating an onclick backup logfile that will run a query and
capture a record and write it to a text file before running an sql
update procedure to modify the database. I one function that runs to
see if the file exists or not. If it does not exist, the file is
created, and the data is written to the file. If the file already
exists (and this is the broken part), the file should be opened, and
the data appended, then the file should be closed. For some reason I am
receiving "Invalid procedure call or argument" when I attempt to run
the opentxtfile sub to append to the file. I have attached the code
below and put a >> where the error is flagging.

Option Compare Database
'myspec is the specification for output. To create this choose the
table/tables you will be exporting from and
'go to file: Export File. Choose save.
'Choose Advanced, change any settings you like and choose and choose
save as.
'Name specification, in this case as myspec.
Function output1() As String
DoCmd.TransferText acExportDelim, myspec, "query1",
"F:\Test1\Test2.txt"
End Function
Sub opentxtfile()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, a
Set fs = CreateObject("Scripting.FileSystemObject")a.Write "Hello world!"
a.Close

End Sub
Function FileExists() As String
'runs onclick to create a backup log.
Dim fso
Dim file As String
file = "F:\Test1\Test2.txt" 'log path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(file) Then
'if file doesent exist run output1 function to create file and populate
data
output1
Else
'if file exists run opentxtfile sub which appends data to the already
created logfile.
opentxtfile
End If

End Function
 
R

requeth

I did some poking around through help files and realized most of my
code was not required since I can choose to create the file it it
doesent exist. I'm still receiving the same error message, but at least
now I have less code to work with. The code remaining was taken out of
hte help files almost character for character, and I can not get it to
run. Any ideas?

Option Compare Database
Sub opentxtfile()
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("F:\Test1\Test2.txt", ForAppending, True,
TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
Function runlog() As String
opentxtfile
End Function
 
R

requeth

I figured it out. The sample file was incorrect. I am attaching the
final code for future reference.

Sub opentxtfile()
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("F:\Test1\Test2.txt", 8, True, 0)
f.Write "Hello world!"
f.Close
End Sub
Function runlog() As String
opentxtfile
End Function
 

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