Change Query SQL in VBA

Posted on 2005-04-25
Last Modified: 2012-06-21
I am using a subreport to display a list of all tasks based on a project ID. I would like to use a list box to allow show selected tasks (based on a field in the list) by using .requery

I would like to change the query that is the source of the subreport.

subreport name is "subProjectTasks"
query name is "ProjectTasks Query"

Any help, as always, is much appreciated.

Question by:jberv534
    LVL 17

    Expert Comment

    In the report's 'On Activate' event try:

    [subProjectTasks].[Report].RecordSource = "ProjectTasks Query"
    Me.[subProjectTasks].[Report].RecordSource = "ProjectTasks Query"

    Not sure if you really need the Me. but it doesn't hurt either. :-)

    Author Comment

    Arji - thanks for the reponse. I did not make myself clear about something:

    I want to change the sql statement that ProjectTasks Query uses by adding a WHERE statement.

    LVL 17

    Expert Comment

    The Column(0) assumes that in your list, column 0 is the project ID.  When you set a RecordSource, it works the same as a requery.

    [subProjectTasks].[Report].RecordSource = "select * from ProjectTasks where [project ID]=" & [YourList].Column(0)

    Also you can open a report with the criteria in the DoCmd method.  Assuming your report's record source is "ProjectTasks":

    DoCmd.OpenReport YourReport, acViewPreview, , "[project ID]=" & [YourList].Column(0)

    I think this method is better in that you can set criteria dynamically when you open the report

    Hope I understood what you wanted....
    LVL 17

    Accepted Solution

    Actually for the second method, I'm assuming that you are linking your report's subreport to the main report using the ProjectID as the Master and Child link fields(see subreport properties).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now