Results 1 to 2 of 2
  1. #1
    ksamaniego is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    4

    Subquery and IIF statement

    I want to have a subquery inputted into my main query based on if a check box has been checked or not...



    How would I go about accomplishing this?

    -Kyle S.

  2. #2
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Let's say your query was:
    SELECT * FROM tblTableA WHERE WidgetID In (SELECT WidgetID FROM tblTableB)
    with the In() portion being the optional. What I would do is the following in the event procedure of whatever triggers the query.

    Dim strSQL as String

    strMain = "SELECT * FROM tblTableA WHERE "

    If chkBox = True Then
    strSQL = strSQL & "WidgetID In (SELECT WidgetID FROM tblTableB)"
    Else
    strSQL = "1=1"
    End If

    DoCmd.RunSQL (strSQL)

    End Sub

    So basically you are building your where clause depending on what the checkbox is. I'm sure your query is more complicated but the concept should still work. If you find yourself in a situation with no where clause, simply use 1=1, as above, to fake one.

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

Similar Threads

  1. Query with maybe subquery
    By AndycompanyZ in forum Queries
    Replies: 4
    Last Post: 06-28-2011, 11:45 AM
  2. Subquery sum?
    By anemoskkk in forum Access
    Replies: 0
    Last Post: 04-29-2011, 12:36 PM
  3. SQL SubQuery Does Not Work
    By saascuba in forum Access
    Replies: 3
    Last Post: 11-04-2010, 01:59 PM
  4. Subquery
    By combine21 in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 04:33 PM
  5. Replies: 4
    Last Post: 09-03-2010, 02:55 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