Event not Firing in MS Access VBA

前端 未结 2 1895
梦毁少年i
梦毁少年i 2021-01-27 13:18

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

2条回答
  •  我在风中等你
    2021-01-27 14:03

    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.

    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.

    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
    

    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.

    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.

    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.

提交回复
热议问题