使用CurrentDB中的导入规范将DoCmd.TransferText导入另一个数据库 [英] DoCmd.TransferText into another DB, using an Import Spec in CurrentDB
问题描述
挑战在标题中.
如何使用CurrentDB中的导入规范,使用< DB> .DoCmd.TransferText ...
将文件导入到单独的DB中?
How can I use <DB>.DoCmd.TransferText...
to import a file into a separate DB, using an Import Spec in CurrentDB?
如果您的建议是创建到目标数据库的链接表,或者在目标数据库中创建导入规范,请不要回复.
到目前为止,这是我的代码,但是显然它不起作用,因为文件规范位于CurrentDB中,而不是在目标DB中.
Here's my code so far, but obviously it does not work because the file spec is in CurrentDB instead of the destination DB.
Option Compare Database
Function job1()
Dim sFinalDB As String, sTableName As String, sSpecName As String, sFileName As String
sFinalDB = "path\db.mdb"
sTableName = "tblName"
sSpecName = "specName"
sFileName = "path\test.csv"
fn_ImportTxt sFinalDB, sSpecName, sTableName, sFileName, True
End Function
Function fn_ImportTxt(sDBPath As String, _
sSpecName As String, _
sTableName As String, _
sFileName As String, _
bHeaders As Boolean)
Dim acApp As Access.Application
Set acApp = New Access.Application
acApp.OpenCurrentDatabase sDBPath
acApp.DoCmd.TransferText acImportDelim, sSpecName, sTableName, sFileName, bHeaders
acApp.CloseCurrentDatabase
acApp.Quit acQuitSaveNone
Set acApp = Nothing
End Function
推荐答案
我今天偶然发现了这个问题,试图自己做同样的事情.我发现规范存储在两个系统对象表"MSysIMEXColumns"和"MSysIMEXSpecs"中.我所做的是每次运行导入过程之前都将两个系统对象表复制到目标数据库,这就是为什么在目标数据库中也可以使用这些规范的原因.
I stumbled upon this question today trying to do the same thing myself. I found that the specs are stored in two system object tables "MSysIMEXColumns" and "MSysIMEXSpecs". What I did was copying the two system object table to the destination database every time before I run the import procedure, that why the specs are also available in the destination database.
DoCmd.TransferDatabase acExport, "Microsoft Access", DBPath, acTable, "MSysIMEXColumns", "MSysIMEXColumns"
DoCmd.TransferDatabase acExport, "Microsoft Access", DBPath, acTable, "MSysIMEXSpecs", "MSysIMEXSpecs"
Dim NewAccessApp As Access.Application
Set NewAccessApp = New Access.Application
With NewAccessApp
.OpenCurrentDatabase strDBPath, True
.DoCmd.TransferText acImportDelim, ImportSpec, TabelName, FilePath, True
.CloseCurrentDatabase
.Quit
End With
显然,只有在您不介意覆盖目标数据库中的规格表时,此方法才有效.
Obviously this would only work if you don't mind overwriting the spec table in the destination database.
这篇关于使用CurrentDB中的导入规范将DoCmd.TransferText导入另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!