Solved

Appenfing to a TextFile from Access VBA

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

752 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