Adding a custom event to a form

G

Guest

All

My overall goal today is to put together a few class we can drop in our new Access (2000) apps. The first of these jewels, the calendar form

I've dropped a calendar control and command button (cmbOk) onto an Access form (frmCal). The form has a custom event BeforeHide(ValueOfCalendarControl As Date), which has a date parameter - the value of the calendar control.

Public Event BeforeHide(ByVal dteDteSel As Date

The event is `raised` when the Ok (cmbOk) button is clicked, which causes the form to become invisible

Private Sub cmbOk_Click(
RaiseEvent BeforeHide(mdteDteSel

Let Me.Visible = Fals
End Su

This form also has 2-methods CloseSelf() and OpenSelf() - code below. CloseSelf() closes the form, and OpenSelf() opens the form

Public Function CloseSelf(
Call DoCmd.Close(acForm, Me.Name
End Functio

Public Function OpenSelf(
Call DoCmd.OpenForm(Me.Name, acNormal
End Functio

For testing purposes, I've made another form (frmTest). I've got a text box on this (test) form, which when double-clicked spawns my nifty calendar modal. I select a date and click the Ok button and the form disappears as expected; however, the test form never receives the date because the BeforeHide() event is never trapped. Here is the code for my test form

Private WithEvents frm As Form_frmCa

Private Sub Form_Load(
Set frm = New Form_frmCa
End Su

Private Sub Form_Unload(Cancel As Integer
Call frm.CloseSel
Set frm = Nothin
End Su

Private Sub frm_BeforeHide(ByVal dteDteSel As Date
Debug.Print "The BeforeHide() event executed!
Let Me.tbDate.Value = CStr(dteDteSel
End Su

Private Sub tbDate_DblClick(Cancel As Integer
Call frm.OpenSel
End Su

These are the steps I follow when trapping events for other applications that I automate; however, its not working for Access forms

Regards
Adam
 
D

Dan Artuso

Hi,
It's to do with calling the OpenSelf method.
When you call this, *another instance* of the form is created that has nothing
to do with your frm variable, hence you do not get the event raised.

If you change your code like this:

Private Sub tbDate_DblClick(Cancel As Integer)
frm.Visible = True
End Sub

then all will be well because you're then seeing the form instance associated with the frm variable.
--
HTH
Dan Artuso, Access MVP


Adam said:
All,

My overall goal today is to put together a few class we can drop in our new Access (2000) apps. The first of these jewels, the calendar form.

I've dropped a calendar control and command button (cmbOk) onto an Access form (frmCal). The form has a custom event
BeforeHide(ValueOfCalendarControl As Date), which has a date parameter - the value of the calendar control.
Public Event BeforeHide(ByVal dteDteSel As Date)

The event is `raised` when the Ok (cmbOk) button is clicked, which causes the form to become invisible.

Private Sub cmbOk_Click()
RaiseEvent BeforeHide(mdteDteSel)

Let Me.Visible = False
End Sub

This form also has 2-methods CloseSelf() and OpenSelf() - code below. CloseSelf() closes the form, and OpenSelf() opens the form.

Public Function CloseSelf()
Call DoCmd.Close(acForm, Me.Name)
End Function

Public Function OpenSelf()
Call DoCmd.OpenForm(Me.Name, acNormal)
End Function

For testing purposes, I've made another form (frmTest). I've got a text box on this (test) form, which when double-clicked spawns
my nifty calendar modal. I select a date and click the Ok button and the form disappears as expected; however, the test form never
receives the date because the BeforeHide() event is never trapped. Here is the code for my test form:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top