Running Access Form from Excel VBA
我正在使用Excel 2013和Access2013。我的访问查询的格式为接受两个输入,即"开始日期"和"结束日期",然后在访问中运行宏。有没有一种方法可以使用VBA从excel输入"开始日期"和"结束日期"并在Access中运行表单?这是我尝试过的操作,但是出现错误消息"操作或方法无效,因为表单或报表未绑定到表或查询"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Sub RunAccessQuery() Dim strDatabasePath As String Dim appAccess As Access.Application Dim startDate As Date Dim endDate As Date startDate = ThisWorkbook.Sheets("sheet1").Range("B2").Value strDatabasePath ="access database path" Set appAccess = New Access.Application With appAccess Application.DisplayAlerts = False .OpenCurrentDatabase strDatabasePath .DoCmd.OpenForm"inputForm", , ,"start =" & startDate '.Quit End With Set appAccess = Nothing ThisWorkbook.RefreshAll MsgBox ("Data has been updated") End Sub |
这是我的表格的样子。单击我运行一个宏,第一个文本框保存变量" start ",第二个文本框保存变量" end "
由于我假设您的表单没有记录源,并且控件未绑定,因此以下内容应避免您遇到的错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Sub RunAccessQuery() Dim strDatabasePath As String Dim appAccess As Access.Application Dim startDate As Date Dim endDate As Date startDate = ThisWorkbook.Sheets("sheet1").Range("B2").Value strDatabasePath ="access database path" Set appAccess = New Access.Application With appAccess Application.DisplayAlerts = False .OpenCurrentDatabase strDatabasePath .DoCmd.OpenForm"inputForm" .Forms("inputForm").start = startDate '.Quit End With Set appAccess = Nothing ThisWorkbook.RefreshAll MsgBox ("Data has been updated") End Sub |
但是,除了打开表单并设置开始日期,并在此之后立即关闭Access之外,这仍然无济于事。