473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Joining a Query and Table while Excluding some results

Here's the problem;

We need to return a query that shows the products that aren't
associated with a specific PriceGroupID.(PriceGroupID is the
specific group that will see these prices)

We've got a Product table

ProductID | ProductName |
-------------------------
1 | Product01 |
2 | Product02 |
3 | Product03 |
4 | Product04 |

and a Price table.

PriceID | PriceGroupID| ProductID| Price |
-----------------------------------------------
1 | 1 | 2 | $35 |
2 | 1 | 4 | $59 |
3 | 2 | 4 | $24 |
4 | 3 | 3 | $99 |

We can make a query (qryGroupsPrices) that just lists one
PriceGroupID from the Price Table which comes from a URL
querystring variable [MMColParam]

i.e.
SELECT *
FROM tblPrices
WHERE ((tblPrices.PriceGroupID)=[MMColParam]);

Then we make an outer join query that finds the Products that aren't
yet associated with the specific group;

SELECT tblProducts.ProductID, tblProducts.ProductName,
qryGroupsPrices.PriceGroupID
FROM tblProducts LEFT JOIN qryGroupsPrices
ON tblProducts.ProductID = qryGroupsPrices.ProductID
WHERE ((qryGroupsPrices.ProductID) Is Null);

And Wallah! It works fine from Access but when we try and get
a result from the web page that sends the querystring [MMColParam]
to this query we get a "Too Few Parameters. Expected 1" Result.

We tried to combine the two queries but we're not so familiar with
the SQL idiosyncrasies of Access and have spent two days pulling
our hair out over it. This just can't be so difficult so please help
us if you can?

In desperate need,

Judy
Nov 13 '05 #1
6 1792
"Judy" <te*****@gmail.com> wrote in message
news:d5*************************@posting.google.co m...
Here's the problem;

We need to return a query that shows the products that aren't
associated with a specific PriceGroupID.(PriceGroupID is the
specific group that will see these prices)

We've got a Product table

ProductID | ProductName |
-------------------------
1 | Product01 |
2 | Product02 |
3 | Product03 |
4 | Product04 |

and a Price table.

PriceID | PriceGroupID| ProductID| Price |
-----------------------------------------------
1 | 1 | 2 | $35 |
2 | 1 | 4 | $59 |
3 | 2 | 4 | $24 |
4 | 3 | 3 | $99 |

maybe this:

parameters [MMColParam] int;
select * from tblProducts as p
where not exists
(
select * from tblPrices as c2
where c2.PriceGroupID| = [MMColParam]
and c2.ProductID = p.ProductID
)


Nov 13 '05 #2
Thanks John,

That's great... I didn't know you could do that with Access... Great
learning this stuff... Thanks!

The only problem is I'm now not so sure how to use the parameter to get
the query?

I'm used to querying a table from an ASP page like this;

<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
If (Request.QueryString("PriceGroupID") <> "") Then
Recordset1__MMColParam = Request.QueryString("PriceGroupID")
End If
%>
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_AutoGates_STRING
Recordset1.Source = "SELECT * FROM Query1 WHERE PriceGroupID = " +
Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>

Sorry for being a little bit slow with this... But if it's any
consolation, the learning curve is fast thanks to you.

Thanks heaps!
Jude


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3
My last reply didn't seem to post... Hmmph! Here's another...

Thanks heaps for that John... I didn't know you could do stuff quite
like that in Access... I'm learning lots here so thanks ever so much!

The only thing is that I'm now not so sure how to get the records from
the query? This is what I'm used to doing to access a table or query on
a web page;

<%
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
If (Request.QueryString("PriceGroupID") <> "") Then
Recordset1__MMColParam = Request.QueryString("PriceGroupID")
End If
%>
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_AutoGates_STRING
Recordset1.Source = "SELECT * FROM Query1 WHERE PriceGroupID = " +
Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>

Sorry for being naive with this... but if it's any consolation your help
has taken a weight of my shoulders!

Thanks Heaps,
Jude

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4
"Judy Stevens" <te*****@gmail.com> wrote in message
news:41**********************@news.newsgroups.ws.. .
Dim Recordset1__MMColParam
Recordset1__MMColParam = "1"
If (Request.QueryString("PriceGroupID") <> "") Then
Recordset1__MMColParam = Request.QueryString("PriceGroupID")
End If
%>
<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_AutoGates_STRING
Recordset1.Source = "SELECT * FROM Query1 WHERE PriceGroupID = " +
Replace(Recordset1__MMColParam, "'", "''") + ""
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()

Recordset1_numRows = 0
%>


The problem is that you have a parameter in the query, so you need to pass
the parameter explicitly, something like this, (long time since I've used
ASP so may be syntax errors)
<%
Dim priceGroupID

priceGroupID = "1"
If (Request.QueryString("PriceGroupID") <> "") Then
priceGroupID = Request.QueryString("PriceGroupID")
End If

%>

<%
Dim Recordset1
Dim cmd
Dim prm

Set cmd = Server.CreateObject("ADODB.Command")
cmd .ActiveConnection = MM_AutoGates_STRING
cmd .CommandText = "Query1"
cmd .CommandType = 4

cmd.Parameters.Append cmd.CreateParameter("priceGroup", adInteger,
adParamInput, , priceGroupID)

