简体   繁体   English

从Excel VBA运行访问表单

[英]Running Access Form from Excel VBA

I am using Excel 2013 and Access 2013. My access query has a form that takes2 inputs, "start date" and "end date", and then runs a macro in access. 我正在使用Excel 2013和Access2013。我的访问查询的表单需要2个输入(“开始日期”和“结束日期”),然后在访问中运行宏。 Is there a way I can use VBA to input "start date" and "end date" from excel and run the form in access? 有没有一种方法可以使用VBA从excel输入“开始日期”和“结束日期”并在Access中运行表单? Here is what I have tried, but I get an error saying "The action or method is invalid because the form or report isn't bound to a table or query" 这是我尝试过的操作,但出现错误,提示“操作或方法无效,因为表单或报表未绑定到表或查询”

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

This is what my form looks like. 这就是我的表格。 Click me runs a macro, the first text box holds variable "start" and second one holds variable "end" 单击我运行一个宏,第一个文本框包含变量“开始”,第二个文本框包含变量“结束”

在此处输入图片说明

Since I assume your form has no recordsource, and the controls are unbound, the following should avoid the error you've encountered: 由于我假设您的表单没有记录源,并且控件是未绑定的,因此以下内容应避免您遇到的错误:

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

However, this still does nothing more than just open the form and set the start date, and closes Access immediately after. 但是,这只不过是打开表单并设置开始日期,并在此之后立即关闭Access。

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

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