2014-04-08 70 views
0

對於VB編程新手,我有一個要求,那裏將存在已經在Access中創建的表,並且我需要編寫代碼讀取文件並在記錄中輸入數據。請幫我解決這個問題。我用Access作爲我的DB如何讀取CSV文件,並將數據插入到Access數據庫中,使用VB

輸入文件是.csv文件,它的格式:

FIRST_NAME, LAST_NAME, HOME_ADD, COMPANY, DESIG 
Mark , Taylor , UK , XYZ , ABC 
Mark , Taylor , UK , XYZ , ABC 
Mark , Taylor , UK , XYZ , ABC 
Mark , Taylor , UK , XYZ , ABC 
Mark , Taylor , UK , XYZ , ABC 
Mark , Taylor , UK , XYZ , ABC 
Mark , Taylor , UK , XYZ , ABC 

此值sholud被讀取「C:\輸入\ data.csv」文件夾的讀寫在已經創建的Access表中。

在此先感謝

+0

你已經嘗試過什麼嗎? – andrei

+0

是schopy,但我做了什麼,它讀取一個文件,並創建一個新表顯示數據那裏 – desiretolearn

+0

我不明白你的意思。但是,如果你已經編寫了不起作用的代碼,爲什麼不發佈呢? –

回答

0

我不確定您是否會提前知道這些字段。我會假設你不知道,直到文件被打開。另外,我假設你正在使用ADO。

Private Sub InsertCsvDataIntoTable(ByRef in_sCsvFileName As String, _ 
            ByRef in_oConn As ADODB.Connection, _ 
            ByRef in_sTableName As String) 

    Dim iFileNo    As Integer 
    Dim sLine    As String 
    Dim vasFields   As Variant 
    Dim lIndex    As Long 
    Dim sSQL_InsertPrefix As String 
    Dim sSQL    As String 

    iFileNo = FreeFile 

    Open in_sCsvFileName For Input As #iFileNo 

    If EOF(iFileNo) Then 
     Exit Sub 
    End If 

    ' Read field names from the top line. 
    Line Input #iFileNo, sLine 

    ' Note that in a "proper" CSV file, there should not be any trailing spaces, and all strings should be surrounded by double quotes. 
    ' However, the top row provided can simply be used "as is" in the SQL string. 

    sSQL_InsertPrefix = "INSERT INTO " & in_sTableName & "(" & sLine & ") VALUES(" 

    Do Until EOF(iFileNo) 

     Line Input #iFileNo, sLine 

     ' Initialise SQL string. 
     sSQL = sSQL_InsertPrefix 

     ' Again, in a proper CSV file, the spaces around the commas shouldn't be there, and the fields should be double quoted. 
     ' Since the data is supposed to look like this, then the assumption is that the delimiter is space-comma-space. 
     vasFields = Split(sLine, " , ") 

     ' Build up each value, separated by comma. 
     ' It is assumed that all values here are string, so they will be double quoted. 
     ' However, if there are non-string values, you will have to ensure they don't get quoted. 
     For lIndex = 0 To UBound(vasFields) - 1 
      sSQL = sSQL & "'" 
      sSQL = sSQL & vasFields(lIndex) 
      sSQL = sSQL & "'" 
      sSQL = sSQL & "," 
     Next lIndex 

     ' This chunk of code is for the last item, which does not have a following comma. 
     sSQL = sSQL & "'" 
     sSQL = sSQL & vasFields(lIndex) 
     sSQL = sSQL & "'" 

     sSQL = sSQL & ")" 

     ' Run the SQL command. 
     in_oConn.Execute sSQL 

    Loop 

    Close #iFileNo 

End Sub 
+0

謝謝您的建議它的工作 – desiretolearn

+0

但這是正確的答案嗎?您的CSV文件看起來有點奇怪(請參閱我的客戶意見) - 這是由您自己創建的示例數據,還是真實的數據? –

0

訪問已經支持從CSV文件導入記錄,所以這可以通過啓動和控制一個Access實例很容易地完成。

With CreateObject("Access.Application") 
    .OpenCurrentDatabase "C:\Database1.accdb" 
    .DoCmd.TransferText , , "Table1", "c:\input\data.csv", True 
    .Quit 
End With 

TransferTextTrue參數指定CSV文件中包含標題行。

相關問題