简体   繁体   English

如何从 Excel VBA 代码触发 Access 数据库表单中的单击

[英]How to trigger a click in Access database form from Excel VBA code

I'm trying to trigger a click on "OK" button of a form in Access database from a VBA code in Excel.我试图从 Excel 中的 VBA 代码触发单击 Access 数据库中表单的“确定”按钮。

The main idea is as follows.主要思想如下。 A VBA code in Excel goes through the following steps: Excel 中的 VBA 代码经过以下步骤:

  1. Opens an Access database打开 Access 数据库
  2. Opens a form in this Access database在此 Access 数据库中打开一个表单
  3. Fills the form with some data用一些数据填充表单
  4. Triggers a click on "OK" button of the opened form触发单击已打开表单的“确定”按钮
  5. Closes the form关闭表单
  6. Closes the Access database关闭 Access 数据库

As for now, the code is able to open the database, to open the form, to fill it with data, but I'm unable to force the click on the "OK" button.至于现在,代码能够打开数据库,打开表单,用数据填充它,但我无法强制单击“确定”按钮。 I tried several different approaches of type:我尝试了几种不同的类型方法:

.Forms("Subscriptions_Redemptions").Controls("OK").Click

but neither works...I would appreciate any help.但两者都不起作用......我将不胜感激。 To make things clear, clicking on "OK" button activate an access VBA code that dispatches the information from the form in the database using "Event Procedure" On Click.为了清楚起见,单击“确定”按钮激活访问 VBA 代码,该代码使用单击时使用“事件过程”​​从数据库中的表单发送信息。

Here is the present version of the code:这是代码的当前版本:

Private Sub CommandButton4_Click()
    Dim appAccess As Object

    'create new access object
    Set appAccess = CreateObject("Access.Application")

    'open the acces project
    Call appAccess.OpenCurrentDatabase( _
    "H:\PROD\HIGH_YIELD\HY_LUX\Database\HY_LUX.mdb")
    appAccess.Visible = True

    With appAccess
        Application.DisplayAlerts = False
       .DoCmd.OpenForm "Subscriptions_Redemptions"
       .Forms("Subscriptions_Redemptions").DateVal = "04/12/2019"
       .Forms("Subscriptions_Redemptions").Controls("OK").Click
    End With

    Set appAccess = Nothing
End Sub

You should call form's object model, that means following syntax:您应该调用表单的对象模型,这意味着以下语法:

.Forms![Subscriptions_Redemptions].ButtonName_Click

Where ButtonName is an button control object name, that can be specified through properties window, or looking at event handler其中ButtonName是按钮控件对象名称,可以通过属性窗口指定,或查看事件处理程序

Thanks to Van Ng and SunKnight0, I have found the problem!感谢 Van Ng 和 SunKnight0,我找到了问题所在! As correctly pointed out by SunKnight0, the issue was the Private definition of the click instead of a Public one.正如 SunKnight0 正确指出的那样,问题是点击的私有定义而不是公共定义。 It works great now.现在效果很好。 Thanks for your contribution.感谢您的贡献。

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

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