Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Dynamic Reports
William Kennedy 
    
2 years ago
Instead of using many queries and many reports I am trying to dynamically load SQL into a standard report shell.   I am selecting the SQL Function found in a form and load into a standard Report.  Each Sql selects a group of categories from a union query.   The report has the same field names and types that the SQL defines.   I can put the selected SQL in the Report's Property Record Source and the report opens correctly.  What I can't seem to get right is the process of putting the SQL into the report from the form.
William Kennedy OP  @Reply  
    
2 years ago

William Kennedy OP  @Reply  
    
2 years ago

William Kennedy OP  @Reply  
    
2 years ago

William Kennedy OP  @Reply  
    
2 years ago

William Kennedy OP  @Reply  
    
2 years ago

Sami Shamma  @Reply  
             
2 years ago
How are you changing the Report Recordset? Are you using VBA?
Please share the way you call the report from the Form
William Kennedy OP  @Reply  
    
2 years ago
I've tried many methods VBA seems to be the most flexible.  But I'm relatively new at VBA.  Any suggestions?  my last effort at VBA was:  Private Sub NZSelBtn_Click()
    On Error Resume Next
    
    ' Get the ID and SQLData from the table NZMultiReport_T
    Dim recordID As Long
    Dim strSQL As String
    
    ' Assuming the ID field in your form is named "ID"
    recordID = Me.ID
    strSQL = DLookup("SQLData", "NZMultiReport_T", "ID = " & recordID)
    
    ' Check if SQL statement is not empty
    If strSQL <> "" Then
        ' Modify the RecordSource property of the report "NZxxx_R"
        Reports("NZxxx_R").RecordSource = strSQL
        
        ' Requery the report to reflect changes
        Reports("NZxxx_R").Requery
        
        ' Open the report in preview mode
        DoCmd.OpenReport "NZxxx_R", acViewPreview
    Else
        MsgBox "SQL statement is empty.", vbExclamation, "Error"
    End If
End Sub
Kevin Robertson  @Reply  
          
2 years ago
You won't be able to set the Record Source of your Report from a button on your Form.
Use the Open event of the Report.

For Example:
    Me.RecordSource = Forms!FormName!SQLData

Substitute FormName for the name of your form

I would also add a check to make sure the Form is open. You can use Richard's IsLoaded function for this.

Button Code Example:
    Private Sub NZSelBtn_Click()

        Dim strSQL As String
    
        If IsNull(ID) Then Exit Sub
        strSQL = Nz(DLookup("SQLData", "NZMultiReport_T", "ID=" & ID),"")
    
        If strSQL <> "" Then
            Me.refresh
            DoCmd.OpenReport "NZxxx_R", acViewPreview
        Else
            MsgBox "SQL statement is empty.", vbExclamation, "Error"
        End If

    End Sub
William Kennedy OP  @Reply  
    
2 years ago
Just to be sure Kevin.  In the Open Event for the Report I use  your example.  And the Command button the rest of the code.
Kevin Yip  @Reply  
     
2 years ago
Hi William, if your reports differ mainly in the criteria, you can use that criteria for the OpenReport command, without having to change the report's record source:

      DoCmd.OpenReport "MyReport", acViewPreview, , "[Field1]='Company A' And [Field2]=2023"

If your report has groupings and sortings, they determine the report's sort order, and therefore they ignore the ORDER BY clause of the SQL in the report's record source.  So you may omit writing ORDER BY altogether.
William Kennedy OP  @Reply  
    
2 years ago
Thank you Kevin.  That worked well.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 8:54:28 AM. PLT: 0s