使用 Access 作为 ODBC 数据库

Using Access as an ODBC Database

这就是我想要做的: 我需要整合工作中许多不同小组的每周预测 excel 模板。我已经为他们创建了一个表格来填写他们所有的预测项目/差异以计划/解释。 那时,我在 excel 模板上有一个按钮,可以将所有数据逐行放置到表单旁边的常规统一 table 中。 我想要发生的是,在一切都统一 excel table 之后,他们单击另一个按钮并使用 ODBC 连接到我的访问数据库,使其将数据附加到 table在访问我?我以前听说过这样做,但我不确定如何做。

我首先尝试在 excel 中转到“数据”选项卡下的 "From Other Sources" 并单击 "From Microsoft Query",然后完成选择我的访问数据库作为数据源的步骤,以及然后执行这些步骤,直到我点击一个按钮,上面写着 "View data or edit query in Microsoft Query" 但后来我迷路了如何使用它来使用 excel sheet 将数据附加到访问数据库ODBC 连接。

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

谢谢!

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

  1. 在您的项目中设置对 Microsoft ActiveX 数据对象 6.1 库(或您拥有的任何版本)的引用。

  2. 昏暗的 cn 作为 ADODB.Connection

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

  4. 将 Excel sheet 中的值加载到数组中

  5. 遍历数组并将每条记录插入table。例如:

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

这是我在其他回复者的帮助下遇到的上述问题的答案:

    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

我拥有的其他代码应该全部批处理,或者 none 如果有错误。但是,当确实发生错误时,它仍在写入成功通过的错误。

     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