Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Old 09-24-2001, 02:59 PM   #1
Member (10 bit)
 
Join Date: May 1999
Location: Orlando, FL
Posts: 975
Send a message via ICQ to artsapimp
ASP and Access Recordsets

I am trying to create an application which queries an access database and displays a list of all people with a specific field empty. The problem I am having is the field is part of a relationship so the records that are empty don't get included in the recordset at all.

The two databases I am opening is [Representative] and [ActionItem]. I would like to display all reps who have not been assigned an ActionItem in a list. When I run the query it runs all of the reps who DO have an ActionItem instead.

How could I possibly resolve this issue other than opening the first database, saving the RepID as a variable, opening the ActionItem database where RepID = varRepID, then re-opening the Representative database again, etc.

This is too much opening of databases and is poor coding. Any help would be greatly appreciated.

Art
artsapimp is offline   Reply With Quote
Old 09-25-2001, 02:39 AM   #2
SQL nutcase
 
mosquito's Avatar
 
Join Date: Sep 2000
Location: Belgium
Posts: 1,136
Send a message via AIM to mosquito
A left join will do the trick.

If you have tables like this
Representative ( repid, name, adress)
ActionItem ( Actid, repid, blabla)

You could retrieve all Representatives with no action items by using this query.

Code:
select r.repid, r.name , r.adress 
from Representative r
left join ActionItem a
on r.repid = a.repid
where actid is null
I hope this helps
mosquito is offline   Reply With Quote
Old 09-25-2001, 09:27 AM   #3
Member (10 bit)
 
Join Date: May 1999
Location: Orlando, FL
Posts: 975
Send a message via ICQ to artsapimp
Thank you

The following is the strSQL I have that kind of works right now. What it's doing is creating all joins correctly, but the ActionItem and Scorecard database is empty until something is written to it. Because of that the recordset always comes up blank. I have commented out the last line and it shows all records.
Code:
strSQL = "SELECT Manager.FullRecord, Scorecards.Reviewed, Rep.First_Name, Rep.Last_Name"
strSQL = strSQL & " FROM (Scorecards"
strSQL = strSQL & " LEFT JOIN ActionItems"
strSQL = strSQL & " ON Scorecards.ScorecardID = ActionItems.Scorecard)"
strSQL = strSQL & " LEFT JOIN (Manager"
strSQL = strSQL & " RIGHT JOIN Rep"
strSQL = strSQL & " ON Manager.ManagerID = Rep.Manager)"
strSQL = strSQL & " ON Scorecards.Rep = Rep.RepID"
strSQL = strSQL & " WHERE Reviewed = True"
strSQL = strSQL & " AND Manager.FullRecord = 213"
strSQL = strSQL & " and Reviewed is null"
The database is setup with 4 tables (Manager, Scorecards, ActionItems, Rep).

Thank you for any help.

Art Sapimp
artsapimp is offline   Reply With Quote
Old 09-25-2001, 10:07 AM   #4
SQL nutcase
 
mosquito's Avatar
 
Join Date: Sep 2000
Location: Belgium
Posts: 1,136
Send a message via AIM to mosquito
I would write the query like this:
Code:
SELECT m.FullRecord, s.Reviewed, r.First_Name, r.Last_Name
FROM Rep r
  LEFT JOIN Manager m
  ON m.ManagerID = r.manager
  LEFT JOIN (Scorecard s
    LEFT JOIN ActionItems a
    ON s.ScorecardID = a.Scorecard)
  ON s.Rep = r.RepID
WHERE m.FullRecord = 21
  and s.reviewed is null
I removed 'Reviewed = True' because that blocks the 's.reviewed is null'.
mosquito is offline   Reply With Quote
Old 09-25-2001, 11:33 AM   #5
Member (10 bit)
 
Join Date: May 1999
Location: Orlando, FL
Posts: 975
Send a message via ICQ to artsapimp
When I removed that line it still came up with no people listed. I ended up playing with your code and still could not get it to work. Eventually I opened Access and created the query I wanted, went to SQL mode and copied and pasted it. I don't know why it worked like it did but I would love to know. If someone could explain this a little better for me I would really appreciate it. Thanks.

Here's the code that worked
Code:
strSQL = "SELECT Manager.FullRecord, Scorecards.Reviewed, Rep.First_Name, Rep.Last_Name"
strSQL = strSQL & " FROM Manager"
strSQL = strSQL & " 	RIGHT JOIN (Scorecards"
strSQL = strSQL & " 		RIGHT JOIN Rep"
strSQL = strSQL & " 		ON Scorecards.Rep = Rep.RepID)"
strSQL = strSQL & " 	ON Manager.ManagerID = Rep.Manager"
strSQL = strSQL & " WHERE Scorecards.Reviewed Is Null AND Manager.FullRecord = 213"
Art
artsapimp is offline   Reply With Quote
Old 09-30-2001, 01:43 PM   #6
SQL nutcase
 
mosquito's Avatar
 
