事件未触发在MS Access VBA [英] Event not Firing in MS Access VBA

查看:411
本文介绍了事件未触发在MS Access VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MS Access中的形式有一个形象。该图像具有打开一个模式窗体的Click事件。模态形式所具有的确定和取消按钮。当您单击确定按钮,一个事件应该火,告诉哪个按钮被点击的主要形式。 (这是为了模拟在C#中的DialogResult功能)。然而,code。在事件处理程序永远不会运行。

I have a form in MS Access which has an image. The image has an Click event which opens a modal form. The modal form has an OK and Cancel button. When you click the OK button, an event is supposed to fire which tells the main form which button was clicked. (This is to simulate the DialogResult functionality in C#). However, the code in the event handler never runs.

模式窗体具有以下的常规声明:

The modal form has the following in the general declarations:

Public Event OnDialogBoxClose(NewRecordID As Long, DialogResult As DialogResults)

和以下code单击OK按钮其中:

and the following code where the OK button is clicked:

RaiseEvent OnDialogBoxClose(NewHardwareBaseItemID, dlgresBtnOKClicked)

主要形式有一般的声明如下:

The main form has the following in the general declarations:

Dim WithEvents RespondQuickAddClose As Form_qckfrmHardwareBaseItemCreate

和下面的事件处理:

Private Sub RespondQuickAddClose_OnDialogBoxClose(NewRecordID As Long, DialogResult As DialogResults)

    MsgBox "Responding to closing of the dialog box" 'Never happens
    Me.Requery

End Sub

有人可以解释为什么在事件处理程序不会被调用? 谢谢!

Can someone explain why the event handler is never called? Thanks!

背景:

这一切的目的是为了让一个模式对话框添加一个条目,然后返回入口的ID回到主窗体设置控件的值。例如,假设你正在填写一份保险单,您需要选择一个品牌的汽车,这是不存在的。你点击它会弹出模态对话框,允许您添加的汽车品牌的图标。然后,当您单击确定,它需要你回到保险的形式,选择刚刚创建的汽车品牌。

The purpose of all this is to allow a modal dialog box to add an entry, then return the ID of the entry back to the main form to set the value of controls. For instance, imagine you are filling out an insurance form, and you need to select a brand of car this is not there. You click on an icon which pops up with the modal dialog box to allow you to add the car brand. Then when you click OK, it takes you back to the insurance form and selects the brand of car you just created.

这遵循一个例子,我在这里找到: http://database.itags.org/ms-access-database/80292/

This follows an example I found here: http://database.itags.org/ms-access-database/80292/

推荐答案

你让你的生活从不同的开发环境来访问VBA应用概念的方式太复杂了。虽然VBA不支持WithEvents就/的RaiseEvent,没有理由得到,这里复杂的。

You're making your life way too complicated by applying concepts from a different development environment to Access VBA. While VBA does support WithEvents/RaiseEvent, there's no reason to get that complicated here.

在访问对话框上班通常的方法是,而不是关闭它们,你隐藏起来。这样的形式后,code是开放的,同时使值的形式提供在code使用运行。

The usual way to work with dialogs in Access is that instead of closing them, you hide them. This allows the code after the form was open to run while leaving the values in the form available for use in that code.

样品code在一份报告中的OnOpen事件打开收集值过滤报表形式:

Sample code in the OnOpen event of a report that opens a form for collecting values to filter the report:

  Private Sub Report_Open(Cancel As Integer)
    DoCmd.OpenForm "dlgDateRange", , , , , acDialog, "ThisYear"
    If IsLoaded("dlgDateRange") Then
       With Forms!dlgDateRange
         If .Tag = "Cancel" Then
            Cancel = True
         Else
            Me.Filter = "[InvoiceDate] Between #" & !txtStart & "# AND #" & !txtEnd & "#"
            Me.FilterOn = True
            Me!lblDateRange.Caption = StrConv(Trim(("from " + varZLStoNull(Format(!txtStart, "mm/dd/yyyy"))) _
                & (" to " + varZLStoNull(Format(!txtEnd, "mm/dd/yyyy")))), vbProperCase)
         End If
       End With
       DoCmd.Close acForm, "dlgDateRange"
    End If
  End Sub

该对话框的形式有两个命令按钮,继续>>和取消。 CANCEL按钮设置窗体的标记为取消,并设置窗体的。可见属性设置为False。 Continue(继续)>>按钮不只是形式的。可见属性设置为False。点击任何这些按钮允许code继续对表后线与acDialog开关打开。

The dialog form has two command buttons, CONTINUE>> and CANCEL. The CANCEL button sets the form's tag to "Cancel" and sets the form's .Visible property to False. The CONTINUE>> button does nothing but set the form's .Visible property to False. Clicking either of those buttons allows the code to continue on the line after the form is open with the acDialog switch.

我的哲学是使对话的愚蠢越好。调用code已经知道它要寻找的形式(即,你需要知道你正在阅读数据出来的控件的名称),但可能各地通过增加客户属性的形式得到。但你必须知道的属性名,所以你刚才移动了球。

My philosophy is to make the dialogs as stupid as possible. The calling code has to know what it's looking for in the forms (i.e., you need to know the names of the controls you're reading data out of), but that could be gotten around by adding customer properties to the form. But then you have to know the property names, so you've just moved the ball.

我也实施了这种事情在类模块包裹dailog形式,然后调用上下文只是初始化类的一个实例,然后拉在适当的时间价值出来。但是,这实际上更复杂,上面的方法。

I have also implemented this kind of thing by wrapping the dailog form in a class module, and then the calling context simply initializes an instance of the class and then pulls the values out of it at the appropriate time. But that's actually more complicated that the approach above.

这篇关于事件未触发在MS Access VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