2017-02-21 102 views
2

我應該在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 

回答

2

有多種方式導入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 
+0

什麼是迭代的最好方式,使每年創建一個新的數據庫。我做了:'直到我> EndYear' –

+0

也在哪裏最好的地方把鏈接線? –

+0

需要在數據導入後,最後可能是最好的,'app.Quit' – Leviathan

相關問題