将csv文件导入到每个新数据库中
问题描述:
我应该在Access中创建一个数据库,稍后将转移到Oracle。对于每年我应该导入一个包含数百万行数据的csv文件,可以追溯到1985年。我相信Access不能保存所有这些数据,所以我想我会有一个主数据库引用其他数据库。将csv文件导入到每个新数据库中
我需要什么代码来创建每年的数据库?
这是我到目前为止有:
DocLocate = InputBox("Input Document Location") & "\"
StartYear = InputBox("Input Start Year")
EndYear = InputBox("Input End Year")
i = StartYear
strPath = DocLocate
strTable = strFile
strFile = "mar31_wts_" & i & ".csv"
Do Until i > EndYear
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, acSpreadsheetTypeText, strFile, strPathFile, blnHasFieldNames
i = i + 1
strFile = "mar31_wts_" & i & ".csv"
Loop
答
有多种方式导入CSV数据导入Access数据库。使用DoCmd.TransferText
方法,您将导入到执行代码的应用程序的当前数据库中。
如果要导入到另一个数据库比执行您的VBA代码的人,你必须在那里创建另一个Application
对象,然后打开目标数据库:
Dim app As Application
Dim strDBPath As String
' create the database
strDBPath = "<path_to_db>\db.accdb"
DAO.CreateDatabase(strDBPath, dbLangGeneral).Close
' open the database in a new Access Application
Set app = New Application
app.OpenCurrentDatabase strDBPath
' import the csv file into that database
app.DoCmd.TransferText acImportDelim, acSpreadsheetTypeText, strFile, strPathFile, blnHasFieldNames
' close the database and the Access application
app.Quit acQuitSaveNone
如果你想使用一个导入规范,它必须存在于新创建的数据库中。在这种情况下,最好手动创建一个已经包含您需要的空模板数据库(例如规范)并在上面的代码中创建一个新模板数据库,而不是使用DAO.CreateDatabase
创建一个新模板数据库副本。
如果你想在你的主数据库连接的各种新的数据库中的所有这些导入的表,你可以调用这个:
DoCmd.TransferDatabase acLink, , strDBPath, acTable, strFile, strFile
什么是迭代的最好方式,使每年创建一个新的数据库。我做了:'直到我> EndYear' –
也在哪里最好的地方把链接线? –
需要在数据导入后,最后可能是最好的,'app.Quit' – Leviathan