Results 1 to 5 of 5
  1. #1
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328

    Set Query criteria with vba

    I am using A2007 and A2003.



    I have two reports that could use the same source query. However, the query criteria required for each report is different. I would like to change the criteria with vba and use only one query..

    Say that the source Query1, as it stands has the criteria Field_X=-1 and field_Y=null. This is ok for report_1. When I open Report_2, I would like to reverse the criteria (x=null and Y=-1). Of course, opposite action when open the first report. If doable, what would the code look like?

    I looked up and tried two different solutions on various help sites and neither worked. Possible I am not using them correctly. e.g., one used a term "qdf" as if it were some standard function. My Access didn't recognize it.

    Any help much appreciated.

  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,771
    Alternative:

    Don't put parameters in query.

    Instead of query object as report RecordSource, can build the SQL directly in the RecordSource property.

    VBA to pass filter criteria to the report. Whether the RecordSource is query object or SQL statement, can apply this approach.

    If this condition met Then
    DoCmd.OpenReport "report1 name", , , "fieldX=-1 And fieldY Is Null"
    Else
    DoCmd.OpenReport "report2 name", , , "fieldX Is Null And FieldY=-1"
    End If

    Note the use of Is Null - cannot =Null, nothing is ever =Null. http://allenbrowne.com/casu-12.html
    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
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    Thanks for quick reply. I didn't give you whole story in posting, trying to keep it simple to write up. A big problem with this system is that the user has a very slow server link. I am always redesigning for speed. I have read somewhere that keeping queries small helps speed, so hesitate to have query with no criteria..

    One of the reports is a sub report. I can handle that with on-load. The other report includes code to change a column to a csv row, opening the report with a control to hold the csv row. I changed query criteria fieldX=-1 OR fieldY=-1, then modified CSV creation code to convert only where fieldY=1.

    Works, but for some reason the query slowed way down when I added the OR function. Both fields are indexed. I will try to find out why

  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,771
    Did you try the approach of building the SQL directly in the report RecordSourse? You can have the criteria in that statement then don't need the WHERE CONDITION argument in VBA and the query then has filter.
    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
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    I had not tried it. I still have two queries, but I don't have to look at them and wonder why whenever I go back and modify the system. Speed is not compromised either. Like a lot of these things, I smack my head when I get the answer. Sometimes, just get too focused on a chosen path.

    Thanks again.

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

Similar Threads

  1. Replies: 4
    Last Post: 05-02-2013, 11:07 AM
  2. Replies: 5
    Last Post: 05-01-2013, 11:39 AM
  3. Replies: 5
    Last Post: 09-20-2012, 03:27 PM
  4. Replies: 5
    Last Post: 08-02-2012, 09:44 AM
  5. Replies: 1
    Last Post: 07-13-2011, 11:00 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