Access Subreport with TOP clause

I have a report with a subreport that uses TOP 1 clause in the query but it is not displaying the subreport when I use TOP 1.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
perhaps there is no record to display
PapoteAuthor Commented:
If I remove the TOP clause the records DO show up using the same key.
I tested the query alone with the specific key and it works, even the report alone, but not when using the Top clause as a subreport.
Dale FyeOwner, Developing Solutions LLCCommented:
How is your report configured?  Is your subreport linked (Master/Child) to the parent report?  If so, an you use the Top 1 syntax in the query, you will only see the entry in the sub-report for that record in the main report that corresponds to the record in the subreport.

You might want to change the query of the subreport to something like:

SELECT [GroupID], [Field1], [Field2]
FROM SomeTable as T
WHERE [Field1] = (SELECT MAX([Field1]) FROM SomeTable WHERE [ID] = T.ID)

Assuming that the [GroupID] field is used in the main report, this query would return one record (with all the appropriate fields) for each [GroupID] value.  Then use the [GroupID] field in your master/child relationship and you will have your main report and a single record for each matching GroupID in the subreport.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Hamed NasrRetired IT ProfessionalCommented:
Top 1 for a seubreport will use only 1 record from the underlying recordsource. So I expect your report to not show any record, or that only one record selected using Top 1.

Change top 1 to top 100 and check!
Hamed NasrRetired IT ProfessionalCommented:
To display top record for each subreport, add a text box to the detail section of the subreport, say txtX
Control SOurce=1
Running Sum: Over All
Visible: No

Remove the top 1, and sort properly.
Detail_Format event:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    If txtX > 1 Then
        Cancel = True
    End If
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hamed NasrRetired IT ProfessionalCommented:
Try this:

' No page footer on page 1
Private Sub PageFooterSection_Format(Cancel As Integer, FormatCount As Integer)
    If Page = 1 Then
        If Page = pages Then
          Cancel = True
        End If
    End If
End Sub

' No report footer and increase size of page footer
Private Sub ReportFooter_Format(Cancel As Integer, FormatCount As Integer)
    PageFooterSection.Height = PageFooterSection.Height + 3000  '(adjust to your needs-do some homework)
    Cancel = True
 End Sub
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.