Here's a quick & dirty routine I just threw together to export all tables in the database to an Excel workbook:
'------ start of code ------
Sub ExportAllTables()
On Error GoTo Err_General
Dim ao As AccessObject
Dim strWorkbookName As String
Dim intOutputType As AcSpreadSheetType
Dim lngTables As Long
Dim lngTablesExported As Long
With Application.FileDialog(msoFileDialogSaveAs)
.Title = "Select or Enter Excel File for Export"
If .Show = True Then
strWorkbookName = .SelectedItems(1)
End If
End With
If Len(strWorkbookName) = 0 Then
Exit Sub
End If
Select Case Mid$(strWorkbookName, InStrRev(strWorkbookName, ".") + 1)
Case "xlsx": intOutputType = acSpreadsheetTypeExcel12Xml
Case "xls": intOutputType = acSpreadsheetTypeExcel9
Case Else
MsgBox "Sorry, I can only export to .xlsx or .xls files.", vbExclamation, "Unsupported File Type"
Exit Sub
End Select
If Len(Dir(strWorkbookName)) > 0 Then
Kill strWorkbookName
End If
For Each ao In CurrentData.AllTables
If ao.Name Like "MSys*" Or ao.Name Like "~*" Then
' Skip system table or deleted table.
Else
lngTables = lngTables + 1
On Error GoTo Err_TableExport
DoCmd.TransferSpreadsheet acExport, intOutputType, ao.Name, strWorkbookName, True
On Error GoTo Err_General
lngTablesExported = lngTablesExported + 1
DoEvents
End If
NEXT_TABLE:
Next ao
Exit_Point:
MsgBox "Exported " & lngTablesExported & " out of " & lngTables & " tables.", _
vbInformation, _
"Export Complete"
Exit Sub
Err_TableExport:
If MsgBox( _
"Unable to export table '" & ao.Name & "' - the following error occurred:" & vbCr & _
vbCr & " " & Err.Number & " : " & Err.Description & vbCr & _
vbCr & "Skipping this table. Click Cancel to stop the export completely.", _
vbExclamation + vbOKCancel + vbDefaultButton1, _
"Error " & Err.Number) _
= vbCancel _
Then
Resume Exit_Point
Else
Err.Clear
Resume NEXT_TABLE
End If
Err_General:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End Sub
'------ end of code ------