Solved

Appenfing to a TextFile from Access VBA

Posted on 2014-02-27
5
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

740 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