Generating report on a dynamic subform

Engr. Matthew

New member
Local time
Today, 04:23
Joined
Feb 27, 2021
Messages
15
I have a main form with subform that loads different subform dynamically.
the form is working very well and subform is displaying correctly.
I am trying to retrieve the information based on the main form and any loaded subform to generate a report based on OrderID and PatientID.
Here is the situation
Private Sub Report_Open(Cancel As Integer)

On Error GoTo Err_Handler

Dim strReportName As String
Dim ctlSub As Control

' --- 1. Check that ReportToLoad exists ---
If Not TempVars.Exists("ReportToLoad") Then
MsgBox "Missing ReportToLoad variable.", vbExclamation, "Report_Open"
Cancel = True
Exit Sub
End If

strReportName = "Report." & TempVars!ReportToLoad
Debug.Print "Loading subreport:", strReportName

' --- 2. Try to load the subreport ---
Set ctlSub = Me!subCompletedReport
ctlSub.SourceObject = strReportName

' --- 3. Don’t set any links yet (show all data first) ---
Debug.Print "? Subreport loaded successfully."

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Report_Open Error"
Resume Exit_Handler


End Sub

This is the code on the report
Private Sub cmdBtnPreviewReport_Click()

On Error GoTo Err_Handler

Dim lngOrderID As Long
Dim lngPatientID As Long
Dim strTestName As String
Dim strReportName As String

' === 1. Collect main identifiers ===
lngOrderID = Nz(Me!OrderID, 0)
lngPatientID = Nz(Me!PatientID, 0)
strTestName = Nz(Me!TestName, "")

If lngOrderID = 0 Or lngPatientID = 0 Or Len(strTestName) = 0 Then
MsgBox "Missing patient or test information.", vbExclamation
Exit Sub
End If

' === 2. Lookup corresponding report name ===
strReportName = Nz(DLookup("ReportName", "tblTestForms", "TestName='" & strTestName & "'"), "")
If Len(strReportName) = 0 Then
MsgBox "No report found for test: " & strTestName, vbExclamation
Exit Sub
End If

' === 3. Save key values in TempVars ===
TempVars.RemoveAll
TempVars.Add "OrderID", lngOrderID
TempVars.Add "PatientID", lngPatientID
TempVars.Add "TestName", strTestName
TempVars.Add "ReportToLoad", strReportName

' === 4. Open the master container report ===
Debug.Print "Opening rptCompletedTest ? " & strReportName
DoCmd.OpenReport "rptCompletedTest", acViewReport ' ? use acViewReport, not Preview

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "cmdBtnPreviewReport_Click"
Resume Exit_Handler


End Sub

The query parameter look like this

PARAMETERS OrderID Long, PatientID Long;
SELECT [Torch Panel].*
FROM [Torch Panel Test]
WHERE [OrderID] = [OrderID]
AND [PatientID] = [PatientID];


The I run the click button on the main form I go Error 348: can someone help
 
Code:
Private Sub Report_Open(Cancel As Integer)

On Error GoTo Err_Handler

Dim strReportName As String
Dim ctlSub As Control

' --- 1. Check that ReportToLoad exists ---
If Not TempVars.Exists("ReportToLoad") Then
MsgBox "Missing ReportToLoad variable.", vbExclamation, "Report_Open"
Cancel = True
Exit Sub
End If

strReportName = "Report." & TempVars!ReportToLoad
Debug.Print "Loading subreport:", strReportName

' --- 2. Try to load the subreport ---
Set ctlSub = Me!subCompletedReport
ctlSub.SourceObject = strReportName

' --- 3. Don’t set any links yet (show all data first) ---
Debug.Print "? Subreport loaded successfully."

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Report_Open Error"
Resume Exit_Handler


End Sub

On form?
Code:
Private Sub cmdBtnPreviewReport_Click()

On Error GoTo Err_Handler

Dim lngOrderID As Long
Dim lngPatientID As Long
Dim strTestName As String
Dim strReportName As String

' === 1. Collect main identifiers ===
lngOrderID = Nz(Me!OrderID, 0)
lngPatientID = Nz(Me!PatientID, 0)
strTestName = Nz(Me!TestName, "")

If lngOrderID = 0 Or lngPatientID = 0 Or Len(strTestName) = 0 Then
MsgBox "Missing patient or test information.", vbExclamation
Exit Sub
End If

' === 2. Lookup corresponding report name ===
strReportName = Nz(DLookup("ReportName", "tblTestForms", "TestName='" & strTestName & "'"), "")
If Len(strReportName) = 0 Then
MsgBox "No report found for test: " & strTestName, vbExclamation
Exit Sub
End If

' === 3. Save key values in TempVars ===
TempVars.RemoveAll
TempVars.Add "OrderID", lngOrderID
TempVars.Add "PatientID", lngPatientID
TempVars.Add "TestName", strTestName
TempVars.Add "ReportToLoad", strReportName

' === 4. Open the master container report ===
Debug.Print "Opening rptCompletedTest ? " & strReportName
DoCmd.OpenReport "rptCompletedTest", acViewReport ' ? use acViewReport, not Preview

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "cmdBtnPreviewReport_Click"
Resume Exit_Handler


End Sub

he query parameter look like this

Code:
PARAMETERS OrderID Long, PatientID Long;
SELECT [Torch Panel].*
FROM [Torch Panel Test]
WHERE [OrderID] = [OrderID]
AND [PatientID] = [PatientID];

As mentioned removed the error handler to see where code breaks, and provide the correct error code (3048? 438?)
 
Also I would get rid of Bang notation here because you lose intellisense and making it hard to debug. You cannot verify that those names are correct
Code:
lngOrderID = Nz(Me!OrderID, 0)
lngPatientID = Nz(Me!PatientID, 0)
strTestName = Nz(Me!TestName, "")
Maybe it is Patient_ID, or NameOfTest. With dot you would know at compile time not at runtime.
 
I see some questions as to which error you get. According to what I've read, 348 suggests that an object is involved and for some reason that object is not visible. Reasons include having closed the object, having set its object variable to nothing, having defined it out-of-scope (visibility). In the most generic terms, it is Access 's way of saying "I can't find that; I can't see that; I don't know what that is."

If you can remove the error handlers so that you can get the "Unhandled error" handler to highlight the error, you would give us info that we need to trace down the problem.
 
You don’t have to remove the error handling code since “Break on all errors” is available in the Options. Remember to set this back to unhandled errors when finished debugging.
 

Users who are viewing this thread

Back
Top Bottom