Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / March 2006

Tip: Looking for answers? Try searching our database.

Help with query to select the maximum date from a query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
graeme34 - 19 Mar 2006 16:05 GMT
Hi I have a query which selects all the details from a product table, as well
as the account index and date ordered for any order placed for that product
from the purchase order table and finally selects the price paid for that
order from the purchase details table....what I am trying to do is select
only the product that is currently shown in a combo box (displaying Product
codes) then display the order details of the last placed order......I have
tried the following query but it is returning empty, obviously I'm doing
somehting wrong, does anybody know what???

SELECT P.[Product Code], P.ProductType, P.Description, P.[Quantity In Stock],
P.[Quantity Description], PO.DateOrdered, POD.Price,
     PO.AccountIndex
FROM tblPurchaseOrder PO INNER JOIN (tblProduct P INNER JOIN
tblPurchaseOrderDetails POD ON P.[Product Code] = POD.ProductCode) ON
     PO.PurchaseOrderNumber = POD.PurchaseOrderNumber
WHERE PO.DateOrdered = (SELECT MAX(T.DateOrdered) FROM tblPurchaseOrder T
WHERE P.[Product Code] = [Forms]![frmProductEnquiry]![cboMoveTo]);

Thanks
graeme34 - 19 Mar 2006 17:09 GMT
Just a little update incase it helps, i think the problem lies in the WHERE
clause because if the value of the Product code in the combo box is also in
the detail of the last sales order placed then it is showing the record so I
think the MAX function is working, just not selecting the Product code first
prior to MAX..not too sure how to fix it though.. :(

>Hi I have a query which selects all the details from a product table, as well
>as the account index and date ordered for any order placed for that product
[quoted text clipped - 15 lines]
>
>Thanks
John Spencer - 19 Mar 2006 19:57 GMT
The following may give you what you want.  But I'm not sure.  

SELECT P.[Product Code], P.ProductType, P.Description, P.[Quantity In Stock],
P.[Quantity Description], PO.DateOrdered, POD.Price,
     PO.AccountIndex
FROM tblPurchaseOrder PO INNER JOIN (tblProduct P INNER JOIN
tblPurchaseOrderDetails POD ON P.[Product Code] = POD.ProductCode)
  ON PO.PurchaseOrderNumber = POD.PurchaseOrderNumber

SELECT P.[Purchase Order Number]
FROM tblPurchaseOrder P
WHERE P.Purchase Order
WHERE PO.DateOrdered = (SELECT MAX(TPO.DateOrdered)
FROM tblPurchaseOrder TPO INNER JOIN tblPurchaseOrderDetails TPOD
ON TPO.[Purchase Order Number] = TPOD.[Purchase Order Number]
WHERE TPOD.[Product Code] = [Forms]![frmProductEnquiry]![cboMoveTo])

AND PO.[Product Code] = [Forms]![frmProductEnquiry]![cboMoveTo]

> Hi I have a query which selects all the details from a product table, as well
> as the account index and date ordered for any order placed for that product
[quoted text clipped - 19 lines]
> Message posted via AccessMonster.com
> http://www.accessmonster.com/Uwe/Forums.aspx/access-queries/200603/1
graeme34 - 19 Mar 2006 20:40 GMT
Nope no joy with that John, thank you anyway :)
>The following may give you what you want.  But I'm not sure.  
>
[quoted text clipped - 20 lines]
>>
>> Thanks
Tom Ellison - 19 Mar 2006 20:52 GMT
Dear Graeme:

SELECT P.[Product Code], P.ProductType, P.Description, P.[Quantity In
Stock],
   P.[Quantity Description], PO.DateOrdered, POD.Price, PO.AccountIndex
 FROM tblPurchaseOrder PO
   INNER JOIN (tblProduct P
   INNER JOIN tblPurchaseOrderDetails POD
     ON P.[Product Code] = POD.ProductCode)
     ON PO.PurchaseOrderNumber = POD.PurchaseOrderNumber
 WHERE PO.DateOrdered =
   (SELECT MAX(T.DateOrdered)
     FROM tblPurchaseOrder T
 WHERE P.[Product Code] = [Forms]![frmProductEnquiry]![cboMoveTo]);

Hmmm.  Looks vaguely familiar.

May I suggest this:

Temporarily remove the last line, so it returns all [Product Code]s.  You
get some results, right?

Assuming you do, then it is this filtering that is at fault.  Choose one
specific [Product Code] produced by this query and Copy it.  Paste it into
your query in a new last line reading:

 WHERE P.[Product Code] = "XXXXXXXX";

where the X's are what you paste.  Omit the double quotes around the pasted
value if it is not a text value.  Run this.  Does it produce exactly that
product code?

Make sure the form is open, and that a product code has been entered into
the cboMoveTo control.  Change the first line of the query to be:

SELECT [Forms]![frmProductEnquiry]![cboMoveTo] AS cboValue,
   P.[Product Code], P.ProductType, P.Description, P.[Quantity In Stock],

