0

I have code that will list tables names, how can I export this to a text file?

 For Each tbl In db.TableDefs
 If Left$(tbl.Name, 4) <> "MSys" Then
   Debug.Print tbl.Name & "      " & tbl.DateCreated & "      " & _
    tbl.LastUpdated & "     " & tbl.RecordCount
  • possible duplicate of [How to create and write to a txt file using VBA](http://stackoverflow.com/questions/11503174/how-to-create-and-write-to-a-txt-file-using-vba) – David Zemens Jul 22 '13 at 17:43

3 Answers3

0

You can use simple File I/O to write to a textfile. MSDN: Write# Statement

Here is the example from that page:

Open "TESTFILE" For Output As #1    ' Open file for output.
Write #1, "Hello World", 234    ' Write comma-delimited data.
Write #1,    ' Write blank line.

Dim MyBool, MyDate, MyNull, MyError
' Assign Boolean, Date, Null, and Error values.
MyBool = False: MyDate = #2/12/1969#: MyNull = Null
MyError = CVErr(32767)
' Boolean data is written as #TRUE# or #FALSE#. Date literals are
' written in universal date format, for example, #1994-07-13#
 'represents July 13, 1994. Null data is written as #NULL#.
' Error data is written as #ERROR errorcode#.
Write #1, MyBool; " is a Boolean value"
Write #1, MyDate; " is a date"
Write #1, MyNull; " is a null value"
Write #1, MyError; " is an error value"
Close #1    ' Close file.

Change the file name, and extension, to, for example, "C:\SomeFolder\myfile.txt".

There are other, more sophisticated, ways to do this, including using the FileSystemObject as shown in the link David provided.

Andy G
  • 19,232
  • 5
  • 47
  • 69
0

See the MSDN article on how to create a text file:

http://msdn.microsoft.com/en-us/library/aa265018(v=vs.60).aspx

Modified slightly for your needs, you will have to tweak it to define db and TableDefs etc:

Sub CreateAfile
    Dim fs as Object, a as Object
    Dim lineText as String
    #Create and open text file for writing:
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\testfile.txt", True)
    '#Iterate over your TableDefs
    For Each tbl In db.TableDefs
         If Left$(tbl.Name, 4) <> "MSys" Then
              lineText = tbl.Name & "      " & tbl.DateCreated & "      " & _
              tbl.LastUpdated & "     " & tbl.RecordCount

              '# Adds a line to the text file
              a.WriteLine(lineText)
         End If
    Next
    '#Close the textfile
    a.Close
End Sub
David Zemens
  • 53,033
  • 11
  • 81
  • 130
0

This will work as a straight copy/paste. Just change the output file name to whatever you want. It outputs the metadata you requested line by line toa .txt

  Dim db As DAO.Database
  Set db = CurrentDb


  Dim filename As String
  filename = "C:\Users\Scotch\Desktop\now\t.txt"     'add your file name here
  Const ForReading = 1, ForWriting = 2, ForAppending = 3
  Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
  Dim fs, f, ts, s
  Set fs = CreateObject("Scripting.FileSystemObject")
  fs.CreateTextFile filename 'Create a file
  Set f = fs.GetFile(filename)
  Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)



   For Each tbl In db.TableDefs
         If Left$(tbl.name, 4) <> "MSys" Then
           ts.Write tbl.name & "      " & tbl.DateCreated & "      " & _
           tbl.LastUpdated & "     " & tbl.RecordCount & vbNewLine
         End If

    Next
    ts.Close
Scotch
  • 3,186
  • 11
  • 35
  • 50