+ Reply to Thread
Results 1 to 9 of 9

Filter main table by data contained in related table.

Hybrid View

  1. #1
    Registered User
    Join Date
    12-11-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    9

    Filter main table by data contained in related table.

    Hi,

    I am working on a simple database based on football just to get some experience.

    I have a 'Main' table that contains the main information for the game [Game ID](Primary key), [Game Date], [Opponent], [Result] etc.

    I also have another table 'Players' related by [Game ID] to 'Main' that contains the player names for each game.

    My 'Main' form is continuous giving me a list of all the games and I can click on one of the records which will open a 'Details' form containing the 'Players' subform so I can see and enter the players for that particular game. (Each player is a new record in the subform, usually 11 records per Game ID ).


    In the header of the 'Main' continuous form I have 4 combo boxes (filter1, filter2....) that can filter the records by fields contained within the 'Main' table.

    This is the code in the filter button:

    Private Sub Command149_Click()
    Dim varFilter As Variant
    varFilter = Null
    
    If Not IsNull(Me.filter1) Then
    varFilter = (varFilter + " AND ") _
    & "[seasonname]= '" & Me.filter1 & "'"
    End If
    
    If Not IsNull(Me.filter2) Then
    varFilter = (varFilter + " AND ") _
    & "[opponent]= '" & Me.filter2 & "'"
    End If
    
    If Not IsNull(Me.filter3) Then
    varFilter = (varFilter + " AND ") _
    & "[competition]= '" & Me.filter3 & "'"
    End If
    
    If Not IsNull(Me.Filter4) Then
    varFilter = (varFilter + " AND ") _
    & "[Result]= '" & Me.Filter4 & "'"
    End If
    
    With Me
    If Not IsNull(varFilter) Then
    .Filter = varFilter
    .FilterOn = True
    Else
    .FilterOn = False
    End If
    .Requery
    End With
    
    End Sub

    It works perfectly for filtering data contained within the 'main' table but I would like to add another filter that will show records where a certain players name is contained in the related table for those games.

    i.e. only show records/games where Joe Bloggs was in the team lineup.

    I have tried creating a query that contains both the 'main' table and the 'players' table (Inner join) it works in that it will filter only those records containing Joe Bloggs but when the filter is clear it gives 11 entries per game e.g.

    Game ID: 1 Date: 10/8/2010 Player: Joe Blogs
    Game ID: 1 Date: 10/8/2010 Player: Dave Smith
    .
    .
    .
    Game ID: 2 Date: 17/8/2010 Player: Joe Blogs
    Game ID: 2 Date: 17/8/2010 Player: Dave Smith

    but I only want to see one record per game ID.


    Any ideas please?
    Last edited by Cutter; 09-08-2012 at 08:21 AM. Reason: Added code tags

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Filter main table by data contained in related table.

    I reckon you probably just need to make your query a SELECT DISTINCT query instead of a plain SELECT query.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    12-11-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Filter main table by data contained in related table.

    Thank you Joseph for the quick response, you have helped me get closer.

    I have been playing with the query; In the column for 'players' I added a WHERE clause and linked it to my filter on the form. It does the job of filtering to the players name like I wanted but when there is no value in the filter (all filters cleared) it omits all the GAME ID's that do not have any player records associated e.g. the games yet to be played or where records have not been added yet. Following this I have tried to change the inner join to Left outer and Right outerjoins but with no luck, they still omit the records.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Filter main table by data contained in related table.

    hard to be picture what you have but you may just need to reverse the where clause so you use
    where [form control] = fieldname or is null

  5. #5
    Registered User
    Join Date
    12-11-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Filter main table by data contained in related table.

    Hi, I'm still lost with this...

    I'll try to give you a picture:
    query1.png

    The above will not return any records if the filter control is null but will work fine if the filter has a selection.

    If I clear the Player query criteria it outputs all the records as needed, one for each game ID:

    queryoutblank.png

    So I think I need a way of doing: If the filter control is null then run the query without the Player criteria, if not null then run the query with the Player criteria. I hope I'm making some sense.
    Attached Images Attached Images

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Filter main table by data contained in related table.

    try moving the [forms].[fixtures].[filter7] to the field area in place of players and then in the criteria use
    players.player or is null

  7. #7
    Registered User
    Join Date
    12-11-2010
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Filter main table by data contained in related table.

    It works now.

    queryout.png

    You sir are a god, thank you very much.

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Filter main table by data contained in related table.

    you're welcome :-)

  9. #9
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Filter main table by data contained in related table.

    @ Dooey123

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as an infrequent user of the forum, you may have forgotten or may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

    Finally, please notice that code tags have been added to your post(s). The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at top of the page and read Rule #3.
    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1