Join Date: Sep 2000
Location: Belgium
Posts: 1,136
Send a message via AIM to mosquito
The Left and right join are both of the "outer join" type. I'll explain it with an example.
Table A(id, name) ==> id is primary key
Table B(id, Aid, score) ==> id is primary key

The tables have the following values:
Code:
Table A     Table B
-------     -------
1, John     1, 2, 100
2, Pete     2, 2, 75
3, Eddy     3, 3, 55
            4, 2, 50
When we do a left join from table A to table B with the query
Code:
select q.name, b.score from A left join B on A.id = B.Aid
we will have the following result
Code:
name, score
-----------
John, NULL
Pete, 100
Pete, 75
Eddy, 55
Pete, 50
as you can see all records from table A are taken, and the matching records from table B. If we want the records from table A that don't have a matching record in table B, we use the query
Code:
select q.name, b.score from A left join B on A.id = B.Aid where B.id is null
The B.score is null filters out all records that have a value for score. Please note that you can only use this method when you are sure that there is always a value in the id column (thats why I filtered on the id column, and not on the score column. id is primary key, and therefore can not be NULL)

Another way to find the records in table A that don't exist in table B is with a subquery.
Code:
select A.name 
from A
where id not in (select Aid from B)
This method also works but is a LOT slower then the outer join.

The difference between a LEFT JOIN and a RIGHT JOIN is simply that with a left join you take all records from table A and the matches in table B, and with a right join you take all records from table B, and the matches from table A

I hope this helps.
mosquito is offline   Reply With Quote
Old 09-30-2001, 06:47 PM   #7
Member (10 bit)
 
Join Date: May 1999
Location: Orlando, FL
Posts: 975
Send a message via ICQ to artsapimp
Thank you

I have read many tutorials, books, source code, etc. and have never understood the difference between joins. I use RIGHT JOINS on a daily basis and I guess it's been luck but it always works like I want it to.

Thank you very much for your help.

Art
artsapimp is offline   Reply With Quote
Old 10-01-2001, 04:12 PM   #8
Member (10 bit)
 
Join Date: May 1999
Location: Orlando, FL
Posts: 975
Send a message via ICQ to artsapimp
More Recordset issues

The easiest way to explain the project goal I am working toward is to describe a bulletin board.

Tables in the database
PHP Code:
Category:
-
CategoryID (autonumber)
-
Category (text)

Topic:
-
TopicID (autonumber)
-
Topic (text)
-
Category (number
Example information for each table
PHP Code:
Category:
1Drinks
2
Snacks
3
Meals

Topics
:
1Coke1
2
Pepsi1
3
Orange Juice1
4
Crackers2
5
Peanuts2
6
Hamburger3
7
Steak
SQL statement
PHP Code:
SELECT Category.CategoryIDCategory.CategoryTopic.TopicIDTopic.Topic
FROM Topic RIGHT JOIN Category ON Category
.CategoryID Topic.Category 
How would I loop through the recordset showing the data like a messageboard would?

Code:
Drinks
     Coke
     Pepsi
     Orange Juice
Snacks
     Crackers
     Peanuts
Meals
     Hamburger
     Steak
Every time I try I get either a BOF or EOF error or it just shows the first category.

Thanks for any help.
artsapimp is offline   Reply With Quote
Old 10-05-2001, 05:05 PM   #9
SQL nutcase
 
mosquito's Avatar
 
Join Date: Sep 2000
Location: Belgium
Posts: 1,136
Send a message via AIM to mosquito
this code simply loops through the recordset and writes the result. If you want to make titles and stuff, you can join everything together (heavy on db server) and simply loop through the single recordset or retrieve a titles recordset, and then retrieve topics per title (heavier on the webserver).

the big recordset would be something like
title message
-----------------
test mess1
test mess2
test2 mess1
test2 mess2
test2 mess3

so you print the title, and then the message, and as long as the title remains the same, only print the messages.


[CODE]
<html>
<body>
<% test %>
</body>
</html>

<%

Sub test()
Dim conn
Dim rs
Dim cm

Set conn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
Set cm = CreateObject("adodb.command")

conn.ConnectionString = "your connectionstring"
conn.Open

If conn.State = adStateOpen Then
Set cm.ActiveConnection = conn
cm.CommandTimeout = 10
cm.CommandText = "select * from message"
Set rs = cm.Execute

If rs.State = adStateOpen Then
Do While Not rs.EOF
response.write "blablabla" & rs.Fields("message").Value
rs.MoveNext
Loop
rs.Close
End If

End If

Set rs = Nothing
Set cm = Nothing
conn.Close
Set conn = Nothing
End Sub
%>
[CODE]
mosquito is offline   Reply With Quote
Old 10-06-2001, 10:37 AM   #10
Member (10 bit)
 
Join Date: May 1999
Location: Orlando, FL
Posts: 975
Send a message via ICQ to artsapimp
Thank you.

That is pretty much what I've done now. It's running a few loops and comparing each one to each other. I'm not good at explaining it, but it's working.

Thanks again everyone.
artsapimp is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:29 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2