Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513

    query to satisfy condition in multiple tables confusion....

    I think I'm totally lost...

    I have a code that creates a query filtered on the values of one table, and it's fine (works exactly as I wanted);
    then I needed to expand it to include records in a second related table (that's where I got all twisted)

    the GOAL IS:
    to show all the records in table [tblManufacturer] where either
    in that table [tblManufacturer], the field [PrimaryCatagoy] meets the criteria in [strIN]
    or
    in a second table [tblMfrCatagory] (related to [tblManufacturer] by the field [ManufacturerName]), the field [AddlCatagories] meets the same criteria


    ' this alone executed correctly:


    ' strSQL = strSQL & "WHERE tblManufacturers.PrimaryCatagory IN(" & Left(strIN, Len(strIN) - 1) & ")"

    'this does not execute at all:
    strSQL = strSQL & "WHERE (tblManufacturers.PrimaryCatagory IN(" & Left(strIN, Len(strIN) - 1) & "))" _
    or _
    (tblMfrCatagory.AddlCatagories IN(" & Left(strIN, Len(strIN) - 1) & "));"
    End If

    'Clear listbox selection after running query"
    For Each varItem In Me.lstCatagorySelect.ItemsSelected
    Me.lstCatagorySelect.Selected(varItem) = False
    Next varItem
    Me.Label27.Caption = "Currently filtered to include: " & strLabel
    Me.Label27.Visible = True

    Forms!frmManufactures.RecordSource = strSQL
    MyDB.QueryDefs.Delete "qryCatagories"
    Set qdef = MyDB.CreateQueryDef("qryCatagories", strSQL)



    greatly appreciated in advance,
    Mark

  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,625
    How do you build the strIN criteria? The IN operation won't work if strIN is just a single string of characters, not a set of discrete values. Don't see how it could have worked correctly at all.

    In("France", "China", "Germany", "Japan") is not the same as In("France, China, Germany, Japan")

    The first is easy to do in Access query object with literal text, not so easy to build in VBA. I've tried.
    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
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513
    ' Build string by looping through the listbox
    strIN = ""
    For i = 0 To lstCatagorySelect.ListCount - 1
    If lstCatagorySelect.Selected(i) Then
    strIN = strIN & "'" & lstCatagorySelect.Column(0, i) & "', "
    End If
    Next i

    'Create the WHERE string, and strip off the last comma of the IN string

  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,625
    Ah Ha! Had to use DAO recordset. Wow, works, now I should never forget.

    Need to trim a comma and space so:

    Left(strIN, Len(strIN) - 2)
    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
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513
    Not sure that will get me where I'm trying to go (out of office and on cell at the moment, and unable to run the code.... )
    If I recall correctly, the problem gets flagged while I'm writing the code at the second "IN"
    ....tblMfrCatagory.addlCatagories IN("

    Could I bother you to post the entire line as you see it ought to be written?

  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,625
    After looping through the listbox:

    strSQL = strSQL & "WHERE tblManufacturers.PrimaryCatagory IN(" & Left(strIN, Len(strIN) - 2) & ")" _

    Do you really have the field named as PrimaryCatagory? That is a misspelling of Category.

    I assume the strSQL has a space built in to precede the "WHERE..." and a mate to the last paren.
    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.

  7. #7
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    513
    Yup
    Catagory
    Things you inherit...

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

Similar Threads

  1. Replies: 1
    Last Post: 04-05-2013, 12:49 PM
  2. Replies: 17
    Last Post: 11-13-2012, 03:28 AM
  3. Replies: 2
    Last Post: 04-04-2012, 03:52 AM
  4. Confusion with table relations
    By BusDriver3 in forum Database Design
    Replies: 13
    Last Post: 08-21-2011, 01:48 PM
  5. Replies: 5
    Last Post: 06-19-2010, 07: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