Solved

Appenfing to a TextFile from Access VBA

Posted on 2014-02-27
5
377 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 119

Accepted Solution

by:
Rey Obrero earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
*** I will check this
0
 

Author Closing Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 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

10 Experts available now in Live!

Get 1:1 Help Now