Results 1 to 13 of 13

Thread: Testing if a record exists in another table [Resolved]

Hybrid View

  1. #1
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Testing if a record exists in another table

    Double up your double quotes (or use CHR$(34)).
    VB Code:
    1. 'doubling up double quotes
    2. strQuery = "SELECT Surname & Chr(44) & Chr(32) & Forenames " & _
    3.           "AS FullName, IIF(IsNull(RegMem.MemID), ""N"", ""Y"") " & _
    4.           "AS Present From Members LEFT JOIN RegMem " & _
    5.           "ON Members.MemID = RegMem.MemID WHERE SDate = #" & _
    6.           Format(#7/4/2005#, "yyyy-mm-dd") & "#;"
    7.                 objRS.Open strQuery, objConn, adOpenStatic, adLockPessimistic

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Testing if a record exists in another table

    That works for displaying the character, but the query only returns the members who were present on that date, not those who were absent.

  3. #3
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Testing if a record exists in another table

    Oh, I get it. I wasn't paying attention!

    You'll be needing a subquery then...
    VB Code:
    1. strQuery = "SELECT Surname & Chr(44) & Chr(32) & Forenames " & _
    2.           "AS FullName, IIF(IsNull(RegMem.MemID), ""N"", ""Y"") " & _
    3.           "AS Present From Members LEFT JOIN (SELECT MemID FROM RegMem WHERE SDate = #" & Format(#7/4/2005#, "yyyy-mm-dd") & "#) RegMem " & _
    4.           "ON Members.MemID = RegMem.MemID
    I doubt if that is syntactically spot on, but you get the idea?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Testing if a record exists in another table

    It works, but I'm not quite clear on the part between the 'LEFT JOIN' and 'ON' - the subquery followed by a table name. How does that work?

  5. #5
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Testing if a record exists in another table

    VB Code:
    1. (SELECT MemID FROM RegMem WHERE SDate = #" & Format(#7/4/2005#, "yyyy-mm-dd") & "#) AS AnyNameYouLike "
    The subquery is effectively like an inline query, I just happened to have aliased it to the same name as the table it came from in the previous post (and I was lazy and skipped the 'AS').
    You would get the same effect if you created a saved query in access with that SQL, and JOINed on that instead. Does that make it any clearer?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Testing if a record exists in another table

    The missing 'AS' did throw me.

    I've now got it as:
    VB Code:
    1. strQuery = "SELECT Surname & Chr(44) & Chr(32) & Forenames " & _
    2.       "AS FullName, IIF(IsNull(QueryDateOnly.MemID), ""N"", ""Y"") " & _
    3.       "AS Present FROM Members LEFT JOIN " & _
    4.       "(SELECT MemID FROM RegMem WHERE SDate = #" & _
    5.       Format("2005-04-07", "yyyy-mm-dd") & "#) AS QueryDateOnly " & _
    6.       "ON Members.MemID = QueryDateOnly.MemID;"

    But why is the subquery needed?

  7. #7
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Testing if a record exists in another table

    Because you don't want to limit the whole result set, just the subset of members present on that date.

    The original query I posted had the following logic:
    Return rows for all members, joining all records in RegMem, marking those not present with an 'N', BUT that have a row in RegMem with an SDate of xyz. This is contradictory rubbish.
    The revised query has this logic:
    Return rows for all members, joining ONLY those rows in RegMem with an SDate of xyz, marking those not present with an 'N'.

    This is surprisingly hard to explain!
    Hope I got close though

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Testing if a record exists in another table

    It's an odd concept to get your head around, but I think the first query would be the same as doing:
    Code:
    "SELECT Surname & Chr(44) & Chr(32) & Forenames " & _
      "AS FullName, IIF(IsNull(RegMem.MemID), ""N"", ""Y"") " & _
      "AS Present FROM Members, RegMem WHERE SDate = #" & _
      Format("2005-04-07", "yyyy-mm-dd") & "# AND " & _
      "Members.MemID = RegMem.MemID;"
    Thanks for your help.

Posting Permissions

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



Click Here to Expand Forum to Full Width