Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class module and "WithEvents" Problem 1

Status
Not open for further replies.

belovedcej

Programmer
Nov 16, 2005
358
US
I am attempting to use the principles outline in this help article:

My problem is in actually getting the Property Set to work properly.

Here's the part in my class module:

Code:
Private WithEvents rpt As Access.Report

Property Set Report(rptIn As Access.Report)
    Set rpt = rptIn
End Property

Here's how I'm using it (this is a report module)

Code:
Private rpt As ReportHandler

Private Sub Report_Open(Cancel As Integer)
Set rpt = New ReportHandler
Set rpt.Report = Report_rptPendingClaims
End Sub

In running this through the debugger, my Report never gets passed into the set event of the class module. It doesn't say null or nothing - it's just blank. Which, of course, explains why nothing else works.

Could anyone tell me what I'm doing wrong? I have also tried:
Set rpt.Report = Me

But that had the same problem.
 
OKay - I was wrong, the object is setting as it should. Here's my whole class module:

Code:
Private WithEvents rpt As Access.Report
Private rptName As String

Property Get Report()
    Report = rpt
End Property

Property Set Report(rptIn As Access.Report)
    Set rpt = rptIn
    rptName = rpt.Name
End Property

Private Sub Report_NoData(Cancel As Integer)
    MsgBox "No Data to Report."
    Cancel = True
End Sub

Private Sub Report_Open()
    DoCmd.RunCommand acCmdZoom75
    MsgBox "It's working"
End Sub

Private Sub Report_Close()
    MsgBox "It's working"
End Sub

Private Sub Report_Error(DataErr As Integer, Response As Integer)
    MsgBox Err.Description & " " & Err.Number
    Response = acDataErrContinue
End Sub

The problem is that I don't get my little message boxes telling me the events are actually sinking. Has anyone ever tried this before? Do you have any pointers?
 
I got the example to work. Did you create a separate Class Module as shown in the example. I created a new class module and called it cReportErrorHandler and this is the code inside it


Option Explicit

'Define the object variable of the object type whose events you want to
'track. See Step 1 in the "More Information" section.
Private WithEvents rpt As Access.Report

'Define property procedures so that you can identify which particular report
'each instance of the class will be using. See Step 1 in the "More
'Information" section.
Public Property Get Report() As Access.Report
Set Report = rpt
End Property
Public Property Set Report(rptIn As Access.Report)
Set rpt = rptIn
End Property
'Define the event procedure that you want to sink. Note that it uses the
'same definition as the Error event procedure in a report's module.
Private Sub rpt_Error(DataErr As Integer, Response As Integer)
MsgBox DataErr, vbOKOnly, "This Is My Error Message!"
Response = acDataErrContinue
End Sub



Then in the report I wanted to test I added this to the module behind the report:

Option Compare Database
Option Explicit

Private clsReportError As cReportErrorHandler

Private Sub report_Open(Cancel As Integer)
Set clsReportError = New cReportErrorHandler
Set clsReportError.Report = Me
End Sub


Finally I ensured that for the Forms Error Event had [Event Procedure] selected.

and then I opened the report with a known error, and it returned the expected messagebox with only the error number and my title.

PaulF
 
I followed the instructions to the "T". Everything is identical.

here's my report class:
Code:
Private WithEvents rpt As Access.Report
Public Property Get Report()
    Set Report = rpt
End Property
Public Property Set Report(rptIn As Access.Report)
    Set rpt = rptIn
End Property
Private Sub Report_NoData(Cancel As Integer)
    MsgBox "No Data to Report."
    Cancel = True
End Sub
Private Sub Report_Open()
    DoCmd.Maximize
    DoCmd.RunCommand acCmdZoom75
End Sub
Private Sub Report_Close()
    DoCmd.Restore
End Sub
Private Sub Report_Error(DataErr As Integer, Response As Integer)
    MsgBox Err.Description & " " & Err.Number
    Response = acDataErrContinue
End Sub

Here's my report which I know has no data:
Code:
Option Compare Database
Option Explicit
Private rpt As clsReportHandler
Private Sub Report_Open(Cancel As Integer)
Set rpt = New clsReportHandler
Set rpt.Report = Me
End Sub

All the events in the properties have event procedure selected.

But not only do I get no error when there is no data, it does not open maximized.

Also, I have tried entering a message box on my report on the no data event, and then I debugged. It never enters the procedure in the class module. Instead it runs through the nodata event on the report module twice.

I wonder if it's because I have Access 2002. Perhaps something has changed, because I have noticed that the applies to section only mentions access 2000.
 
Oops - there's a typo. My open event really does have the Cancel As Integer parameter - I just missed it. Even with that, it doesn't work.
 
don't know if this is a typo or not but

Public Property Get Report()
should be
Public Property Get Report() As Access.Report

I wasn't able to get it to run the Open Events, so I moved them to the Activate event and it maximized as requested but returned an error stating that Zoom wasn't available at that time, you might need to call that from the command button that opens the report.


and finally did you check to see if you had [Event Procedure] in the dropdown box for EACH of the Events you wanted to trap on the report?

PaulF
 
I have changed the Property get. It doesn't really make a difference, though, because I'm never using it.

I had made sure Event Procedure was in the properties for all the events of the report. Still nothing.

Thanks for the thoughts...
 
I got the answer!!!

Notice that my variable is rpt, but my events all start with Report_

As soon as I changed the Report to rpt it worked.

Thanks so much for taking the time to try to help!
 


Regarding the below:

Code:
I wasn't able to get it to run the Open Events, so I moved them to the Activate event and it maximized as requested but returned an error stating that Zoom wasn't available at that time, you might need to call that from the command button that opens the report.

right on both counts - thanks for pointing it out. Have a star for your work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top