将CSV文件附加到现有的Access表

时间:2016-06-03 17:01:32

标签: vba csv ms-access access-vba

我有一个Access数据库,里面有大量的表,表单和查询。使用我的一个表单,我有一个上传按钮,我想用它来将csv文件附加到数据库中的特定表。

我有一个按钮的OnClick功能,我正挂断..

每次到达

adoCSVConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strPathToTextfile & ";" & _
    "Extended Properties=""text;HDR=YES;FMT=Delimited"""

它说:提供商无法找到或未正确安装..

这是否与数据源管理设置中的设置有关?我错过了参考文献吗?

如果您有兴趣了解我所拥有的一切

,以下是所有代码
Private Sub uploadBTN_Click()

'Dim adoCSVConnection, adoCSVRecordset, strPathToTextfile
'Dim strCSVFile, adoJetConnection, adoJetCommand, strDBPath

Dim adoCSVConnection As ADODB.Connection
Dim adoCSVRecordset As ADODB.Recordset
Dim adoJetConnection As ADODB.Connection
Dim adoJetCommand As ADODB.Command

Set adoCSVConnection = New ADODB.Connection


Const adCmdText = &H1

' Specify path to CSV file. ex: c:\Scripts\
strPathToTextfile = "C:\Desktop\"

' Specify CSV file name. ex: Users.csv
strCSVFile = "testfile2.csv"

' Specify Access database file. ex: c:\Scripts\MyData.mdb
strDBPath = "\\folder\NewMasterclient.mdb"

' Open connection to the CSV file.
Set adoCSVConnection = CreateObject("ADODB.Connection")
Set adoCSVRecordset = CreateObject("ADODB.Recordset")

' Open CSV file with header line.
adoCSVConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strPathToTextfile & ";" & _
    "Extended Properties=""text;HDR=YES;FMT=Delimited"""


adoCSVRecordset.Open "SELECT * FROM " & strCSVFile, adoCSVConnection

' Open connection to MS Access database.
Set adoJetConnection = CreateObject("ADODB.Connection")
adoJetConnection.ConnectionString = "DRIVER=Microsoft Access Driver (*.mdb);" _
    & "FIL=MS Access;DriverId=25;DBQ=" & strDBPath & ";"
adoJetConnection.Open

' ADO command object to insert rows into Access database.
Set adoJetCommand = New ADODB.Command
Set adoJetCommand.ActiveConnection = adoJetConnection
adoJetCommand.CommandType = adCmdText

' Read the CSV file.
Do Until adoCSVRecordset.EOF
    ' Insert a row into the Access database.
    adoJetCommand.CommandText = "INSERT INTO testfile2" & "(a, b, c, d, clientid, reg) " & "VALUES (" _
            & "'" & adoCSVRecordset.Fields("a").Value & "', " _
            & "'" & adoCSVRecordset.Fields("b").Value & "', " _
            & "'" & adoCSVRecordset.Fields("c").Value & "', " _
            & "'" & adoCSVRecordset.Fields("d").Value & "', " _
            & "'" & adoCSVRecordset.Fields("clientid").Value & "')" _
            & "'" & adoCSVRecordset.Fields("reg").Value & "')"

    adoJetCommand.Execute
    adoCSVRecordset.MoveNext
Loop

' Clean up.
adoCSVRecordset.Close
adoCSVConnection.Close
adoJetConnection.Close

End Sub

0 个答案:

没有答案