This will now show the exact same thing as before, but with a new first
column, being what was entered into the combo box.  Does the cboValue column
now exactly match the [Product Code] column?

Please let me know what you learned from this.  Perhaps we can soon discover
where the error is.

Tom Ellison

> Hi I have a query which selects all the details from a product table, as
> well
[quoted text clipped - 19 lines]
>
> Thanks
graeme34 - 19 Mar 2006 21:59 GMT
Hi Tom
Yes everythng you stated went according to plan, therefore the error must lie
in the >  WHERE PO.DateOrdered =
   (SELECT MAX(T.DateOrdered)
     FROM tblPurchaseOrder T
 WHERE P.[Product Code] = [Forms]![frmProductEnquiry]![cboMoveTo]);
Clause, yet this is reason for the query to select only last record from the
tblPurchaseOrder using the MAX function on the date, any suggestions Tom ??
>Dear Graeme:
>
[quoted text clipped - 48 lines]
>>
>> Thanks
Tom Ellison - 19 Mar 2006 22:05 GMT
Dear Graeme:

I have missed something along the way, I believe.

Please try this:

SELECT P.[Product Code], P.ProductType, P.Description,
   P.[Quantity In Stock], P.[Quantity Description],
   PO.DateOrdered, POD.Price, PO.AccountIndex
 FROM tblPurchaseOrder PO
   INNER JOIN (tblProduct P
   INNER JOIN tblPurchaseOrderDetails POD
     ON P.[Product Code] = POD.ProductCode)
     ON PO.PurchaseOrderNumber = POD.PurchaseOrderNumber
 WHERE PO.DateOrdered =
   (SELECT MAX(T.DateOrdered)
     FROM tblPurchaseOrder T
 WHERE T.[Product Code] = P.[Product Code])
   AND P.[Product Code] =
     [Forms]![frmProductEnquiry]![cboMoveTo]);

It makes a lot more sense to me.  Does this fix anything.

Tom Ellison

> Hi Tom
> Yes everythng you stated went according to plan, therefore the error must
[quoted text clipped - 62 lines]
>>>
>>> Thanks
graeme34 - 19 Mar 2006 22:32 GMT
Success Tom thank you for your help :)
Just had to alter it a bit, Product Code is a field in PurchaseOrderDetails
not PurchaseOrder but you wasnt to know, very grateful for you help Tom....
P.S
I have another question in the Forms Programming section about linking a
Master/Sub form when the Master is data entry, wondering if there is any way
to do it without creating a  temp table??

>Dear Graeme:
>
[quoted text clipped - 26 lines]
>>>>
>>>> Thanks
graeme34 - 19 Mar 2006 22:54 GMT
In case your wondering Tom here is the working query

SELECT P.[Product Code], P.ProductType, P.Description, P.[Quantity In Stock],
P.[Quantity Description], PO.DateOrdered, POD.Price, PO.AccountIndex
FROM tblPurchaseOrder AS PO INNER JOIN (tblProduct AS P INNER JOIN
tblPurchaseOrderDetails AS POD ON P.[Product Code] = POD.ProductCode) ON PO.
PurchaseOrderNumber = POD.PurchaseOrderNumber
WHERE (((P.[Product Code])=[Forms]![frmProductEnquiry]![cboMoveTo]) AND ((PO.
DateOrdered)=(SELECT MAX(T.DateOrdered)      FROM tblPurchaseOrder T INNER
JOIN tblPurchaseOrderDetails TD      ON T.PurchaseOrderNumber = TD.
PurchaseOrderNumber  WHERE TD.[ProductCode] = P.[Product Code])));

But after getting the query working its had a knock on effect when I
havechanged the forms control source. to this query now my combo box isnt
working which fills the detail section, the recordset is staying empty and
getting the no current record when i try to display the data in the form..
heres the code..

Private Sub cboMoveTo_AfterUpdate()
   
   Dim rs As DAO.Recordset
     
   If Not IsNull(Me.cboMoveto) Then

       'Save before move.
       If Me.Dirty Then
           Me.Dirty = False
       End If
       'Search in the clone set.
       Set rs = Me.RecordsetClone

       rs.FindFirst "[Product Code] = """ & Me.cboMoveto & """"
     
          'Display the found record in the form.
       Me.Bookmark = rs.Bookmark
       rs.Close
   End If
End Sub

>Success Tom thank you for your help :)
>Just had to alter it a bit, Product Code is a field in PurchaseOrderDetails
[quoted text clipped - 9 lines]
>>>>>
>>>>> Thanks
graeme34 - 19 Mar 2006 22:57 GMT
I think I know the problem, as this code is on the afterupdate the query has
not been ran, I think , will have to have a mess about with it...
>In case your wondering Tom here is the working query
>
[quoted text clipped - 40 lines]
>>>>>>
>>>>>> Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.