连接Excel和访问之间的字符串

时间:2016-01-29 07:30:28

标签: excel vba excel-vba ms-access access-vba

此子程序在Access中运行以从Excel文件导入数据," FileName"

此连接字符串/打开过程几乎是从MSDN引用中复制的,但是存在错误。

当我单步执行时,我会收到"无法识别的数据库格式"在oConn.Open一行。

Filename是电子表格,而不是数据库。我如何表明这一点?

Public Sub Import2(FileName As Variant)
    Dim wb As Object, ws As Object
    Dim xl As Object
    Set xl = CreateObject("excel.Application")
    Dim qs As String
    Dim ValueString As String
    Dim sConn As String

    Dim oConn As Object
    Set oConn = CreateObject("adodb.connection")

    oConn.ConnectionString = "Provider=microsoft.ACE.OLEDB.12.0; Data Source=filename; Extended Properties=Excel 12.0 XML; hdr=yes" & ";"
    oConn.Open FileName

    Set wb = xl.Workbooks.Open(FileName)
    Set ws = wb.worksheets("For Export")
    data1 = ws.cells(2, 1)
    Data2 = ws.cells(2, 2)
    Data3 = ws.cells(2, 3)

    'Following lines may or may not be correct.  Working on the connection string
    'ValueString = Format(data1, "yyyy\/mm\/dd")
    'qs = "INSERT INTO MAF (FormDate) VALUES (#" & ValueString & "#)"

    DoCmd.RunSQL (qs)
    'CurrentDb.Execute qs

End Sub

1 个答案:

答案 0 :(得分:1)

使用文件路径<{1}}更改Data Source=filename