Results 1 to 3 of 3
  1. #1
    princeofdumph is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Dec 2011
    Posts
    4

    How to show previous order date

    Hi,



    I have a table named "PurchaseOrders" in which I have stored Vendor Names and Purchase Order Numbers alongwith order dates. There is a form which is based on this table for daily data entry. On the form, I select "For Approval" when I am entering a new purchase order for approval from manager, or "Already Processed" when eventually it is approved by the manager. These two options are contained in a drop-down combo box. From the 'PurchaseOrders' table, i then created a simple query named "qApprovalReport" and from there a report based on it. When the report is run, say the current month March2018, it shows two sections of data. The upper portion shows purchase orders already processed in march2018 while the lower portion shows the new order forms whose approval is required. However, now i want to insert a column in report that, for each purchase order, shows the previous order date of the item described in that order. Below is the output that i desire to have:

    Vendor Name; OrderNo.; OrderDate; ItemOrdered; Rate; Qty; Amount; PreviousOrderDate; Remarks
    ABC 111 1-Mar-18 Pens 26-Feb-18 Already Processed
    LMN 118 2-Mar-18 Cups 28-Feb-18 Already Processed

    XYZ 222 10-Mar-18 Pens 1-Mar-18 For Approval
    PQR 225 11-Mar-18 Cups 2-Mar-18 For Approval



    I had created a separate totals query based on 'Entry' table to pull only item name and order date and select 'Last' in the order date dropdown. However, it shows me the order number and date which I just entered today on the form instead of the previous order date for that item. I would also like two options to be considered: one where an item's previous order date is strictly w.r.t. the same vendor; or secondly, where it is just the item's previous order date irrespective of from which vendor it was purchased.

    I would really appreciate if the solution is possible with simple queries rather than any complex VBA programming. an SQL query would also be good enough for me to apply. Thanx

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    if you are using a combo box, then use 2 combos....
    cbo1 is your current one that shows all dates.
    make cbo2 for the previous, but setting it to show all before the one picked

    select top 1 OrderDate from table where [orderDate]< forms!myForm!cbo1, sort desc

    Note: referesh the cbo2 once cbo1 has changed:
    Code:
    sub cbo1_afterupdate()
       cbo2.requery
    end sub

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I would create a UDF and use the UDF in a query.

    The UDF might be something like
    Code:
    Public Function fGetPrevDate(pItem As String, pDateOrdered As Date, Optional pVendor As String) As Date
        Dim r As DAO.Recordset
        Dim sSQL As String
    
        'default return date
        fGetPrevDate = #1/1/1900#
    
        sSQL = "SELECT TOP 1 OrderDate"
        sSQL = sSQL & " FROM PurchaseOrders"
    
        'include Vendor name?
        If Len(Trim(pVendor & "")) = 0 Then
            sSQL = sSQL & " WHERE [ItemOrdered] = '" & pItem & "' and [OrderDate] < #" & pDateOrdered & "#"
        Else
            sSQL = sSQL & " WHERE [ItemOrdered] = '" & pItem & "' and [OrderDate] < #" & pDateOrdered & "# And [VendorName] = '" & pVendor & "'"
        End If
        sSQL = sSQL & " ORDERBY OrderDate DESC;"
        '  Debug.Print sSQL
    
        CurrentDb.Execute sSQL, dbFailOnError
        If Not r.BOF And Not r.EOF Then
            fGetPrevDate = r("OrderDate")
        End If
    
        r.Close
        Set r = Nothing
    
    End Function
    Place this in a standard module.

    Usage in a query without the Vendor name would look like
    Code:
    PrevDate: fGetPrevDate(ItemOrdered, OrderDate)
    Usage in a query with the Vendor name would look like
    Code:
    PrevDate: fGetPrevDate(ItemOrdered, OrderDate, [Vendor Name])
    Note: you shouldn't use spaces in object names (especially field names)


    If you wanted "Never Ordered" returned and not "1/1/1900", you could try
    Code:
    Public Function fGetPrevDate(pItem As String, pDateOrdered As Date, Optional pVendor As String)
        Dim r As DAO.Recordset
        Dim sSQL As String
    
        'default return value
        fGetPrevDate = "Never Ordered"
    
        sSQL = "SELECT TOP 1 OrderDate"
        sSQL = sSQL & " FROM PurchaseOrders"
    
        'include Vendor name?
        If Len(Trim(pVendor & "")) = 0 Then
            sSQL = sSQL & " WHERE [ItemOrdered] = '" & pItem & "' and [OrderDate] < #" & pDateOrdered & "#"
        Else
            sSQL = sSQL & " WHERE [ItemOrdered] = '" & pItem &  "' and [OrderDate] < #" & pDateOrdered & "# And [VendorName] =  '" & pVendor & "'"
        End If
        sSQL = sSQL & " ORDERBY OrderDate DESC;"
        '  Debug.Print sSQL
    
        CurrentDb.Execute sSQL, dbFailOnError
        If Not r.BOF And Not r.EOF Then
            fGetPrevDate = r("OrderDate")
        End If
    
        r.Close
        Set r = Nothing
    
    End Function



    BTW.... this code is air code - untested!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 04-29-2016, 04:03 AM
  2. Replies: 3
    Last Post: 02-09-2016, 04:36 PM
  3. Replies: 1
    Last Post: 06-02-2015, 10:07 AM
  4. not to show previous-next
    By AccessIgnorant in forum Forms
    Replies: 3
    Last Post: 12-05-2011, 11:04 PM
  5. Automatically show previous dates.
    By mbonning in forum Access
    Replies: 6
    Last Post: 09-12-2011, 12:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums