Results 1 to 8 of 8
  1. #1
    dougie is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2011
    Posts
    27

    One selection clause for many queries

    I have several queries (crosstabs) which are eventually combined together to form the data for a single report. If I want to date select for this report I need to add a WHERE clause to every query via VBA. This isn't difficult but it set me wondering if there was any way to have one dummy query containing the necessary WHERE statement, and this somehow gets appended to all the other queries, i.e. to filter on date I only need a change in one place. Any ideas?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    See Martin Green's tutorials where he introduces and leads you through some interesting Sql and vba examples. There are a number of them, but they start here
    http://www.fontstuff.com/access/acctut14.htm

  3. #3
    dougie is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2011
    Posts
    27
    Thanks, a good resource, but a quick read through didn't find what I want. I need something like:

    SELECT * FROM <tablename> WHERE (SELECT Criterion condition from somewhere else)

    The subquery is appended to every query but it's actual value is stored and controlled in one place, e'g' a field in a table. For example, I create a table called refTable containing one text field called 'condition' and which has (say) this value "month(recorddate)=6" and then write:

    SELECT * FROM <tablename> WHERE (SELECT [condition] from refTable)

    ...but sadly this dosn't work.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    This is along the line of the use of vba to create the sql you want to use.
    I have a table HIRES with fields
    name text
    hiredate date/time

    My RefTable has fields
    id autonumber
    RefClause text

    Sub reftest()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim rs1 As DAO.Recordset
    Dim sql As String 'Base sql statement
    sql = "SELECT * FROM HIRES WHERE "
    Set db = CurrentDb
    Set rs = db.OpenRecordset("select refclause from refTable where id = 1")
    rs.MoveFirst 'only get 1 record back
    sql = sql & rs!refclause 'append the where condition to the base sql
    Debug.Print sql
    End Sub

  5. #5
    dougie is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2011
    Posts
    27
    Thanks - excellent.

  6. #6
    dougie is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2011
    Posts
    27

    On second thoughts...

    No - of course it's easy to write VBA and complete the SQL string - sorry if I didn't make myself clear. What I want is the SQL view of the query to show 'blah blah blah WHERE ' & <condition pulled from somewhere>. This query never gets edited, only the 'somewhere' it pulls its WHERE clause from.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    This query never gets edited, only the 'somewhere' it pulls its WHERE clause from.
    How does one pull in the where clause and put it together with the base sql and NOT EDIT the query? Perhaps you should tell us your definition of edit.

    In the example I posted to show a concept, you could have a number of "where clauses", but, somehow, you have to know which one to apply whenever you want to use it in the query.

    Going back to your statement that I quoted above, you would need to know which where clause to apply; get it from somewhere and apply it somehow - in my view.

    I'd like to see a working example of what you are conceiving in the"one selection clause many queries".

  8. #8
    dougie is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2011
    Posts
    27

    Clarification

    >Perhaps you should tell us your definition of edit

    You'll get a definition of 'edit' here - my apologies if English isn't your first language.

    Look at my second post, which uses a subquery to supply a 'WHERE' clause. It *almost* works, and I've used db languages (e.g. Clipper) where such a thing would work (blocks of code may be held in variables and compiled on the fly). To be a bit more explicit, I want to write this:

    SELECT * FROM <tablename1> WHERE (SELECT [fieldname] from <tablename2>)

    [fieldname] in tablename2 contains some text such as '[area]="north"'

    Tantalisingly, a statement like:

    SELECT * FROM <tablename1> WHERE [area]=(SELECT [fieldname] from <tablename2>)

    ...and fieldname contains "north", does work.

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

Similar Threads

  1. Not In Clause
    By dukect in forum Queries
    Replies: 10
    Last Post: 08-29-2011, 04:55 PM
  2. Using the TOP clause
    By WSlepecki in forum Queries
    Replies: 1
    Last Post: 04-08-2011, 06:59 AM
  3. Where clause
    By Amerigo in forum Queries
    Replies: 2
    Last Post: 03-30-2011, 07:34 AM
  4. IIF clause
    By Peljo in forum Queries
    Replies: 2
    Last Post: 02-05-2008, 11:22 AM
  5. Help on WHERE clause
    By QBCM in forum Programming
    Replies: 1
    Last Post: 12-19-2005, 08:43 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