Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

Appenfing to a TextFile from Access VBA

Hi,

I am adding a new line to my "audit" log in Access VBA.

Each time a change is made to may data I want to APPEND to a text file.

How do I do this?

(Appending is the key thing ... i.e. the file will get bigger and bigger !)
0
Patrick O'Dea
Asked:
Patrick O'Dea
3 Solutions
 
Rey Obrero (Capricorn1)Commented:
here is the basic code

dim txtFile as string
txtFile="<path to text file>\myText.txt"

open txtfile for append as #1

' build the string you want to append here

print #1, <string>


close #1
0
 
regmigrantCommented:
When you open the file you need to 'open for append' and it depends which method you are using to open/create it
the simplest version is :
 
  Open LogFileName For Append As #FileNum ' creates or opens
    Print #FileNum, LogMessage ' write information at the end 
    Close #FileNum ' close the file

Open in new window



If you are using FSO set iomode = 8 (append) in the following

 
Set fso = CreateObject("Scripting.FileSystemObject")
' Create the file, and obtain a file object for the file.
FileName = "c:\Users\you\Desktop\testfile.txt"
fso.CreateTextFile FileName
Set fileObj = fso.GetFile(FileName)
' Open a text stream for output.
Set a = fileObj.OpenAsTextStream(iomode, TristateTrue)

Open in new window

0
 
Jim P.Commented:
Open Statement
Public Function OutputData(FileNameAndPath As String, ParamA As String, ParamB As String)
Dim DB As Database
Dim RS As Recordset
Dim SQL As String

Dim FileNum As Integer
Dim OutputLine As String

FileNum = FreeFile()

'The SQL query to open the recordset
SQL = "SELECT * " & _
    "FROM MyTable " & _
    "WHERE Field1 = '" & ParamA & "' " & _
    "AND Field2 = '" & ParamB & "'"

Set DB = CurrentDb()                'Use the current database
Set RS = DB.OpenRecordset(SQL)      'actually open the recordset

If RS.EOF = False Then
    RS.MoveFirst
Else
    MsgBox "No Data", vbExclamation, "Exiting Function"
    Set RS = Nothing
    Set DB = Nothing
    Exit Function
End If

'Open the file for output
'Change the "Append" to "Output" to replace the file instead.
Open FileNameAndPath For Append Access Write Lock Write As FileNum

Do Until RS.EOF
    'Do it this way if you want it comma delimited
    OutputLine = RS!Field1 & ","
    OutputLine = OutputLine & RS!Field2 & ","
    OutputLine = OutputLine & RS!Field3 & ","
    OutputLine = OutputLine & RS!Field4
    'Do it this way if you want it space delimited (fixed width)
    OutputLine = RS!Field1 & Space(50 - Len(RS!Field1))
    OutputLine = OutputLine & RS!Field2 & Space(50 - Len(RS!Field2))
    OutputLine = OutputLine & RS!Field3 & Space(50 - Len(RS!Field3))
    OutputLine = OutputLine & RS!Field4 & Space(50 - Len(RS!Field4))
    'Output the line
    Print #FileNum, OutputLine
    RS.MoveNext
    OutputLine = ""
Loop

Close #FileNum
Set RS = Nothing
Set DB = Nothing

End Function

Open in new window

0
 
Patrick O'DeaAuthor Commented:
*** I will check this
0
 
Patrick O'DeaAuthor Commented:
Thanks folks,  I've got it now!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now