Go Back   Access World Forums > Microsoft Access Reference > Microsoft Access Tutorials

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
Old 10-29-2012, 06:07 PM   #1
ChrisO
Newly Registered User
 
ChrisO's Avatar
 
Join Date: Apr 2003
Location: Brisbane, Australia
Posts: 3,202
Thanks: 7
Thanked 272 Times in 194 Posts
ChrisO is just really nice ChrisO is just really nice ChrisO is just really nice ChrisO is just really nice
Common Class modules in Forms and Reports.

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:
' Used for both Form and Report properties.
Private ThisObjectProperty        As Access.Label

' Used only to sink Form Label events.
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)

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

    ' Set the object properties for both Form and Report parents.
    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:
' Used for sinking ----------
Public Property Let SetSinkHandlers(ByVal strHandler As String)
    ' Set the sink events.
    ThisObjectSink.OnClick = strHandler
    ThisObjectSink.OnMouseDown = strHandler
End Property

Private Sub ThisObjectSink_Click()
    ' Only for proof of concept.
    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)
    ' Only for proof of concept.
    MsgBox "You mouse downed on " & ThisObjectSink.Name & vbNewLine & _
           "at X = " & sngX & " and Y = " & sngY
End Sub
'----------



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

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

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.

__________________
Access 2003, Win7, GMT +10,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
ChrisO is offline  
Closed Thread

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I print a list of tables, reports, forms, modules? VelvetVoice General 5 02-24-2017 07:12 PM
Class modules?? for common operations databasedonr Modules & VBA 13 06-23-2016 07:46 PM
Hide / Unhide All forms - reports - Modules editolis Modules & VBA 5 02-22-2010 06:37 AM
How to take care Modules and Class Modules (Access) in ASP.Net nandar ASP and ASP.NET 0 02-08-2007 08:10 PM
[SOLVED] Tables, Queries, Forms, Reports and Modules Have Disappeared chathag General 1 05-25-2006 05:43 AM




All times are GMT -8. The time now is 02:29 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Sponsored Links

How to advertise

Media Kit


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World