Skip to content

Commit 1155e00

Browse files
authored
Merge pull request #801 from MicrosoftDocs/lindalu-MSFT-add-export-all-data-topic
Lindalu msft add export all data topic
2 parents 8dfa0d4 + f090201 commit 1155e00

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed

docs/access/desktop-database-reference/exitforeachrecord-macro-action.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ ms.localizationpriority: medium
1111

1212
# ExitForEachRecord macro action
1313

14-
1514
**Applies to**: Access 2013, Office 2013
1615

1716
Use the **ExitForEachRecord** action to immediately exit a **[ForEachRecord](foreachrecord-data-block.md)** data block.
1817

19-
2018
> [!NOTE]
2119
> The **ExitForEachRecord** action is available only in Data Macros.
22-
23-
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Export all data from a Microsoft Access Database
3+
TOCTitle: Export all data
4+
ms:assetid: 79a1f793-7154-1c13-7dfe-a1b8cd64e1ea
5+
ms.date: 08/06/2024
6+
ms.localizationpriority: medium
7+
---
8+
9+
# Export all data from a Microsoft Access database
10+
11+
**Applies to**: Access 2024, Access 2021
12+
13+
This topic explains how an IT administrator can export all data and objects from a Microsoft Access database to text files, enabling use with other tools and supporting data portability.
14+
15+
For data stored in Tables, data can be exported to a variety of other formats using Access Wizards, such as Excel, see [Export data to Excel](https://support.microsoft.com/office/export-data-to-excel-64e974e6-ae43-4301-a53e-20463655b1a9), or text, see [Export data to a text file](https://support.microsoft.com/office/export-data-to-a-text-file-f72dfc38-a8a0-4c5b-8c2c-bf2950814140).
16+
17+
The following instructions demonstrate how to export objects using VBA code, using the [DoCmd.TransferText method](/office/vba/api/Access.DoCmd.TransferText) command to export table data, and the [Application.SaveAsText](application-save-as-text.md) command for other objects.
18+
19+
The code provided will create a function that can be called with the full path to a database and a destination folder. It will then create a text file for each object in the database, which will contain the data from tables, and the definitions of all user created objects such as forms or reports.
20+
21+
Since this process uses OLE automation, the code could also be written using PowerShell or using a .NET language such as C#. For this example, we use VBA.
22+
23+
1. Create a new database.
24+
1. Create a new module.
25+
1. Copy and paste the following export database code into the module.
26+
1. For each database that you wish to export, enter the following command in the immediate window:<br><br>`ExportAccessDatabase "_Full path to database_", "_Folder to contain exported objects_"`<br>
27+
1. Note that a new folder will be created in the target folder with the name of the exported database. This is to allow exporting multiple databases to the same target folder while keeping the contents of each exported database in a separate folder.
28+
1. If the database to be exported contains a startup form, or an AutoExec macro, startup actions may interfere with the export process, so you should reset the Startup Form property for the database, and remove or rename the AutoExec macro before attempting to call ExportAccessDatabase.
29+
1. If you wish to further automate this process, you could write a function that, given the path to a folder, enumerates all the databases in the folder, and calls the ExportAccessDatabase function to export each Microsoft Access database to a target folder.
30+
31+
## Export database code (VBA)
32+
33+
```vba
34+
Option Compare Database
35+
Option Explicit
36+
37+
' Pass in the location of the database to export, and the folder to export to
38+
Sub ExportAccessDatabase(strDBPath As String, exportFolder As String)
39+
Dim objAccess As Object
40+
Dim db As Database
41+
Dim td As TableDef
42+
Dim qd As QueryDef
43+
Dim doc As Document
44+
Dim cont As Container
45+
46+
On Error GoTo Err_ExportDatabase
47+
48+
' Create a new instance of Access
49+
Set objAccess = CreateObject("Access.Application")
50+
51+
' Open the database
52+
objAccess.OpenCurrentDatabase strDBPath
53+
54+
' Create a new folder using the name of the database to hold all the exported objects if it does not already exist
55+
exportFolder = exportFolder & Mid(strDBPath, InStrRev(strDBPath, "\"), InStr(strDBPath, ".") - InStrRev(strDBPath, "\")) & "\"
56+
57+
If Dir(exportFolder, vbDirectory) = "" Then
58+
MkDir exportFolder
59+
End If
60+
61+
Set db = objAccess.CurrentDb()
62+
63+
' Export all objects to export location with a name based on the type and name of the object
64+
65+
' Export Tables
66+
For Each td In db.TableDefs
67+
If Left(td.Name, 4) <> "MSys" Then ' Skip Microsoft Access system tables
68+
objAccess.DoCmd.TransferText acExportDelim, , td.Name, exportFolder & "Table_" & td.Name & ".txt", True
69+
End If
70+
Next td
71+
72+
' Export Forms
73+
For Each doc In db.Containers("Forms").Documents
74+
objAccess.SaveAsText acForm, doc.Name, exportFolder & "Form_" & doc.Name & ".txt"
75+
Next doc
76+
77+
' Export Reports
78+
For Each doc In db.Containers("Reports").Documents
79+
objAccess.SaveAsText acReport, doc.Name, exportFolder & "Report_" & doc.Name & ".txt"
80+
Next doc
81+
82+
' Export Macros
83+
For Each doc In db.Containers("Scripts").Documents
84+
objAccess.SaveAsText acMacro, doc.Name, exportFolder & "Macro_" & doc.Name & ".txt"
85+
Next doc
86+
87+
' Export Modules
88+
For Each doc In db.Containers("Modules").Documents
89+
objAccess.SaveAsText acModule, doc.Name, exportFolder & "Module_" & doc.Name & ".txt"
90+
Next doc
91+
92+
' Export Queries
93+
For Each qd In db.QueryDefs
94+
' Skip Microsoft Access temporary queries
95+
If Left(qd.Name, 3) <> "~sq" Then
96+
objAccess.SaveAsText acQuery, qd.Name, exportFolder & "Query_" & qd.Name & ".txt"
97+
End If
98+
Next
99+
100+
objAccess.Quit
101+
102+
Set db = Nothing
103+
Set cont = Nothing
104+
Set objAccess = Nothing
105+
106+
MsgBox "Export complete to " & exportFolder, vbInformation
107+
108+
GoTo Exit_Sub
109+
110+
Err_ExportDatabase:
111+
MsgBox Err.Number & ": " & Err.Description
112+
113+
Exit_Sub:
114+
End Sub
115+
```

0 commit comments

Comments
 (0)