Results 1 to 11 of 11
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237

    Criteria to open a subreport


    Hi to all,

    A command button on a form opens a main report with a subreports.

    Code:
    DoCmd.OpenReport "rptReportName", view:=acViewPreview, wherecondition:=strCriteria
    This main report has a subreport with record source (qryNameOfQuery).
    I need to open the subreport with condition that one of the field values is False (MarkValue = False). What code should I put in the on Load Event of the subreport?


    Khalil

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    IIRC, you cannot apply a filter to a subreport using the load or open events. If your T/F field isn't or cannot be on the main report also (thus allowing you to use link master & child field properties) then I'd say you must either
    - modify the querydef object for the sub report query and then open the main report. The sub report query will then be filtered accordingly. Or
    - construct the sub report sql in code and try setting the sub report record source property to that. Or
    - you could have a control (e.g. button) in main form header that will apply your filter
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Thank you for the reply,
    My field is not on the main form. I will try the options suggested.

  4. #4
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi,

    I have the following partial code:

    Code:
    Dim db As dao.Database
    Dim strSQL As String
    
    Set db = CurrentDb
     strSQL = "Select * from qryQueryNameSubReport WHERE MarkDeleted = -1"
    
    rptMAinReportName.cntContainerName.Report.RecordSource = strSQL      
    DoCmd.OpenReport "rptReportName", view:=acViewPreview, wherecondition:=strCriteriaDim db As dao.Database
    Dim strSQL As String
    
    Set db = CurrentDb
     strSQL = "Select * from qryQueryNameSubReport WHERE MarkDeleted = -1"
    
    rptSubReportReportName.cntCollected.Report.RecordSource = strSQL      
    DoCmd.OpenReport "rptTReportName", view:=acViewPreview, wherecondition:=strCriteria
    I am receiving the following error
    Error 91: Object variable or With block variable not set

    Any ideas?

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    If we knew what line, that would help
    Why is the code repeated?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    The repetition of the code is by mistake.
    The error is in the following line:
    Code:
    rptMainReportName.cntCollected.Report.RecordSource = strSQL
    The above code is supposed to determine the Record Source of the Sub Report. I am using the name of the Container and not the name of the Sub Report.

    Khalil

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    Quote Originally Posted by Khalil Handal View Post
    Hi,
    The repetition of the code is by mistake.
    The error is in the following line:
    Code:
    rptMainReportName.cntCollected.Report.RecordSource = strSQL
    The above code is supposed to determine the Record Source of the Sub Report. I am using the name of the Container and not the name of the Sub Report.

    Khalil
    And the main report is rptMainReportName , literally ?
    If that is the object holding the main report, where is that set?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I would say it's because if you're going to refer to the report by name that you must begin with the collection it belongs to - as in Forms! or Reports! in this case. I'm not positive as I've never coded that way. If I don't use Reports! or Forms! I use Me.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    I think I don't know where to put the SET, And how to select the subReport control source.

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I'm saying not rptSubReportReportName.cntCollected.Report.RecordS ource = strSQL
    but
    Me.cntCollected.Report.RecordSource = strSQL
    or
    Reports!cntCollected.Report.RecordSource = strSQL

    I don't see why you're setting anything because you don't need it based on what you posted. If you think you do and need assistance, post the whole procedure.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    Thank you for the suggestions.
    I was able to do it by: modifying the querydef object for the sub report query and then open the main report.
    Thank you again
    Khalil

Please reply to this thread with any new information or opinions.

Similar Threads

  1. vba syntax to open form from a subReport
    By mainerain in forum Programming
    Replies: 5
    Last Post: 05-13-2021, 07:38 AM
  2. Subreport criteria not working
    By Flyboy1945 in forum Reports
    Replies: 5
    Last Post: 11-03-2016, 02:22 PM
  3. Subreport with criteria from main report
    By Evilferret in forum Reports
    Replies: 1
    Last Post: 08-15-2012, 03:19 PM
  4. Criteria for Subreport
    By blazerboy6 in forum Reports
    Replies: 4
    Last Post: 08-09-2011, 12:46 PM
  5. Multi Based Criteria for SubReport
    By lostfan789 in forum Reports
    Replies: 1
    Last Post: 05-22-2010, 08:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums