当Access被声明为数据库而不是对象时,如何从Excel调用Access子?

时间:2017-05-24 16:15:19

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

    Sub exportRecords()

    Dim report As Worksheet
    Set report = Sheets("Report")

    Dim lastrow, x As Integer
        With report
            lastrow = report.Range("A" & .Rows.Count).End(xlUp).Row
        End With

    Dim db As Database
    Dim rs As DAO.Recordset

    Set db = OpenDatabase("\\~\myDB.accdb")
    Set rs = db.OpenRecordset("TblData", dbOpenTable)

    For x = 2 To lastrow
        If report.Range("AI" & x) = "MSH" Then
            rs.AddNew
            rs.Fields("Date") = report.Range("A" & x).Value
            rs.Fields("AgentName") = report.Range("E" & x).Value
            rs.Fields("Tardy") = report.Range("S" & x).Value
            rs.Fields("Comments") = report.Range("X" & x).Value
            rs.Update
        Else
        End If
    Next x

    '****THIS IS WHERE I WANT TO RUN THE ACCESS SUB/MACRO****

    rs.Close
    db.Close


    End Sub

如果将Access声明为对象,我熟悉如何调用外部子:

Dim accApp As Object
Set accApp = CreateObject("Access.Application")
accApp.OpenCurrentDatabase("\\~\myDB.accdb")
accApp.DoCmd.RunMacro "Modulename.Macroname"

但如果我走那条路,那么我不确定如何使用DAO添加记录,就像我在下面一样。有没有其他方法来调用sub?

0 个答案:

没有答案