Results 1 to 8 of 8
  1. #1
    gem1204 is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    May 2010
    Posts
    32

    Report based on Paramaterized query

    I need to open a report that is based on a parameterized query. The report can be opened from a variety of forms that users have to access to open the report. I know how to use the do command to open the report and pass a filter argument but that doesn’t work if the report is based on parameters. I don’t want to change the source of the parameters to a control on the form because this report can be accessed from several different forms and the query itself is used in several different reports showing different amounts of detail.

    I’ve always been taught that it’s better to open a report with the data you want to see rather than opening a report and then applying a filter after it is opened. Even if I wanted to, I’m not allowed to change the report or the query so I’m stuck with the parameterized query and making it work. The query is fairly complex with several tables one of which has over 200,000 rows.

    I can use this code to open a form based on the parameterized query but when I tried to use it on a report I got a message “This feature is only available in an ADP” and my tables are based on ODBC tables from a pervasive database and not an sql server ADP.

    This is my code I can use to open a form
    Code:
    Private Sub Form_Open(Cancel As Integer)
        'This doesn't work on a report
        Dim mydb As dao.Database
        Dim Myqdf As dao.QueryDef
        Set mydb = CodeDb
        Set Myqdf = mydb.QueryDefs("qryPODetails")
        Myqdf.Parameters("WhatPO").Value = "0008009"
        Set Me.Recordset = Myqdf.OpenRecordset
    End Sub
    Is there any way to open this report as it is without removing the parameters and using a filter argument instead?
    Last edited by June7; 03-26-2015 at 10:23 AM. Reason: correct typo

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    I've never seen this code before. Is this setting the RecordSource of form?

    I have used code to set the RecordSource of a report by passing the SQL string to the report through its OpenArgs. Example:


    DoCmd.OpenReport "SampleList", IIf(booView = True, acViewPreview, acViewNormal), , Replace(Me.Filter, "zPre2009SamplesView", "SampleList"), , Me.RecordSource


    Private Sub Report_Open(Cancel As Integer)
    'report is called by SampleManagement and zPre2009SamplesView forms to print list of samples
    'set RecordSource to pull data from appropriate tables
    Me.RecordSource = Nz(Me.OpenArgs, Me.RecordSource)
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    gem1204 is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    May 2010
    Posts
    32
    You can set the recordsource of a form in the open event to an sql statment that you write or you can create a record set based on the sql statement and then assign the record set to be the form recordset.

    You cannot assign a recordset to a report after it opens (open event) so I somehow have to pass the paramaters to the underlying query for the report

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    That's exactly what I do with the example code.

    I just don't see need for QueryDef just to set a form or report RecordSource property but I am not using ODBC connection.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    gem1204 is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    May 2010
    Posts
    32
    If I set the recordsource to the name of a paramaterized query when the form or reports opens up the user is prompted to enter the values for parameters in the parameters query. I want to pass the criteria to the report before it opens so the user doesn't have to deal with the default pop-ups that ask for criteria. There's no way for any kind of error protection if the user enters invalid data or the wrong datatype.
    I would like to open the report just like I did in the docmd. Docmd.openreport "Report name", acViewReport,,,Criteria. The criteria argument for the DoCmd command doesn't work for paramaters.
    The code I entered in my first post works for a form but doesn't work for a report. I would like to be able to do the same thing for the report that I did for form.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Since I never use dynamic parameterized queries, not sure I can help. Sorry.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    gem1204 is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    May 2010
    Posts
    32
    ...June 7 .... you said what I do is exactly what I do. Did you use a parameterized query? If you did and you're using it on a form, then try the same code in the open even of a report and see if it works. It probably shouldn't

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    I do not use dynamic parameterized queries. I apply filter to forms and reports with the WHERE CONDITION argument of OpenReport method.

    The code I posted uses the filter criteria of the form and applies it to the report in the OpenReport method and also passes the form's recordset SQL to the report with OpenArgs.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 1
    Last Post: 06-03-2014, 06:56 AM
  2. Replies: 3
    Last Post: 03-11-2013, 05:11 PM
  3. report based on a query
    By blueraincoat in forum Reports
    Replies: 4
    Last Post: 03-27-2011, 03:31 AM
  4. Report based on query + sub-report (1:N relation)
    By BayerMeister in forum Reports
    Replies: 1
    Last Post: 08-19-2010, 03:26 AM
  5. Replies: 1
    Last Post: 02-02-2009, 05:52 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