Private Sub bExportAsText_Click()
Application.SaveAsText acTable, "YourTableName", "C:\YourTableName.txt"
Application.SaveAsText acQuery, "YourQueryName", "C:\YourQueryName.txt"
Application.SaveAsText acReport, "YourReportName", "C:\YourReportName.txt"
Application.SaveAsText acModule, "YourModuleName", "C:\YourModuleName.txt"
Application.SaveAsText acForm, "YourFormName", "C:\YourFormName.txt"
Application.SaveAsText acMacro, "YourMacroName", "C:\YourMacroName.txt"
' Application.LoadFromText acMacro, "YourMacroName", "C:\YourMacroName.txt" 'import the text file
End Sub
My apologies Pat. You can not use the SaveAsText method for tables, you have to use the TransferText method to export the table to a text file. The below code works for exporting queries, reports, forms, modules, macros, and the form/report module code as well. This definitely works for Access 97 and I trust for the newer versions as well.Pat Hartman said:Are you certain that this works with Access? It sounds like a VB method. It doesn't work in either A97 or AXP as far as I can tell. In both versions I get a runtime error = 2487 - The object type argument for the action or method is blank or invalid.
Private Sub bExportAsText_Click()
'Application.SaveAsText acTable, "Table1", "C:\Table1.txt"
DoCmd.TransferText acExportDelim, , "Table1", "C:\Table1.txt", True
Application.SaveAsText acQuery, "Query1", "C:\Query1.txt"
Application.SaveAsText acReport, "Report1", "C:\Report1.txt"
Application.SaveAsText acModule, "Module1", "C:\Module1.txt"
Application.SaveAsText acForm, "Form1", "C:\Form1.txt"
Application.SaveAsText acMacro, "Macro1", "C:\Macro1.txt"
Application.LoadFromText acForm, "Form1X", "C:\Form1.txt"
End Sub
strDatabase = Forms!frmPrintDoc!txtDBName
Set ThisDB = CurrentDb()
If strDatabase = "" Then
Set db = CurrentDb()
Else
Set db = DBEngine.Workspaces(0).OpenDatabase(strDatabase)
End If
Excellent link Happy! I added what I needed to export all of the tables as a text file for a total solution for those who want to export all database objects into a text file. This is great for documenting a database as well as another way to back up their database objects.Happy YN said:http://www.datastrat.com/Code/DocDatabase.txt
Have checked this and it works! exports all objects except tables
Option Compare Database
Option Explicit
Public Sub ExportDatabaseObjects()
On Error GoTo Err_ExportDatabaseObjects
Dim db As Database
'Dim db As DAO.Database
Dim td As TableDef
Dim d As Document
Dim c As Container
Dim i As Integer
Dim sExportLocation As String
Set db = CurrentDb()
sExportLocation = "C:\Temp\" 'Do not forget the closing back slash! ie: C:\Temp\
For Each td In db.TableDefs 'Tables
If Left(td.Name, 4) <> "MSys" Then
DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & "Table_" & td.Name & ".txt", True
End If
Next td
Set c = db.Containers("Forms")
For Each d In c.Documents
Application.SaveAsText acForm, d.Name, sExportLocation & "Form_" & d.Name & ".txt"
Next d
Set c = db.Containers("Reports")
For Each d In c.Documents
Application.SaveAsText acReport, d.Name, sExportLocation & "Report_" & d.Name & ".txt"
Next d
Set c = db.Containers("Scripts")
For Each d In c.Documents
Application.SaveAsText acMacro, d.Name, sExportLocation & "Macro_" & d.Name & ".txt"
Next d
Set c = db.Containers("Modules")
For Each d In c.Documents
Application.SaveAsText acModule, d.Name, sExportLocation & "Module_" & d.Name & ".txt"
Next d
For i = 0 To db.QueryDefs.Count - 1
Application.SaveAsText acQuery, db.QueryDefs(i).Name, sExportLocation & "Query_" & db.QueryDefs(i).Name & ".txt"
Next i
Set db = Nothing
Set c = Nothing
MsgBox "All database objects have been exported as a text file to " & sExportLocation, vbInformation
Exit_ExportDatabaseObjects:
Exit Sub
Err_ExportDatabaseObjects:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_ExportDatabaseObjects
End Sub
Application.LoadFromText acForm, "YourFormName", "C:\Temp\Form_frmTest.txt"