【问题标题】:Export all tables to txt files with export specification使用导出规范将所有表导出为 txt 文件
【发布时间】:2016-11-04 11:38:42
【问题描述】:

我有一个 Access DB,其中包含几个不同的表,每个表都有不同的结构(字段数和名称、行数、标题)。

我想做的是将所有这些表导出到 txt 文件中,并带有给定的分隔符(“|”),点作为小数点分隔符,字符串用引号。

我浏览了互联网,得到的是:

  • 使用DoCmd.TransferText acExportDelim 命令
  • 保存自定义导出规范并应用它

我收到一条错误消息(“对象不存在”),我认为这与导出规范是“特定于工作表”的事实有关,即不适用于具有不同字段和字段名的表。

你能帮帮我吗? 谢谢!!

编辑。 我还发布了我运行的原始代码。正如我之前所说,我是 VBA 新手,所以我只是在网络上查找代码,根据我的需要对其进行调整,然后运行。

Public Sub ExportDatabaseObjects()
On Error GoTo Err_ExportDatabaseObjects

Dim db As Database
Dim db As DAO.Database
Dim td As TableDef
Dim sExportLocation As String
Dim a As Long

Set db = CurrentDb()

sExportLocation = "C:\" 'Do not forget the closing back slash! ie: C:\Temp\

For a = 0 To db.TableDefs.Count - 1
    If Not (db.TableDefs(a).Name Like "MSys*") Then
        DoCmd.TransferText acExportDelim, "Export_specs",  db.TableDefs(a).Name, sExportLocation & db.TableDefs(a).Name & ".txt", True
    End If
Next a

Set db = 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

在运行代码之前,我手动导出了第一个将 Export_specs 保存到文件的表。

考虑一个包含两个表 A 和 B 的数据库。 当我运行代码 A 正确导出时,我收到以下错误消息“3011 - Microsoft Access 数据库引擎找不到对象'B#txt'。确保对象存在并且拼写它的名称和路径名称正确。如果 'B#txt' 不是本地对象,请检查您的网络连接或联系服务器管理"。

【问题讨论】:

  • 什么对象不存在?
  • 请发布完整代码。 TransferText 行没有引用导出规范。

标签: ms-access vba


【解决方案1】:

所以,这有点复杂。我创建了一个使用 ImportExport Specs 导入文件的例程,您应该能够轻松地适应您的目的。基本操作是创建一个规范,该规范完全符合您对一个文件的要求。然后,使用以下代码导出此规范:

Public Function SaveSpecAsXMltoTempDirectory(sSpecName As String)

Dim oFSO As FileSystemObject
Dim oTS As TextStream

Set oFSO = New FileSystemObject
Set oTS = oFSO.CreateTextFile("C:\Temp\" & sSpecName & ".xml", True)
oTS.Write CurrentProject.ImportExportSpecifications(sSpecName).XML

oTS.Close
Set oTS = Nothing
Set oFSO = Nothing

End Function

然后在记事本中打开这个文件并用一些占位符替换文件名(我在这个示例中使用了“FILE_PATH_AND_NAME”)。然后,使用以下代码重新导入数据库:

Public Function SaveSpecFromXMLinTempDirectory(sSpecName As String)

Dim oFSO As FileSystemObject
Dim oTS As TextStream
Dim sSpecXML As String
Dim oSpec As ImportExportSpecification

Set oFSO = New FileSystemObject
Set oTS = oFSO.OpenTextFile("C:\Temp\" & sSpecName & ".xml", ForReading)
sSpecXML = oTS.ReadAll
For Each oSpec In CurrentProject.ImportExportSpecifications
    If oSpec.Name = sSpecName Then oSpec.Delete
Next oSpec
Set oSpec = CurrentProject.ImportExportSpecifications.Add(sSpecName, sSpecXML)

Set oSpec = Nothing
oTS.Close
Set oTS = Nothing
Set oFSO = Nothing

End Function

现在您可以循环浏览文件并将规范中的占位符替换为文件名,然后使用以下代码执行它:

Public Function ImportFileUsingSpecification(sSpecName As String, sFile As String) As Boolean

Dim oSpec As ImportExportSpecification
Dim sSpecXML As String
Dim bReturn As Boolean

'initialize return variable as bad until function completes
bReturn = False
'export data using saved Spec
'   first make sure no temp spec left by accident
For Each oSpec In CurrentProject.ImportExportSpecifications
    If oSpec.Name = "Temp" Then oSpec.Delete
Next oSpec
sSpecXML = CurrentProject.ImportExportSpecifications(sSpecName).XML
If Not Len(sSpecXML) = 0 Then
    sSpecXML = Replace(sSpecXML, "FILE_PATH_AND_NAME", sFile)
    'now create temp spec to use, get template text and replace file path and name
    Set oSpec = CurrentProject.ImportExportSpecifications.Add("Temp", sSpecXML)
    oSpec.Execute
    bReturn = True
Else
    MsgBox "Could not locate correct specification to import that file!", vbCritical, "NOTIFY ADMIN"
    GoTo ExitImport
End If

ExitImport:
    On Error Resume Next
    ImportFileUsingSpecification = bReturn
    Set oSpec = Nothing
    Exit Function
End Function

显然,您需要在规范 XML 中找到表名,并在其上使用占位符。如果你不能让它工作,请告诉我,我会更新导出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多