Full Version: How is a function connected to an event?
UtterAccess Discussion Forums > Microsoft® Access > Access Reports
cedubose
OK, I have an old application I have to maintain -- it has forms and reports.

One of the reports (a whole bunch actually) has a function called "SetTotalClaimed", which sets one of the fields in the report to a certain value.

The thing is, I can't figure out how it gets called. The "Report_Open" event handler does not call it. It must be wired up to an event, but I can't figure out which one. The property page for the report says there is just one event -- the No Data event.

I have to create another report with similar functionality, so I really need to understand this.

How can I find out how this function is called?

Thanks,
Cynthia
NoahP
It could be the Control Source for an unbound control.

=SetTotalClaimed

It's it triggered by an event, it should show in the VBA window, unless it's used on the Event Tab like:

=SetTotalClaimed

HTH
Noah
cedubose
Well ...

(1) If it's defined as a Control Source, then the control is not unbound, is it? (I can't find a control that has this as its Control Source .. is there any place where they are listed?)

(2) What do you mean by the VBA window?

(3) What do you mean by the Event Tab?

Otherwise .. thanks!

Cynthia
niesz
Can you post the DB? You can delete all personal data in the tables. Sometimes seeing it is the easiest way.
cedubose
Hmmm, not really! The database is about 30MB, and there is a file of linked tables that is about 132MB. I mean, this is a large application!

I am just so confused as to how a function like this gets executed. Does anyone know the "lifecycle" of the generation of a report? Is it perhaps true that if there is a function in the code-behind, it will automatically get executed when the report is generated? That does not make sense to me, but I can't think of anything else right now.
NoahP
1) If a function is in the Control Source of a control, then the control IS unbound.

2) The VBA window is the window containing all the code for a given form, report or module.

3) The Event Tab is one of the tabs on the Property box. Right click anywhere in the report, and the Left Click Properties at the bottom of the menu that opens. Each control on a report, the report itself and each section of the report will have its own set or Properties.

Noah
niesz
There are many, many events that fire off code in the Code-Behind of a report. OnFormat, OnOpen, OnPrint, etc, etc....

More than likely this is where the function is being called from.
cedubose
OK, but how do I find what event handlers (if any) are connected to each event? Is there a list somewhere?

When I look on the events tab of the property window (thank you Noah) there seems to be only one event handler -- for the NoData event. The SetTotalClaimed function does not SEEM to be wired up to an event.

Cynthia
schroep
Open up your report in design view. Click VIEW/CODE from the menus to get to the full VBA code behind the report. Is the function called anywhere in there?

The other place it could be called, as mentioned, is in the CONTROLSOURCE property (not an event) for the textbox that displays the result.

It may also be code in a GLOBAL code module, which may be referenced from your report. Open up the MODULES in your application and check there. If you have the VBE open, you can do an EDIT/FIND and specify to search in the CURRENT PROJECT for your function name.
HiTechCoach
I could also be called from within the record source (query) of the report. It is possible to call the function from within the SQL.

Hope this helps...
cedubose
Here is the query that is the source of the report:

CODE

