使用Access作为ODBC数据库

时间:2016-02-22 14:31:43

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

这就是我要做的事情: 我需要在工作中整合来自许多不同组的每周预测excel模板。我已经为他们创建了一个表格,用于填写所有预测项目/差异以进行计划/解释。 此时,我在excel模板上有一个按钮,它将所有数据逐行放在表单旁边的常规统一表中。 我想要发生的事情是,在统一的excel表中所有内容之后,他们点击另一个按钮并使用ODBC连接到我的访问数据库,使它将数据附加到访问中的表中吗?我之前听说过这件事,但我不知道怎么做。

我首先尝试进入"来自其他来源"在“数据”选项卡下单击“"来自Microsoft Query"然后完成选择我的访问数据库作为数据源的步骤,然后完成这些步骤,直到我找到一个按钮,然后单击“查询数据”或“在Microsoft Query中编辑查询”。但后来我迷失了如何用它来使用ODBC连接将excel表中的数据附加到访问数据库。

有人可以帮我弄清楚怎么做吗?将从excel模板附加到此访问数据库的多个组。如果我能做到这一点,访问将是一种简单的方法来跟踪所有数据。

谢谢!

2 个答案:

答案 0 :(得分:0)

使用ADO连接(请参阅connectionstrings.com)。步骤是:

  1. 在项目中将引用设置为Microsoft ActiveX Data Objects 6.1库(或您拥有的任何版本)。

  2. Dim cn As ADODB.Connection

  3. 打开连接(Google搜索会告诉您如何)

  4. 将Excel工作表中的值加载到数组中

  5. 循环遍历数组并将每条记录插入表中。例如:

  6. cn.Execute" INSERT INTO SomeTable VALUES("& array(i,1)&","& array(i,2)& ..

    以上只是一个指南,在语法上可能不完全正确。

    编辑:

    Dim conString as String

    conString =" Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ myFolder \ myAccessFile.accdb; 坚持安全信息=错误;"

    设置cn =新ADODB.Connection cn.Open conString

答案 1 :(得分:0)

这是我在上面的其他人的帮助下我上面遇到的问题的答案:

    Sub ADOFromExcelToAccess()
   ' exports data from the active worksheet to a table in an Access database
 ' this procedure must be edited before use
  Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source=Path to the database;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Forecast_Items", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 3 ' the start row in the worksheet
Do While Len(Range("Q" & r).Formula) > 0
' repeat until first empty cell in column A
    With rs
        .AddNew ' create a new record
        ' add values to each field in the record
        .Fields("UserName") = Range("O" & r).Value
        .Fields("Forecast_Date") = Range("P" & r).Value
        .Fields("Area") = Range("Q" & r).Value
        .Fields("Description_Item") = Range("R" & r).Value
        .Fields("Account") = Range("S" & r).Value
        .Fields("RRDD") = Range("T" & r).Value
        .Fields("CostCenter") = Range("U" & r).Value
        .Fields("Fleet") = Range("V" & r).Value
        .Fields("ForecastAmount") = Range("W" & r).Value
        .Fields("PlanAmount") = Range("X" & r).Value
        .Fields("VarianceForecast") = Range("Y" & r).Value
        .Fields("Explanation") = Range("Z" & r).Value

        ' add more fields if necessary...
        .Update ' stores the new record
    End With
    r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing




    End Sub

我有的其他代码,如果有错误,应该批量全部或无。但是,当发生错误时,仍然会写出成功完成的错误。

     Sub ADOFromExcelToAccess()

     If MsgBox("This Button Will Submit all Data in the Table to the Right & Clear the Table! Are you sure?", vbYesNo) = vbNo Then Exit Sub


     ' exports data from the active worksheet to a table in an Access database
    ' this procedure must be edited before use
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
    "Data Source=Filepath.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open "Forecast_Items", cn, adOpenKeyset, adLockBatchOptimistic, adCmdTable
' all records in a table
r = 3 ' the start row in the worksheet
Do While Len(Range("Q" & r).Formula) > 0
' repeat until first empty cell in column A
    With rs
        .AddNew ' create a new record
        ' add values to each field in the record
        .Fields("UserName") = Range("O" & r).Value
        .Fields("Forecast_Date") = Range("P" & r).Value
        .Fields("Area") = Range("Q" & r).Value
        .Fields("Description_Item") = Range("R" & r).Value
        .Fields("Account") = Range("S" & r).Value
        .Fields("RRDD") = Range("T" & r).Value
        .Fields("CostCenter") = Range("U" & r).Value
        .Fields("Fleet") = Range("V" & r).Value
        .Fields("ForecastAmount") = Range("W" & r).Value
        .Fields("PlanAmount") = Range("X" & r).Value
        .Fields("VarianceForecast") = Range("Y" & r).Value
        .Fields("Explanation") = Range("Z" & r).Value



        ' add more fields if necessary...

    End With
    r = r + 1 ' next row
Loop
rs.UpdateBatch 'injects full table from excel into access at the same time, eliminating possible errors with inserting certain rows over others
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

MsgBox ("Data was Submitted Successfully!")

Exit Sub




  End Sub