Results 1 to 7 of 7
  1. #1
    c.vaibhav is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    3

    Filtering a database based on instr rules

    Hi All,

    I am absolutely new to MS access.

    I have 2 database. The first database has 20 columns and 40000 rows. The first database looks like:

    Region Deals
    US 8855
    APAC 7988 7999
    India 6855 6852 6853
    India 7411








    The other database has only 'Deals' column and each row of the second database has just 1 Deal Number unlike the first database where 'Deals' column can have multiple numbers.

    I want to filter the first database where 'Deals' should have numbers that are present in the second database.
    For Eg: If the second database contains 8855 & 6853, I would want the 1st and 3rd entry from first database.

    FYI, the second database also has around 40000 rows.

    Thanks for your help.



    Regards,
    Vaibhav

    I have also posted same query on SQL Server Central
    http://www.sqlservercentral.com/Foru...093-391-1.aspx
    Last edited by c.vaibhav; 08-26-2012 at 01:47 AM. Reason: Cross Posted on SQLSERVERCENTRAL

  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,870
    Investigate Normalization. Get you tables Normalized and make life easier for yourself.

    With a Normalized table structure it appears you want to do something along this pattern
    select records in Table1 , Table2 (where there is a record in Table2 with a matching RecId)
    where Table2.RecId=Table1.RecId

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    To accomplish with present data structure will probably require VBA custom function. The function would:
    1. receive the multi-part string from table1 as an argument.
    2. parse string parts into array using Split function
    3. loop through the array and compare each element with values in table2 - DLookup could be used for the search
    4. if value found function returns a Yes value
    5. the function could be called from a query or a textbox
    6. if function called from query, apply filter criteria to field constructed with expression that calls the function

    Consider:
    Code:
    Function CompareDeal(strDeals As String) As String
    Dim ary As Variant
    ary = Split(strDeals, " ")
    For i = 0 To UBound(ary)
        If Not IsNull(DLookup("Deals", "Table2", "Deals='" & ary(i) & "'")) Then CompareDeal = "yes"
    Next
    End Function
    SELECT Deals, Region
    FROM Table1
    WHERE CompareDeal([Deals])="yes";
    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.

  4. #4
    c.vaibhav is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    3
    Thanks Orange and June7 for your help

    @June7 - I tried using your trick and it works absolutely fine on the dummy database I have. I need to try it on my main database which I will be able to do in another 18 hours max. I will close the thread once it works fine on the database I have, else I will bother you once again.

    Thank you very much for this amazing piece of code.

    Regards,
    Vaibhav

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Sometimes it is easier for me to see a VBA solution than a query but took another look and maybe this will get you what you want:

    SELECT Table1.Region, Table1.Deals
    FROM Table1, Table2
    WHERE (((Table1.Deals) Like "*" & [Table2].[Deals] & "*"));
    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.

  6. #6
    c.vaibhav is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    3
    Thanks June7

    Both code works fine. I am going with the first solution (VBA) as it works faster with the database size that I have.

    I believe the thread is classified as Solved. Do I need to take any other action now?

    Regards,
    Vaibhav

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Interesting that you found the VBA solution faster. Just glad something worked for you. No, you don't need to do anything else with the thread. I marked it solved after seeing your rep comment. Thank you.
    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.

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

Similar Threads

  1. Replies: 2
    Last Post: 06-19-2012, 08:30 AM
  2. Using InStr() inside Mid()
    By urbi in forum Programming
    Replies: 12
    Last Post: 06-06-2012, 12:00 PM
  3. Filtering based on form dates
    By cbh35711 in forum Programming
    Replies: 3
    Last Post: 03-13-2012, 11:46 AM
  4. Field rules/validation rules
    By sk88 in forum Access
    Replies: 14
    Last Post: 02-26-2012, 01:03 PM
  5. Replies: 0
    Last Post: 07-30-2009, 12:40 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