To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Database Development

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Mar 10th, 2005, 04:43 PM   #1
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
Resolved Testing if a record exists in another table [Resolved]

Hi,

I have two tables:
- [Members] containing the fields MemID (a unique number), Surname and Forenames
- [RegMem] containing the fields SDate and MemID

The database holds information about my local hockey club. [RegMem] is a register of the members present on any date, so it contains a list of dates each linked to one MemID if the member was present on that date.
For example:
01/01/2005 - 007
01/01/2005 - 015
01/01/2005 - 126
01/01/2005 - 018
etc.

The program I am writing (in VB6, using ADO) has to output a list of the names of all the members and then a 'Y' or 'N' depending on whether they were present on that date.

At the moment, I am doing this by doing:
"SELECT Surname & Chr(44) & Chr(32) & Forenames AS FullName, MemID
FROM [Members];"

Then looping through each item in the created Recordset and doing (where dteSDate and intSDate are my passed arguments):
"SELECT MemID
FROM [RegMem]
WHERE SDate=#" & Format(dteSDate, "yyyy-mm-dd") & "# AND MemID=" & intMemID & ";"

Then testing if the RecordCount property of the Recordset is 0.

This is quite slow, as it involves creating one Recordset for the member list, then another one to test for each separate MemID. How could I improve this?

I thought of something along the lines of this:
"SELECT Surname & Chr(44) & Chr(32) & Forenames AS FullName
FROM Members
INNER JOIN RegMem
ON Members.MemID = RegMem.MemID
WHERE SDate = #" & Format(dteSDate, "yyyy-mm-dd") & "#;"

which returns the name of everyone present on a date, but I still need the full list of members.

Can this be done with something like RIGHT JOIN?

Last edited by olamm2k; Mar 10th, 2005 at 06:46 PM.
olamm2k is offline   Reply With Quote
Old Mar 10th, 2005, 04:52 PM   #2
PilgrimPete
Frenzied Member
 
PilgrimPete's Avatar
 
Join Date: Feb 02
Posts: 1,313
PilgrimPete  is on a distinguished road (40+)
Re: Testing if a record exists in another table

Yes, a LEFT or RIGHT OUTER JOIN can be used.
Something like this:
Code:
"SELECT Surname & Chr(44) & Chr(32) & Forenames AS FullName,
IIF(IsNull(RegMem.MemID), "N", "Y") AS Present
FROM Members
LEFT JOIN RegMem
 ON Members.MemID = RegMem.MemID
WHERE SDate = #" & Format(dteSDate, "yyyy-mm-dd") & "#;"
I'm assuming that you are using Access...
PilgrimPete is offline   Reply With Quote
Old Mar 10th, 2005, 05:03 PM   #3
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
Re: Testing if a record exists in another table

Quote:
Originally Posted by PilgrimPete
Yes, a LEFT or RIGHT OUTER JOIN can be used.
Something like this:
Code:
"SELECT Surname & Chr(44) & Chr(32) & Forenames AS FullName,
IIF(IsNull(RegMem.MemID), "N", "Y") AS Present
FROM Members
LEFT JOIN RegMem
 ON Members.MemID = RegMem.MemID
WHERE SDate = #" & Format(dteSDate, "yyyy-mm-dd") & "#;"
I'm assuming that you are using Access...
LEFT JOIN is the one that selects everything from the first table and just the records from the second table with the matching field specified, right?

What's the difference between an INNER and OUTER JOIN?

I am using access. I didn't know you could use functions like IIF and IsNull in statements like that. I'll give it a try - thanks.
olamm2k is offline   Reply With Quote
Old Mar 10th, 2005, 05:16 PM   #4
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
Re: Testing if a record exists in another table

I'm getting an error (expected end of statement) from the double quotes in the IIF statement (this is in VB):

VB Code:
  1. strQuery = "SELECT Surname & Chr(44) & Chr(32) & Forenames " & _
  2.           "AS FullName, IIF(IsNull(RegMem.MemID), [color=Red]"N", "Y"[/color]) " & _
  3.           "AS Present From Members LEFT JOIN RegMem " & _
  4.           "ON Members.MemID = RegMem.MemID WHERE SDate = #" & _
  5.           Format(#7/4/2005#, "yyyy-mm-dd") & "#;"
  6.        
  7.         objRS.Open strQuery, objConn, adOpenStatic, adLockPessimistic

What's the proper syntax for this?
olamm2k is offline   Reply With Quote
Old Mar 10th, 2005, 05:21 PM   #5
PilgrimPete
Frenzied Member
 
PilgrimPete's Avatar
 
Join Date: Feb 02
Posts: 1,313
PilgrimPete  is on a distinguished road (40+)
Re: Testing if a record exists in another table

Yes, a LEFT JOIN takes all the records from the first table and the matching rows from the second. A RIGHT JOIN does the opposite - takes all the records from the second table and the matching rows from the first.
[OUTER is an optional Keyword in Access for LEFT and RIGHT.]
A FULL OUTER JOIN returns records from both tables regardless of matching... not all databases support this though.
There is an article on MSDN that might be of interest...
http://msdn.microsoft.com/library/de...qd_09_0zqr.asp
PilgrimPete is offline   Reply With Quote
Old Mar 10th, 2005, 05:24 PM   #6
PilgrimPete
Frenzied Member
 
PilgrimPete's Avatar
 
Join Date: Feb 02
Posts: 1,313
PilgrimPete  is on a distinguished road (40+)
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
PilgrimPete is offline   Reply With Quote
Old Mar 10th, 2005, 05:28 PM   #7
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
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.
olamm2k is offline   Reply With Quote
Old Mar 10th, 2005, 05:33 PM   #8
PilgrimPete
Frenzied Member
 
PilgrimPete's Avatar
 
Join Date: Feb 02
Posts: 1,313
PilgrimPete  is on a distinguished road (40+)
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?
PilgrimPete is offline   Reply With Quote
Old Mar 10th, 2005, 05:43 PM   #9
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
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?
olamm2k is offline   Reply With Quote
Old Mar 10th, 2005, 05:54 PM   #10
PilgrimPete
Frenzied Member
 
PilgrimPete's Avatar
 
Join Date: Feb 02
Posts: 1,313
PilgrimPete  is on a distinguished road (40+)
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?
PilgrimPete is offline   Reply With Quote
Old Mar 10th, 2005, 06:04 PM   #11
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
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?
olamm2k is offline   Reply With Quote
Old Mar 10th, 2005, 06:22 PM   #12
PilgrimPete
Frenzied Member
 
PilgrimPete's Avatar
 
Join Date: Feb 02
Posts: 1,313
PilgrimPete  is on a distinguished road (40+)
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
PilgrimPete is offline   Reply With Quote
Old Mar 10th, 2005, 06:45 PM   #13
olamm2k
Lively Member
 
Join Date: Feb 05
Posts: 116
olamm2k is an unknown quantity at this point (<10)
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.
olamm2k is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Database Development


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:25 AM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.