2017-02-21 5 views
2

나중에 Oracle에 전송 될 Access 데이터베이스를 작성해야합니다. 매년 나는 수백만 행의 데이터가 들어있는 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

Access 데이터베이스에 CSV 데이터를 가져올 수있는 다양한 방법이 있습니다. 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

때문에 매년 새 데이터베이스를 만듭니다. 나는했다 : 'Do Until i> EndYear' –

+0

또한 링크 라인을 넣을 수있는 가장 좋은 장소는 어디입니까? –

+0

데이터를 가져온 후에 필요합니다. 'app.Quit' 다음에 가장 좋을 것입니다. – Leviathan

관련 문제