Results 1 to 4 of 4
  1. #1
    enjiel is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    11

    Dynamic Report Record Source

    Reading through the various forums, this seems to be a pretty common question. In some form or another, people are wondering how to make their subreport's recordsource dynamic. I've lose count of the different ways that I've tried and the issue, I think, is that my main report is not a parent of the subreports that are called, in fact, they are independent of the main report.



    The report is for planning city projects. The main report prints everything from a table (Project) and at the bottom of the report I have 3 subreports that query from other tables (Equipment, Labor, Materials).

    Obviously everything displayed in the reports depends on the name of the project that was selected before the report is generated, which means that the recordsource needs to be dynamic.

    Code:
    Private Sub Report_Load()
    Dim WorkOrder As String
    WorkOrder = Forms!Project.WorkOrderCombo.Column(0)  //get the name of the project (or workorder) from the main form
    SQL = "SELECT * FROM EquipJoin WHERE [ID] LIKE '" & WorkOrder & "*';"  //build a query to find all equipment that is used in this workorder
    Me.RecordSource = SQL
    End Sub
    To note: The architecture of the database is as so:
    1 table to list projects
    1 table to list equipment
    1 table to join projects and equipment
    a table for materials (and a table to join mat/projects)
    same for labor

    The IDs for the Join tables is built as so: WorkOrderName:EquipmentID#
    ex. project1:00012
    with a semicolon to separate the values. This way I can search all equipment, for instance, with LIKE *

    In any case, I can get the report to run but the subreports won't populate until I actually click on the subreports themselves.
    The best answer that I've found, so far, (I suppose) is that I'm going about it the wrong way. Usually, when I'm stuck like this that is the case, but I can't think of another way to accomplish this.
    I'd like the data for the subreports to populate without need for the user to click on each subreport.

    Click image for larger version. 

Name:	report.png 
Views:	12 
Size:	70.0 KB 
ID:	20141

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    The subform should populate automatically IF you have the sub form bound by the query and the linking values to the master form,
    Is this true?

    if so, you could try the ON CURRENT event in the master form to refresh the sub form.

  3. #3
    enjiel is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    11
    Quote Originally Posted by ranman256 View Post
    The subform should populate automatically IF you have the sub form bound by the query and the linking values to the master form,
    Is this true?

    if so, you could try the ON CURRENT event in the master form to refresh the sub form.
    That's the problem. I don't know how to bind it to a query that has to be generated through vba (because an element of the query is a variable obtained from another form).
    Also, there is nothing to link the subforms and the form that they load in. The primary form loads all overarching project data from the project table. The subforms load all data concerning that project using conjunction tables. The project ID that is the PK in project table is used to generate the PK for the junction tables but there are no direct relations otherwise.

    The record sources on all reports are generated by VBA on load event.

    For the main report:

    Code:
    Private Sub Report_Load()
    Dim WorkOrder As String
    WorkOrder = Forms!Project.WorkOrderCombo
    Me.RecordSource = "SELECT * FROM Project Where [WorkOrder] = '" & WorkOrder & "';"
    Me.WorkOrder = WorkOrder
    Me.TCostEst = Forms!Project.TCostEst
    Me.StopDate = Forms!Project.StopDate
    Report.Requery
    End Sub
    For the sub reports:
    Code:
    Private Sub Report_Load()
    Dim WorkOrder As String
    WorkOrder = Forms!Project.WorkOrderCombo.Column(0)
    SQL = "SELECT * FROM MatJoin WHERE [ID] LIKE '" & WorkOrder & "*';"
    Me.RecordSource = SQL
    End Sub

  4. #4
    enjiel is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    11
    I see what you mean.
    This fixed the problem:

    Code:
    Private Sub Report_Current()
    Me.[EquipJoin SubReport].Requery
    Me.[LabJoin SubReport].Requery
    Me.[MatJoin SubReport].Requery
    End Sub

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

Similar Threads

  1. Replies: 2
    Last Post: 01-15-2015, 01:10 AM
  2. Report with no Record Source
    By NISMOJim in forum Reports
    Replies: 13
    Last Post: 07-19-2014, 02:29 AM
  3. Getting to SQL in report record source
    By Monterey_Manzer in forum Reports
    Replies: 3
    Last Post: 12-04-2012, 01:44 PM
  4. Replies: 8
    Last Post: 07-30-2012, 04:43 PM
  5. How to modify sub report record source
    By EddieN1 in forum Reports
    Replies: 4
    Last Post: 12-12-2011, 06:57 AM

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