[AccessD] Referencing forms as objects

jwcolby jwcolby at colbyconsulting.com
Mon Aug 1 11:29:16 CDT 2011


Dan, here ya go.

Notice that in order to do this I had to:

1) Dimension a form object WithEvents in the header of the calling form.  This tells the calling 
form that it has a class (form in this case) that may be raising events.

2) Build one or more event sink.  This takes the object dimensioned in step 1 above and sinks one 
(or more) events that that object is raising.

3) In the object raising the event I have to define the event that it will raise in the header of 
that object (the modal form in this case).  This tells the compiler that this object is capable of 
raising at least one event.

4) Raise the event wherever needed.  This actually causes the event to fire and transfers data of 
event parameters are defined.

These 4 steps are always required to use events.

Only classes can raise events.  Only classes can sink events.  Forms are classes, ergo forms can 
sink events (we already know that) but forms can also raise events.

In the calling form:

'Header
'
'Define an object (a form) that we are going to sink events for
'
Public WithEvents frmDiaryExtensionReason As Form_frmDiaryExtensionReason

'
'event sink
'This is where the data comes back from the modal form
'
Private Sub frmDiaryExtensionReason_evExtendDate(dte As Date, strExtendReason As String)

     	txtExtendReason.Value = strExtendReason
	txtExtendDate.value = dte
	me.dirty = false
End Sub

'The combo that causes the unbound form to open
Private Sub cboCloseReason_AfterUpdate()
     Select Case cboCloseReason.column(0)
     Case 2      'Rescheduled - needs to be copied then the original closed
         DoCmd.OpenForm "frmDiaryExtensionReason"
         Set frmDiaryExtensionReason = Forms("frmDiaryExtensionReason")

In the modal form:

'this defines the event that the form will raise
'
Public Event evExtendDate(dte As Date, strExtendReason As String)
'
Private blnValidDate As Boolean

Private Sub cmdCancel_Click()
     DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdExtendDate_Click()
     If blnValidDate Then
         'Validate that they put something in the comment
         If Len(txtExtendReason.Value) Then
             '
	    'This is where the event is raised and data sent of to someone else
             RaiseEvent evExtendDate(txtExtendDate.Value, txtExtendReason.Value)
             DoCmd.Close acForm, Me.Name
         Else
             MsgBox "No reason was entered"
             txtExtendReason.SetFocus
         End If
     Else
         MsgBox "The date entered is not valid"
         txtExtendDate.Value = ""
         txtExtendDate.SetFocus
     End If
End Sub

'Validate that the entry is a date
Private Sub txtExtendDate_AfterUpdate()
Dim dte As Date
     On Error Resume Next
     dte = txtExtendDate.Value
     If Err Then
         blnValidDate = False
     Else
         blnValidDate = True
     End If
End Sub


John W. Colby
www.ColbyConsulting.com



More information about the AccessD mailing list