SELECT DISTINCT
    Sites.SiteName,
    Sites.KindegartenEntrants,
    Sites.OutOfStateXfers,
    Mandates.ProgramID,
    Mandates.ProgramName,
    Mandates.Chapter,
    Mandates.StatuteYear,
    Mandates.Appropriations,
    Mandates.EducCodes,
    Mandates.LongProgramName,
    Mandates.DetailFormName,
    Mandates.SummaryFormName,
    Mandates.ClaimFormName,
    Districts.*,
    DistrictInfo.Abbreviation,
    DistrictInfo.IndirectRate,
    DistrictInfo.SiteSize,
    DistrictInfo.[#Truants],
    DistrictInfo.[#Screened],
    DistrictInfo.[#ReScreened],
    DistrictInfo.Medical,
    tblUnitRates.Factor,
    tblUnitRates.YearID,
    tblClaims.EstimatedClaim,
    tblClaims.ReimbursementClaim
FROM
    (
        (
            Districts
            INNER JOIN
            DistrictInfo
            ON
            Districts.DistrictID = DistrictInfo.DistrictID
        )
        INNER JOIN
        Sites
        ON
        Districts.DistrictID = Sites.DistrictID
    )
    INNER JOIN
    (
        (
            tblUnitRates
            INNER JOIN
            tblClaims
            ON
            (tblClaims.MandateID = tblUnitRates.ProgramID)
            AND
            (tblUnitRates.YearID = tblClaims.ProgramYearID)
        )
        INNER JOIN
        Mandates
        ON
        tblClaims.MandateID = Mandates.ProgramID
    )
    ON
    Districts.DistrictID = tblClaims.DistrictID
WHERE
    (
        (
            (Mandates.ProgramID)=[forms]![PrintMenuFRM]![chooseProgramID]
        )
        AND
        (
            (Districts.DistrictID)=[forms]![PrintMenuFRM]![chooseDistrictBox]
        )
        AND
        (
            (DistrictInfo.YearID)=[forms]![PrintMenuFRM]![chooseProgramYearID]
        )
        AND
        (
            (Sites.DistrictID)=[forms]![PrintMenuFRM]![chooseDistrictBox]
        )
        AND
        (
            (tblClaims.ProgramYearID)=[forms]![PrintMenuFRM]![chooseProgramYearID]
        )
    )
ORDER BY
    Sites.SiteName;


and here is the entire visual basic code for this report:

CODE
  
Option Compare Database
Option Explicit
Public Function SetTotalClaimed()

On Error GoTo HandleErrors

  Me![13] = dblTotalClaimed + dblTotalClaimed2
  'frm![TotalClaimed] = dblTotalClaimed
  Exit Function


HandleErrors:
  MsgBox "Error was encountered...  Proceeding with errors." & vbCrLf & vbCrLf & "Please note the District and Program being printed for tech support.", vbInformation
  Resume Next

End Function

Private Sub Report_NoData(Cancel As Integer)
    If ContinueReportGeneration(Me.Name) = True Then
        Cancel = False
    Else
        Cancel = True
    End If
End Sub

Private Sub Report_Open(Cancel As Integer)
  DoCmd.Maximize
End Sub


I did do a search on the "SetTotalClaimed" function, and I can't find anywhere in the code that it is called, although it appears in the code-behind of several reports.

Also, none of the fields in the report have "SetTotalClaimed" as the Control Source. (Anyway, even though it's a function, it acts more like a Sub since nothing is returned.)
schroep
You indicated this was an old application.

Are you sure this function is ever being called? Perhaps it is just unused code, that served a purpose at one time but no longer.

Is there a control named [13] on your report? Does it get set as this function says it will? What is it's controlsource? Maybe the original author used to do this calculation via this "function", but now is doing it straight in the controlsource of the control, and this code is just "abandoned."

You could test if this code ever runs by putting a "debug" line in there:
CODE
Public Function SetTotalClaimed()

On Error GoTo HandleErrors

  MsgBox "THE FUNCTION WAS CALLED."

  Me![13] = dblTotalClaimed + dblTotalClaimed2
  'frm![TotalClaimed] = dblTotalClaimed
  Exit Function


HandleErrors:
  MsgBox "Error was encountered...  Proceeding with errors." & vbCrLf & vbCrLf & "Please note the District and Program being printed for tech support.", vbInformation
  Resume Next

End Function

If you never see that MessageBox pop up, the code is likely not used. Make a copy of your report and delete the "function" from it. If the report still works and all the totals are correct, the function is abandoned and not needed.
cedubose
No .. there is a [13] control, and it IS being set by this function. I have put a breakpoint in there. There is no value for the Control Source of this text box.

I just can't figure out WHY it is called. I can't find a call to it.

Now, by stepping through with the debugger, I am discovering that several functions in a module called "Report Code" are being executed. I can't find any calls to them. I don't understand it.

Is there some way an application could be wired up so that ALL reports call some function(s) in a module ... is there a way of doing that, and if so, is there a way of looking at what functions are called in this way?
schroep
Since we're all kind of shooting blind here, any chance you could zip up and post your MDB?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.