Common Class modules in Forms and Reports. (1 Viewer)

Status
Not open for further replies.

ChrisO

Registered User.
Local time
Tomorrow, 01:03
Joined
Apr 30, 2003
Messages
3,202
Common Class modules in Forms and Reports.

As the title suggests, a Class module can not generally be used for both Forms and Reports if it includes WithEvents in the declaration line for the Control concerned.

The reason is that Controls on Reports do not have events and, so, the Class module will raise an error if called from a Report. Even in Access 2010, where a Report can be opened in a new way, there are ways to open the Report which will still raise an error when the Class is called.

The real answer is deceptively simple; split the declaration line into two parts, one for the Events of the Form and one for the Properties of both the Form and Report.

Hence, a single declaration line gets split into two parts such as:-
Code:
[color=green]' Used for both Form and Report properties.[/color]
Private ThisObjectProperty        As Access.Label

[color=green]' Used only to sink Form Label events.[/color]
Private WithEvents ThisObjectSink As Access.Label

Now, when we setup the class we differentiate the setup routine by determining the Object Type of the Parent of the Control concerned with:-
Code:
Public Property Set ThisControl(ByRef ThisControl As Object)

    [color=green]' Set the object event sink only if the parent of the caller is a Form.
    ' This avoids the error if called from a Report.[/color]
    If TypeOf ThisControl.Parent Is Access.Form Then
        Set ThisObjectSink = ThisControl
    End If

    [color=green]' Set the object properties for both Form and Report parents.[/color]
    Set ThisObjectProperty = ThisControl
    
End Property

Subsequently, we can call the Class and specify which Object, ThisObjectProperty or ThisObjectSink, we want to use, as in:-
Code:
[color=green]' Used for sinking ----------[/color]
Public Property Let SetSinkHandlers(ByVal strHandler As String)
    [color=green]' Set the sink events.[/color]
    ThisObjectSink.OnClick = strHandler
    ThisObjectSink.OnMouseDown = strHandler
End Property

Private Sub ThisObjectSink_Click()
    [color=green]' Only for proof of concept.[/color]
    MsgBox "You clicked on " & ThisObjectSink.Name
End Sub

Private Sub ThisObjectSink_MouseDown(ByRef intButton As Integer, _
                                     ByRef intShift As Integer, _
                                     ByRef sngX As Single, _
                                     ByRef sngY As Single)
    [color=green]' Only for proof of concept.[/color]
    MsgBox "You mouse downed on " & ThisObjectSink.Name & vbNewLine & _
           "at X = " & sngX & " and Y = " & sngY
End Sub
[color=green]'----------[/color]



[color=green]' Used for properties ----------[/color]
Public Property Let ObjectBackColor(ByVal lngBackColor As Long)
    [color=green]' Only for proof of concept.[/color]
    MsgBox "Setting object back color for " & ThisObjectProperty.Name
    
    ThisObjectProperty.BackColor = lngBackColor
End Property

Public Property Get ObjectBackColor() As Long
    [color=green]' Only for proof of concept.[/color]
    MsgBox "Getting object back color for " & ThisObjectProperty.Name
    
    ObjectBackColor = ThisObjectProperty.BackColor
End Property
[color=green]'----------[/color]


It is a deceptively simple answer but, if you have the need to use a Class module which incorporates WithEvents in a Report, there it is.


Chris.
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom