Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: Access Reports FAQ

How To

Write to external text file by randysmid
Posted: 23 Jun 03 (Edited 23 Oct 03)

Here is a bit of code that you can use to write things to an external text file.  (I've included some optional code to delete the external text file too)  This code will only write one line to the text file, but if you want multiple lines, simply put the "write" statement inside a loop.

 dim strList as String
  'loop through calculations here
  '   assign the values, e.g., "A", "B", etc. to strList
  '  example:  strList = strList & strNewValue
  Open "c:\List.txt" For Output As #1
  Write #1, strList
  Close #1

NOTE: with the example above, you may want to keep track of the number of characters contained within strList, so that your line doesn't exceed the width of your printer.  In that case, you can use the "len" function as follows:
  If len(strList) > 70 Then
    Write #1, strList
    strList = ""
  End If

The following example creates a custom filename using the current date, and then opens the output file.
  Dim strFileName As String
 'it is necessary to use "DatePart" because "Date"
 ' would return a value that includes slash marks,
 ' which is interpreted by VB/VBA to be a folder name
 strFileName = "c:\TEST" & DatePart("m", Date) & DatePart("dd", Date) & DatePart("yyyy", Date) & ".txt"
 Open strFileName For Output As #1

It isn't so straightforward to delete files in Access.  It will be necessary to declare a FileSystemObject.  Here is the code:
  Dim fso As Object, fsoFile As Object
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set fsoFile = fso.GetFile("c:\TEST" & DatePart("m", Date) & DatePart ("dd", Date) & DatePart("yyyy", Date) & ".txt")

Feedback, comments?  
Randy Smith, MCP


Back to Microsoft: Access Reports FAQ Index
Back to Microsoft: Access Reports Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close