B
BobEQ
Guest
I'm using the Application.SaveAsText Method to export the objects in an Access database and it works great. I'm using the code below.
My problem arises when I try to make the application more flexible so I can pass in a different database name - using the sSourceDB variable. And basically if I don't set db=CurrentDB I get an error (2001 - You canceled the previous operation).
I can't find anything that says you have to use currentdb, but it seems you do.
If anyone knows of a solution to fix this problem please let me know.
thanks,
Bob
---------------------------------------------------------------------
Sample Below
---------------------------------------------------------------------
Public Sub ExportDatabaseObjects(sSourceDB As String, sExportLocation As String)
On Error GoTo Err_ExportDatabaseObjects
Dim db As Database
Dim td As TableDef
Dim d As Document
Dim c As Container
Dim i As Integer
'Set db = CurrentDb
Set db = DBEngine.OpenDatabase(sSourceDB)
Set c = db.Containers("Forms")
For Each d In c.Documents
'The following line generates 2001 - You canceled the previous operation when db is not equal to CurrentDB
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
My problem arises when I try to make the application more flexible so I can pass in a different database name - using the sSourceDB variable. And basically if I don't set db=CurrentDB I get an error (2001 - You canceled the previous operation).
I can't find anything that says you have to use currentdb, but it seems you do.
If anyone knows of a solution to fix this problem please let me know.
thanks,
Bob
---------------------------------------------------------------------
Sample Below
---------------------------------------------------------------------
Public Sub ExportDatabaseObjects(sSourceDB As String, sExportLocation As String)
On Error GoTo Err_ExportDatabaseObjects
Dim db As Database
Dim td As TableDef
Dim d As Document
Dim c As Container
Dim i As Integer
'Set db = CurrentDb
Set db = DBEngine.OpenDatabase(sSourceDB)
Set c = db.Containers("Forms")
For Each d In c.Documents
'The following line generates 2001 - You canceled the previous operation when db is not equal to CurrentDB
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