Results 1 to 5 of 5
  1. #1
    drhansenjr is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    14

    SQL Query if ID in ANY of 4 fields

    I have a Collections table that includes four member ID fields and a collection ID field, and a CollectionTypes table with type ID and type name field, both of which are included in a query. I need to pass a member ID to the query and get back a recordset with Collections table ID and collection names where that member ID appears in ANY of the four member ID fields. After fetching that collection recordset, I use the member IDs it returns to get the member's full name using another query (which works fine). I will use the results to populate a listbox. My problem is that when I run the first query, it returns a recordset with a row for each record in the CollectionTypes (and the same four member IDs in each. I know I could accomplish this with four queries and then combine the results, but hope there is a way to do it in one.




    Here is my query code...


    Code:
    PARAMETERS [lngCurrentMemberID] Long;
    SELECT tblCollections.ID AS COLLID, tblCollections.strPrefix, tblCollections.lngMember1ID,
    tblCollections.lngMember2ID, tblCollections.lngMember3ID, tblCollections.lngMember4ID, 
    tblCollectionTypes.txtCollectionTypeName AS TYPENAME,tblCollectionTypes.ID, 
    tblCollectionTypes.txtCollectionTypeName
    
    FROM tblCollections, tblCollectionTypes
    
    WHERE [lngCurrentMemberID] IN (tblCollections.lngMember1ID, tblCollections.lngMember2ID, 
    tblCollections.lngMember3ID, tblCollections.lngMember4ID);
    ...and the corresponding VB:


    Code:
        Set qdfGetCurrentMemberCollections = currDB.QueryDefs("qryGetCurrentMemberCollections")
        qdfGetCurrentMemberCollections.Parameters!lngCurrentMemberID = Me.ID
        Set rstGetCurrentMemberCollections = qdfGetCurrentMemberCollections.OpenRecordset()
        If rstGetCurrentMemberCollections.RecordCount > 0 Then 'Iterate through recordset
            If Not (rstGetCurrentMemberCollections.EOF And rstGetCurrentMemberCollections.BOF) Then
                rstGetCurrentMemberCollections.MoveFirst
                Do Until rstGetCurrentMemberCollections.EOF
                    lngCollectionMember1ID = 0
                    lngCollectionMember2ID = 0
                    lngCollectionMember3ID = 0
                    lngCollectionMember4ID = 0
                    strMember1Fullname = ""
                    strMember2Fullname = ""
                    strMember3Fullname = ""
                    strMember4Fullname = ""
                    strCollectionTypeName = ""
                    strCollectionItemPrefix = ""
    
    
                    lngCollectionID = rstGetCurrentMemberCollections!COLLID
                    strCollectionTypeName = rstGetCurrentMemberCollections!txtCollectionTypeName
    
    
                    If IsNull(rstGetCurrentMemberCollections!strPrefix) Then
                        strCollectionItemPrefix = "UNDEFINED"
                    Else
                        strCollectionItemPrefix = rstGetCurrentMemberCollections!strPrefix
                    End If
    
    
                    If IsNull(rstGetCurrentMemberCollections!lngMember1ID) Then
                        strMember1Fullname = "-"
                    Else
                        strMember1Fullname = strGetNameByID(rstGetCurrentMemberCollections!lngMember1ID)
                    End If
    
    
                    If IsNull(rstGetCurrentMemberCollections!lngMember2ID) Then
                        strMember2Fullname = "-"
                    Else
                        strMember2Fullname = strGetNameByID(rstGetCurrentMemberCollections!lngMember2ID)
                    End If
                    
                    If IsNull(rstGetCurrentMemberCollections!lngMember3ID) Then
                        strMember3Fullname = "-"
                    Else
                        strMember3Fullname = strGetNameByID(rstGetCurrentMemberCollections!lngMember3ID)
                    End If
                    
                    If IsNull(rstGetCurrentMemberCollections!lngMember4ID) Then
                        strMember4Fullname = "-"
                    Else
                        strMember4Fullname = strGetNameByID(rstGetCurrentMemberCollections!lngMember4ID)
                    End If
    
    
                    lbAssociatedCollections.AddItem CStr(lngCollectionID) & ";" & strCollectionItemPrefix & ";" & _
                        strCollectionTypeName & ";" & strMember1Fullname & ";" & strMember2Fullname & ";" & _
                        strMember3Fullname & ";" & strMember4Fullname
    
    
                    rstGetCurrentMemberCollections.MoveNext
                Loop
            End If
        End If

    Click image for larger version. 

Name:	queryquestion.jpg 
Views:	30 
Size:	211.9 KB 
ID:	45078



    Ideas???

  2. #2
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The main problem is that the table design in not normalized. The Collections Table looks like an Excel spreadsheet design. In an Access table, you would have one field for the member ID number (lngMemberID).
    What would you have to do if there is another member ID you wanted to add? You would have to change the table design, the queries, the forms, the reports and possible the code.


    You really need to fix your design before you move on. If you don't, you will probably end up bald from pulling your hair out, trying to make things work. (Ask me how I know )


    Consider:

    tblCollections
    -------------
    CollectionID_PK (Autonumber)
    lngMemberID_FK (Long Integer)
    lngCollectionTypeID_FK (Long Integer)
    strPrefix (Short Text)




    tblCollectionTypes
    ------------------
    CollectionTypeID_PK (Autonumber)
    txtCollectionTypeName (Short Text)



    tblMembers
    -------------
    MemberID_PK (Autonumber)
    MemberFirstName (Short Text)
    MemberLastName (Short Text)

  3. #3
    drhansenjr is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    14
    4 members will never be exceeded. And I solved the problem with a simple LEFT JOIN (but thanks!)

    Code:
    PARAMETERS [lngCurrentMemberID] Long;
    SELECT tblCollections.ID AS COLLID, tblCollections.strPrefix, tblCollections.lngMember1ID, tblCollections.lngMember2ID, tblCollections.lngMember3ID, tblCollections.lngMember4ID, tblCollectionTypes.txtCollectionTypeName AS TYPENAME,tblCollectionTypes.ID, tblCollectionTypes.txtCollectionTypeName
    FROM tblCollections LEFT JOIN tblCollectionTypes
    ON tblCollections.lngCollectionType = tblCollectionTypes.ID
    WHERE [lngCurrentMemberID] IN (tblCollections.lngMember1ID, tblCollections.lngMember2ID, tblCollections.lngMember3ID, tblCollections.lngMember4ID);

  4. #4
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,564
    I would recommend that you take note of Steve's comments regarding a record for each Member in a separate related table vice fields in the same table.

    If you do not correct the bad table design now you will hit further snags down the line.
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  5. #5
    drhansenjr is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    14
    This is a personal (family history data that will be used to populate a database-driven Wix website. It's a one-time-use and essentially a throw-away app that will go away once the initial data is collected and transferred elsewhere. If this were a serious, important business app I would heartily agree with you, but it's not. In 90% of the cases there will only be two members in collection. I added two more for a little wiggle-room. Members in excess of 4 related to a collection I don't care about and they can be ignored with no adverse effects. Sorry I didn't make this clear earlier.

    Thanks --

    DH

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

Similar Threads

  1. Replies: 6
    Last Post: 07-03-2018, 03:51 PM
  2. Replies: 2
    Last Post: 11-27-2017, 08:59 PM
  3. Replies: 4
    Last Post: 07-17-2013, 01:11 PM
  4. Replies: 4
    Last Post: 05-14-2012, 06:10 PM
  5. Replies: 12
    Last Post: 05-07-2012, 12:41 PM

Tags for this Thread

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