Set Recordset1= Server.CreateObject("ADODB.Recordset")
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open cmd

%>

Alternatively, assemble the sql string in code and dispense with the query
altogether:

<%
Dim priceGroupID
priceGroupID = "1"
If (Request.QueryString("PriceGroupID") <> "") Then
priceGroupID = Request.QueryString("PriceGroupID")
End If
%>

<%
Dim Recordset1

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_AutoGates_STRING

Recordset1.Source = "select * from tblProducts as p where not exists " _
& "(select * from tblPrices as c2 where c2.PriceGroupID = " _
& priceGroupID & " and c2.ProductID = p.ProductID)"

Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()
%>




Nov 13 '05 #5
te*****@gmail.com (Judy) wrote in message news:<d5*************************@posting.google.c om>...
Here's the problem;

We need to return a query that shows the products that aren't
associated with a specific PriceGroupID.(PriceGroupID is the
specific group that will see these prices)

We've got a Product table

ProductID | ProductName |
-------------------------
1 | Product01 |
2 | Product02 |
3 | Product03 |
4 | Product04 |

and a Price table.

PriceID | PriceGroupID| ProductID| Price |
-----------------------------------------------
1 | 1 | 2 | $35 |
2 | 1 | 4 | $59 |
3 | 2 | 4 | $24 |
4 | 3 | 3 | $99 |

We can make a query (qryGroupsPrices) that just lists one
PriceGroupID from the Price Table which comes from a URL
querystring variable [MMColParam]

i.e.
SELECT *
FROM tblPrices
WHERE ((tblPrices.PriceGroupID)=[MMColParam]);

Then we make an outer join query that finds the Products that aren't
yet associated with the specific group;

SELECT tblProducts.ProductID, tblProducts.ProductName,
qryGroupsPrices.PriceGroupID
FROM tblProducts LEFT JOIN qryGroupsPrices
ON tblProducts.ProductID = qryGroupsPrices.ProductID
WHERE ((qryGroupsPrices.ProductID) Is Null);

And Wallah! It works fine from Access but when we try and get
a result from the web page that sends the querystring [MMColParam]
to this query we get a "Too Few Parameters. Expected 1" Result.

We tried to combine the two queries but we're not so familiar with
the SQL idiosyncrasies of Access and have spent two days pulling
our hair out over it. This just can't be so difficult so please help
us if you can?

In desperate need,

Judy


Probably shouldn't be answering this, but have you looked at this:
http://www.mvps.org/access/queries/qry0013.htm

my *guess* is that you should be using ADO and a command object with
parameters. Then you should have no problem.
Nov 13 '05 #6

Hallelujah!

Got it finally with thanks John Winterbottom... We ended up doing the
query from the web page instead of making a separate query in Access.

Here's the web page code that does the magic. Hope this saves anyone
else from the 3 days of agony we suffered trying to solve it. Cheers to
all!!!!! And a huge CHEERS to John for your help... You deserve 5 gold
stars for that help... Absolute professional!!!

Jude :D

<%
Dim Recordset1__PriceGroupID
Recordset1__PriceGroupID = "1"
If (Request.QueryString("PriceGroupID") <> "") Then
Recordset1__PriceGroupID = Request.QueryString("PriceGroupID")
End If
%>

<%
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_AutoGar_STRING
Recordset1.Source = "select * from tblProducts as p where not exists
(select * from tblPrices as c2 where c2.PriceGroupID = " +
Replace(Recordset1__PriceGroupID, "'", "''") + " and c2.ProductID =
p.ProductID)"

Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()
Recordset1_numRows = 0
%>

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: sdowney717 | last post by:
Select LOCGeneralHave.LSCH, LOCSubClassHave.LSCH from LOCGeneralHave , LOCSubClassHave Where (LOCGeneralHave.LCNT <> '0' and LOCSubClassHave.LCNT = '0') This query seems to be ignoring the...
3
by: james | last post by:
Hi, I would like to get all the records from 9 tables that have the same field value in one field (it is a unique field)that is shared by all the tables. Would this method of joining work: ...
7
by: Not Me | last post by:
Hi, Having a table with some duplicate ID's (different data tho), how can I return the list but with only one record from each ID? Would this be using the first() function and grouping? ...
3
by: Matt. | last post by:
Hi all! The SQL is below. Here is the scenario. The table Production contains all of the production information for our plant. Primary key is Job #, ProDate, Shift, Machine #. The table...
5
by: redstamp | last post by:
Try as I might I cannot find a way to write an access query to return a result set with the records from my database WITHOUT a certain set of values within a field. To explain, I have a table of...
4
by: J | last post by:
I am editing a pre-existing view. This view is already bringing data from 40+ tables so I am to modify it without screwing with anything else that is already in there. I need to (left) join it...
8
by: fel | last post by:
does anybody know of any *web* query builder in PHP, something like the Access query builder, or similar, done in PHP or similar? I've looked for it trough the net, but nothing is colse to what I...
4
by: The.Daryl.Lu | last post by:
Hi, Have a bit of a problem... I've created a form in Access and will use the form for a user to query a table based on the selected fields. The problem lies in that I was using checkboxes for...
2
by: Neekos | last post by:
Im working on web tool that will allow supervisors to see a list of their agents and their call stats for a call center. I have one main table that holds employee IDs and their supervisor names....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.