gpt4 book ai didi

ms-access - 使用波浪号分隔符将 MSAccess 表导出为 Unicode

转载 作者:行者123 更新时间:2023-12-04 07:12:05 25 4
gpt4 key购买 nike

我想从 MSAccess2003 中导出几个表的内容。
这些表包含 unicode 日文字符。
我想将它们存储为波浪号分隔的文本文件。

我可以使用文件/导出手动执行此操作,并在“高级”对话框中选择波浪号作为字段分隔符和 Unicode 作为代码页。

我可以将其存储为导出规范,但这似乎是特定于表的。

我想使用 VBA 代码导出许多表。

到目前为止,我已经尝试过:

子导出表()

Dim lTbl As Long
Dim dBase As Database
Dim TableName As String

Set dBase = CurrentDb

For lTbl = 0 To dBase.TableDefs.Count
'If the table name is a temporary or system table then ignore it
If Left(dBase.TableDefs(lTbl).Name, 1) = "~" Or _
Left(dBase.TableDefs(lTbl).Name, 4) = "MSYS" Then
'~ indicates a temporary table
'MSYS indicates a system level table
Else
TableName = dBase.TableDefs(lTbl).Name
DoCmd.TransferText acExportDelim, "UnicodeTilde", TableName, "c:\" + TableName + ".txt", True
End If
Next lTbl
Set dBase = Nothing

结束子

当我运行它时,我得到一个异常:

运行时错误“3011”:
Microsoft Jet 数据库引擎找不到对象“Allowance1#txt”。请确保该对象存在并且您正确拼写了它的名称和路径名称。

如果我此时调试,TableName 是“Allowance1”,正如预期的那样。

我猜我的 UnicodeTilde 导出规范是特定于表的,所以我不能将它用于多个表。

解决办法是什么?我应该使用 TransferText 以外的其他东西,还是以编程方式创建导出规范?

任何帮助表示赞赏。

最佳答案

我最终解决了这个问题。 (我现在使用的是 Access 2007,但遇到了与 Access 2003 相同的问题。)

首先,什么不起作用:

即使使用格式正确的 schema.ini,TransferText 也只会使标题行 unicode 和波浪号分隔。 (不,我没有把它全部放在一行上,这只是 stackoverflow 上 html 的格式问题。)

[MyTable.txt]
CharacterSet = Unicode
Format = Delimited(~)
ColNameHeader = True
NumberDigits = 10
Col1= "Col1" Char Width 10
Col2= "Col2" Integer
Col3= "Col3" Char Width 2

只需使用 select 语句:
SELECT * INTO [Text;DATABASE=c:\export\;FMT=Delimited(~)].[MyTable.txt] FROM [MyTable]

完全忽略了 FMT。我发现很难找到有关参数格式的文档。无论我在 FMT 参数中输入什么,我唯一能开始工作的就是 Fixed。其他所有内容都被视为 CSVDelimited。我可以检查这一点,因为 select 语句创建了一个 schema.ini 文件,如下所示:
[MyTable.txt]
ColNameHeader=True
CharacterSet=1252
Format=CSVDelimited
Col1=Col1 Char Width 10
Col2=Col2 Integer
Col3=Col3 Char Width 2

我最终的解决方案是创建自己的 schema.ini,然后使用 select 语句。我的模块代码如下所示:
Option Compare Database
Option Explicit

Public Function CreateSchemaFile(bIncFldNames As Boolean, _
sPath As String, _
sSectionName As String, _
sTblQryName As String) As Boolean


Dim Msg As String
On Local Error GoTo CreateSchemaFile_Err
Dim ws As Workspace, db As Database
Dim tblDef As TableDef, fldDef As Field
Dim i As Integer, Handle As Integer
Dim fldName As String, fldDataInfo As String
' -----------------------------------------------
' Set DAO objects.
' -----------------------------------------------
Set db = CurrentDb()
' -----------------------------------------------
' Open schema file for append.
' -----------------------------------------------
Handle = FreeFile
Open sPath & "schema.ini" For Output Access Write As #Handle
' -----------------------------------------------
' Write schema header.
' -----------------------------------------------
Print #Handle, "[" & sSectionName & "]"
Print #Handle, "CharacterSet = Unicode"
Print #Handle, "Format = Delimited(~)"
Print #Handle, "ColNameHeader = " & _
IIf(bIncFldNames, "True", "False")
Print #Handle, "NumberDigits = 10"
' -----------------------------------------------
' Get data concerning schema file.
' -----------------------------------------------
Set tblDef = db.TableDefs(sTblQryName)
With tblDef
For i = 0 To .Fields.Count - 1
Set fldDef = .Fields(i)
With fldDef
fldName = .Name
Select Case .Type
Case dbBoolean
fldDataInfo = "Bit"
Case dbByte
fldDataInfo = "Byte"
Case dbInteger
fldDataInfo = "Short"
Case dbLong
fldDataInfo = "Integer"
Case dbCurrency
fldDataInfo = "Currency"
Case dbSingle
fldDataInfo = "Single"
Case dbDouble
fldDataInfo = "Double"
Case dbDate
fldDataInfo = "Date"
Case dbText
fldDataInfo = "Char Width " & Format$(.Size)
Case dbLongBinary
fldDataInfo = "OLE"
Case dbMemo
fldDataInfo = "LongChar"
Case dbGUID
fldDataInfo = "Char Width 16"
End Select
Print #Handle, "Col" & Format$(i + 1) _
& "= """ & fldName & """" & Space$(1); "" _
& fldDataInfo
End With
Next i
End With
CreateSchemaFile = True
CreateSchemaFile_End:
Close Handle
Exit Function
CreateSchemaFile_Err:
Msg = "Error #: " & Format$(Err.Number) & vbCrLf
Msg = Msg & Err.Description
MsgBox Msg
Resume CreateSchemaFile_End
End Function

Public Function ExportATable(TableName As String)
Dim ThePath As String
Dim FileName As String
Dim TheQuery As String
Dim Exporter As QueryDef
ThePath = "c:\export\"
FileName = TableName + ".txt"
CreateSchemaFile True, ThePath, FileName, TableName
On Error GoTo IgnoreDeleteFileErrors
FileSystem.Kill ThePath + FileName
IgnoreDeleteFileErrors:
TheQuery = "SELECT * INTO [Text;DATABASE=" + ThePath + "].[" + FileName + "] FROM [" + TableName + "]"
Set Exporter = CurrentDb.CreateQueryDef("", TheQuery)
Exporter.Execute
End Function


Sub ExportTables()

Dim lTbl As Long
Dim dBase As Database
Dim TableName As String

Set dBase = CurrentDb

For lTbl = 0 To dBase.TableDefs.Count - 1
'If the table name is a temporary or system table then ignore it
If Left(dBase.TableDefs(lTbl).Name, 1) = "~" Or _
Left(dBase.TableDefs(lTbl).Name, 4) = "MSYS" Then
'~ indicates a temporary table
'MSYS indicates a system level table
Else
TableName = dBase.TableDefs(lTbl).Name
ExportATable (TableName)
End If
Next lTbl
Set dBase = Nothing
End Sub

我没有声称这很优雅,但它确实有效。另请注意,stackoverflow 代码格式化程序不喜欢我的\",因此它不能很好地打印我的代码。

关于ms-access - 使用波浪号分隔符将 MSAccess 表导出为 Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/160532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com