Results 1 to 4 of 4
  1. #1
    davideps is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    2

    Creating a function that queries a one to many relationship

    Hi Everyone,

    In order to ask my question, I need to provide a bit of background with the question at the end. I am creating a database that allows me to add an arbitrary number of tags to notes. I have three tables: tags, notes, and tagnotes (the last is an association table). Roughly these look like:

    TAGS
    id,tag
    1,"participation"
    2,"citizenship"
    3,"democracy"

    NOTES
    id,longtext
    1,"long memo from a book"
    2,"another long memo from a book"

    TAGNOTES
    id,tagid,noteid
    1,2,1

    So, the last table shows that the second note has the tag "participation". The following query returns only those notes with a tag that at least partially matches a string (in this case 'partic'):



    SELECT n.*
    FROM tags AS t, notes n, tagnotes AS tn
    WHERE t.tag like '*Partic*' AND tn.tagid=t.id AND tn.noteid=n.id

    Here is my question--I want to turn this query into a Boolean function "hasTag" so I can write complex queries concisely in a form textbox with a "filter on" and "filter off" button. Those queries would look like: "hasTag('Partic') AND not(hasTag('Citiz')". I'm not sure how to start. Is this a VBA function using DoCmd.RunSQL?

    Thank you
    -david

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'm not really clear on the goal, but it sounds like you want to pass a portion of a tag and see if it has a note? You wouldn't use RunSQL, as that will only run an action query, which that is not. You could open a recordset on SQL built using the input parameter and test whether or not it returned a record (test for EOF). This might give you a start:

    http://www.baldyweb.com/Function.htm

    plus of course you'll need to build SQL and use it in a recordset.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Don't understand the query - there are no join clauses. Are you getting expected results?

    Don't really understand what you want to accomplish. You want to run a query that filters records where 'Partic' is in tag but 'Citiz' is not? DoCmd.RunSQL is used only with action SQL's like INSERT, UPDATE, DELETE. Can use DoCmd.OpenQuery to open an Access SELECT query.

    Just what is it you want to apply 'filter on' and 'filter off' to?
    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
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by June7 View Post
    there are no join clauses. Are you getting expected results?
    David used the older style of criteria instead of joins, which I wouldn't use but probably returns the expected result.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 6
    Last Post: 03-09-2012, 01:07 PM
  2. Creating relationship where none exists
    By bespra in forum Database Design
    Replies: 8
    Last Post: 09-21-2011, 11:07 AM
  3. Creating a driving relationship on a form
    By ld8732 in forum Database Design
    Replies: 1
    Last Post: 12-31-2010, 06:20 AM
  4. Build a relationship between Queries (Howto)
    By Access_Headaches in forum Queries
    Replies: 1
    Last Post: 06-24-2010, 01:41 PM
  5. Creating an algorithm function.
    By NGLG in forum Access
    Replies: 0
    Last Post: 12-07-2008, 09:57 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