Results 1 to 6 of 6
  1. #1
    bbxrider is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Posts
    69

    how to run a sql string built in form in a query

    from this post, http://access.mvps.org/access/forms/frm0007.htm
    I can build the sql string with the selected items from the listbox right in the form that has the listbox
    but not sure how to pass that select statement into a query which will be record source for a report


    I know I could have a hidden text box and populate the textbox with the sql string and then reference the textbox content in a say a query column criteria row but what do I put in the top box of the query column?
    or just how to get that sql string as the sql string for a query.

  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,770
    I suggest the easiest approach is to apply the filter string constructed by the looping on listbox to report or form when opening.

    DoCmd.OpenReport "report name", , , strWHERE
    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
    bbxrider is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Posts
    69
    ok so don't bother with the query, just go right to the report, yes? In your example the strWhere last parameter of invoking the report is essentially the same as 'building' the sql as the 'record source' of the report, yes?

  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,770
    In my suggestion, the only part of the sql that is built is the WHERE clause, only without the WHERE keyword. It does not alter the report RecordSource, it sets report 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
    bbxrider is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Posts
    69
    ? not following your reply, I am building a complete sql query 'where' clause in a form where the 'or' criteria are the items selected in a listbox, "select * from myTable m where m.someField = 'listboxItem1' or m.someField = 'listboxItem2' "etc
    that sql query will be the record source for a report, I thought I would have to 'run' the sql in a query that is the record source for the report. I was asking how to 'pass' the sql statement to a access query which would run it. you have lost me now talking about the sql without the 'where' as a filter in the report. why would there be a need for filter?

  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,770
    Filter criteria is what you are building with the code that loops through the listbox.

    You can try the code referenced in the link you posted but I see a flaw in that code. It first sets the variable strSQL to the text "SELECT * FROM Employees where [EmpID]=" then in the loop it concatenates that variable to each value in the listbox. This means with each iteration of the loop the text "SELECT * FROM Employees where [EmpID]=" will be repeated and the result will be a bad SQL statement.

    Try it, see what the constructed value looks like by printing it to the VBA immediate window: Debug.Print strSQL

    Correcting that code would mean putting the loop first to build the criteria then outside the loop concatenate once to the end of "SELECT * FROM Employees where [EmpID]="

    My suggestion just builds the WHERE clause of an SQL statement. If the fields of the report don't need to change why bother programmatically modifying the RecordSource? Just apply filter to the report.

    I never use dynamic parameterized queries as a RecordSource, only static queries.

    So with your example, the report RecordSource would be: SELECT * FROM myTable;

    Then the code would build the WHERE criteria and pass to the report when it opens with code in post 2.

    I think use of IN operator is a better option with multiple parameters for same field. Review: http://www.allenbrowne.com/ser-50.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.

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

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2014, 07:59 PM
  2. report built on query
    By Daryl2106 in forum Access
    Replies: 4
    Last Post: 12-09-2012, 07:07 PM
  3. Text String from Form Query - Multiple Choices
    By wrandyrice in forum Access
    Replies: 1
    Last Post: 08-12-2012, 10:58 AM
  4. Replies: 4
    Last Post: 11-16-2011, 04:46 PM
  5. Query built in VBA
    By doci4a in forum Programming
    Replies: 1
    Last Post: 03-16-2011, 01:51 PM

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