Solved

Appenfing to a TextFile from Access VBA

Posted on 2014-02-27
5
384 Views
Last Modified: 2014-03-04
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
Comment
Question by:Patrick O'Dea
5 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 200 total points
ID: 39893038
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
 
LVL 19

Assisted Solution

by:regmigrant
regmigrant earned 200 total points
ID: 39893043
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
 
LVL 38

Assisted Solution

by:Jim P.
Jim P. earned 100 total points
ID: 39893071
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
 

Author Comment

by:Patrick O'Dea
ID: 39893105
*** I will check this
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 39904742
Thanks folks,  I've got it now!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

773 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