Question : Dlookup with multiple criterias and tables

I'm trying to display the Status on a form based on the most recent [StatusHistoryDateStamp] field.
I have a parent form "frmOrders" (based on tblOrders) and a subform "SubFormOrderStatus" (based on the tblOrderStatusHistory table)


lkpOrderStatusState
-------------------------------------
StatusID |  StatusName
------------    --------------------
    1           New  
    2           In Progress
    3           Pending
    4           Close

tblOrders
------------------------------------------------
OrderID  | OrderStatusHistoryID
------------------------------------------------
    001                  1


tblOrderStatusHistory
--------------------------------------------------------------------------------------------------------
OrderStatusHistoryID  | OrderID | StatusHistoryDateStamp   | StatusID
--------------------------------------------------------------------------------------------------------
     1                                  001             Dec 1 2009                          1
     2                                  001             Dec 2 2009                          2
     3                                  001             Dec 3 2009                          3


I then tried query design and performing Max totals and group by, but couldn't figure it out.

So then I did this really long approach within the forms properties control source because I can't figure out how to combine it nor have the coding skill to to it within code builder.


1. I first get the latest date for the current order that the form is displaying:

=DMax("StatusHistoryDateStamp","tblOrderStatusHistory","OrderID=" & [Forms]![frmViewOrder]![SubFormOrderStatus].[Form]![txtOrderID])


2. Then I got the associated StatusID of the tblOrderStatusHistory:

=DLookUp("OrderStatusStateID","tblOrderStatusHistory","[OrderID]=" & [Forms]![frmViewOrder]![SubFormOrderStatus].[Form]![txtOrderID] & "And [StatusHistoryDateStamp]= #" & [txtLastUpdated] & "#")


3. Finally I'm able to lookup the actual status name:
=DLookUp("[StatusName]","lkpOrderStatusState","[OrderStatusStateID]=" & [txtLatestOrderStatusID])




I'm looking for a simper and efficient way to do this since I'd like to show the status on other various forms.

thanx
 

Answer : Dlookup with multiple criterias and tables

Try:
=(Select lkpOrderStatusState.StatusName From lkpOrderStatusState
Inner Join tblOrderStatusHistory
On lkpOrderStatusState.OrderStatusStateID = tblOrderStatusHistory.OrderStatusStateID
Where StatusHistoryDateStamp = DLookUp("OrderStatusStateID","tblOrderStatusHistory","[OrderID]=" & [Forms]![frmViewOrder]![SubFormOrderStatus].[Form]![txtOrderID] & "And [StatusHistoryDateStamp]= #" & [txtLastUpdated] & "#"))
Random Solutions  
 
programming4us programming4us