简体   繁体   English

从 Excel VBA 处理特定的访问表单

[英]Addressing a specific Access Form from Excel VBA

I have an Excel sheet with a command button, which opens an Access form and fills it with data from the sheet.我有一个带有命令按钮的 Excel 工作表,它打开一个 Access 表单并用工作表中的数据填充它。

If I have two Access instances running (different databases) I get the message that VBA can not find the form.如果我有两个 Access 实例正在运行(不同的数据库),我会收到 VBA 找不到表单的消息。

Is it possible to address a specific database form?是否可以处理特定的数据库表单?

Current Code: DoCmd.OpenForm ("FORM_ABC")当前代码: DoCmd.OpenForm ("FORM_ABC")

Not sure you need a Form to do this kind of thing.不确定你需要一个表格来做这种事情。 Here's one way to send data from Excel to Access.这是将数据从 Excel 发送到 Access 的一种方法。

Sub InsertIntoX2()

    Dim cn As ADODB.Connection, rs As ADODB.Recordset, row As Long
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
            "Data Source=C:\Users\Excel\Desktop\Test.accdb;"

    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "tblTrx", cn, adOpenKeyset, adLockOptimistic, adCmdTable

    row = 3    ' the start row in the worksheet
    Do While Not IsEmpty(Worksheets("Sheet1").Range("A" & row))

        With rs
            .AddNew    ' create a new record
            .Fields("ID") = Worksheets("Sheet1").Range("A" & row).Value
            .Fields("Product") = Worksheets("Sheet1").Range("B" & row).Value
            .Fields("ProdDate") = Worksheets("Sheet1").Range("C" & row).Value
            .Update
        End With
        row = row + 1
    Loop

    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing

End Sub

If you want to use an Excel form to capture data and save the data to a Worksheet, and then send the results to Access, use the VBA code sample described above.如果要使用 Excel 表单捕获数据并将数据保存到工作表,然后将结果发送到 Access,请使用上述 VBA 代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM