Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    EniGma19 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    7

    looping through recordsets to find the latest field value

    Hi ,



    I need help to solve this problem

    Problem definition:

    I have a table 'tblItemOrdQty' with item name and its order quantities (Lot1,Lot2,Lot3.... etc). Each items are ordered in different lots. Some are ordered as single lot.. some in two lots.. some in three lots etc.
    My objective is to have a new column ( a kind of calculated field) with the value of latest order quantity for each item.

    Eg.

    Item ID | Item Name | Lot1 | Lot2 | Lot3| Latest Qty (calc.field)

    1 | Item1 |10| 11 |14 | 14
    2 | Item2 |5 | 4 |null | 4
    3 | Item3 |20|null| null | 20


    I have made a query 'ItemOrdQuery'with Item ID, Item name, Lot 1 , Lot 2, Lot 3. I need to know how to build expression for the Latest Qty calculated field for my desired objective.

    Is there any way it can be done using SQL or VBA program using recordsets?

    Waiting for your reply.

    Thank you for your time.

  2. #2
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    What do you mean by "latest?"

    When and where will you use the "calculated field?"

    Think things through very carefully and then think some more before you introduce calculated fields into your database.

    Actually the proper (and easiest) structure is the normalised solution where there is a table for items, a table for lots and a table for orders (or perhaps deliveries).

  3. #3
    EniGma19 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    7
    Hi Rod,

    Thank you for your response.
    By "latest" i mean the current lot quantity. Check out the example i provided.
    For Item 1 the current lot Qty is Lot3=14.
    For Item 2 the current lot Qty is Lot2=4
    For Item 3 the current lot Qty is Lot1=20

    "Null" means that the item is yet to be ordered in the corresponding Lots.

    I need the calculated field to display the current lot quantity.
    I then need to use this calculated field (Current lot Quantity) in a query to get the difference between the Total Quantity required and Total quantity ordered.

    I cannot use seperate table for item name,lot quantities etc because i recieve this table as input from another team and need to import it into access and proceed.
    I have jus given a example of what i need. The real data is very huge and it is difficult for me to split into different tables.

  4. #4
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Sorry to be pedantic; by "current" you seem to mean: the quantity in column 3 if not null; otherwise the quantity in column 2 if not null; otherwise the quantity in column 1 if not null; otherwise zero. Is this correct? Are there only three columns or by 'huge' do you mean the number of columns? What are the column names?

    With three or less columns you can handle this with a nested Iif statement; for more than three the nesting gets a bit of a nightmare and a user written function may be better. You can perform the netting at the same time.

    Once you reply, we'll have a go at the specifics.

  5. #5
    EniGma19 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    7
    Rod,

    I will try to be more clear this time. The items are ordered in a number of Lots. For understanding purpose, let us assume that Lot1 is the Qty ordered in Jan, Lot 2 is Qty ordered in Feb and Lot3 is Qty ordered in Mar.

    By current i mean that
    For Item 1 the latest order is done on March i.e Lot 3= 14
    For Item 2 the latest order is done on Feb i.e Lot 2= 4
    For Item 1 the latest order is done on Jan i.e Lot 1= 20

    My calculated field (Latest Qty) should display

    14
    4
    20


    My table has about 120 items with upto 10 lot numbers i.e Lot1, Lot2, Lot3,..... Lot12)

  6. #6
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    OK, forget nested Iif statements and let's concentrate on a couple of user functions written in VBA.

    1. The first function will return the latest lot quantity for a specific item. It will also tell you which lot is the latest.
    2. The second (and much more complex) function will build a new table with an additional column for the latest lot quantity.


    Notes:

    • It will help if you can give me the table design - i.e. field/attribute names, data types, etc.
    • I need to know how to identify the names of the lot quantity columns. Presumably they are 'Lot1, Lot2, Lot3, ..' or even better 'Lot01, Lot02, Lot03, ...'
    • I predict a function that builds a new table will be much faster than complex SQL.
    • I shall place the functions in a VBA module and if necessary give you instructions on how to incorporate it in your Access db.


    This will take a little while so I'll start now without awaiting your response.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849

  8. #8
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Hi Orange,

    In Enigma's defense, he has informed us he receives the table (I suspect a copy of a spreadsheet) and can do nothing about it.

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849

  10. #10
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Hi EniGma,

    Here we go! I've written three routines, all contained in a standard module called ItemOrdQtyManager that is attached here as a zip file.

    ItemOrdQtyManager.zip

    To include this in your database do the following:

    1. Unzip the file; it then has a .bas extension.
    2. Go to the VBA coding window (Alt+F11).
    3. Right-click in the upper left-hand pane.
    4. Select Import File...
    5. Browse until you can select the unzipped ItemOrdQtyManager.bas in your directories.
    6. Click Open.


    Here are some comments on the routines. You must however change the code to suit your own names if they differ from my assumptions. Further the module contains no error handling; this is something that should be included as the implementation is 'refined.'

    Public Function SQLTest(rlngItemId As Long) As Long

    I wrote this as an afterthought to test whether my techniques would work within SQL. They do but the routine is likely to prove slow for large data. However 120 records is hardly 'large' and I suspect this is the solution you will prefer which is why I'm describing it first. Here is a sample of SQL that shows how to use it.

    SELECT tblItemOrdQty.*, SQLTest([ItemID]) AS LatestQty FROM tblItemOrdQty;

    Public Sub TransposeTable(rstrTableNameIn As String)

    This routine creates a new table with a suffix appended to the name of the original table. The sufix is currently 'Ext' but this can be changed in the declarations section of the module. Because I suspect the number of 'lot' columns may vary, the routine first deletes any existing table and recreates the new table definition from the source table adding a field for LatestQty. (The name of the additional column may be changed in the declarations section of the module.)

    I have been crude when copying field specifications limiting the copied properties to name and data type only. There may be other properties (and perhaps indices) that should be copied but I have not seen your table design and therefore do not know.

    The routine needs to be invoked (called) somehow. The usual method is to do this from the Click event of a command button. The code behind the event will be similar to the following.

    Code:
    Private Sub Command0_Click()
        ItemOrdQtyManager.TransposeTable "tblItemOrdQty"
    End Sub
    This to my mind is the preferred generalised solution, especially for large numbers of records (i.e. thousands!)

    Public Function GetLatestQuantity(rstrTableNameIn As String, _
    rlngItemId As Long, _
    rstrFieldNameOut As String) As Long

    Finally, to complete the set, this function returns the latest lot quantity for a single item and indicates which lot is the latest. It is not intended for mass use but for individual enquiries should the need arise. You must supply a string variable for the column name but it may be empty; the procedure places the column name in this variable.

    Here's an example of the 'calling' code. The MsgBox statement is there simply for my testing purpose.

    Code:
    Private Sub Command1_Click()
        Dim lngQty As Long
        Dim strFieldName As String
        lngQty = ItemOrdQtyManager.GetLatestQuantity("tblItemOrdQty", 101, strFieldName)
        MsgBox lngQty & " " & strFieldNam
    End Sub

  11. #11
    EniGma19 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    7
    Hi Rod,

    Thank you very much for your reply.

    I will follow your instructions and let you know about the results.

    I have attached my database with table and a query. My exact objective is to have a query of the table with a calculated field that displays the latest quantity. I can then use this field in another query to find the difference between the Quantity required and Quantity ordered.
    Attached Files Attached Files

  12. #12
    EniGma19 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    7
    Hi Orange,

    Thank you for the article. Very useful for a beginner like me.

  13. #13
    EniGma19 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    7
    Hi Rod,

    I followed your instruction step by step.I am not able to import the .bas file. Once i clike the Open, there is a empty VB screen.

    Help!!

  14. #14
    EniGma19 is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2012
    Posts
    7
    Hi Rod,

    I was able to open it now. The code looks very complex for a beginner like me. I will try to understand it.

    My friend ( in another forum) had given me a code as follows


    1. Private Sub CalculateLast()
    2. Dim i As Integer
    3. Dim varFieldValue As Variant
    4. Dim strFieldName As String
    5. 'Open Recordset
    6. Dim rs As DAO.Recordset
    7. Set rs = CurrentDb.OpenRecordset("tblItemOrdQty", dbOpenDynaset)
    8. If rs.EOF Then GoTo exitSub
    9. 'Loop through records
    10. Do While Not rs.EOF
    11. 'Reset storage
    12. varFieldValue = Null
    13. 'Loop through fields in reverse
    14. For i = 10 To 0 Step -1
    15. strFieldName = "r" & Format(i, "00") & " Qty"
    16. If Not IsNull(rs(strFieldName)) Then
    17. varFieldValue = rs(strFieldName)
    18. 'Found a value, so exit loop
    19. Exit For
    20. End If
    21. Next
    22. 'Write calculated value to field
    23. rs.Edit
    24. rs("RLastQty") = varFieldValue
    25. rs.Update
    26. 'Move to next record
    27. rs.MoveNext
    28. Loop
    29. exitSub:
    30. 'Cleanup objects
    31. rs.Close
    32. Set rs = Nothing
    33. End Sub




    This code seems to be working for me. But, it requires a form and a command button to trigger this module. I want this as a query with a calculated field without the need for forms.

    It requires some SQL programming i believe. Can u help me to do this??

    Thanks

  15. #15
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    I have mimiced what you should be doing and have no problem. Before we do it another way, make sure you click on the '+' sign alongside Modules to expand the list, then double click on ItemOrdQtyManager.

    PS Oops, your latest post suddenly appeared, so ignore this.
    Last edited by Rod; 10-23-2012 at 05:26 AM. Reason: Add PS

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 08-24-2012, 12:04 PM
  2. Replies: 2
    Last Post: 06-13-2012, 06:00 PM
  3. Code to display latest record number field created?
    By rowardHoark in forum Programming
    Replies: 1
    Last Post: 01-31-2011, 08:03 AM
  4. Looping through a tbl to find a certain value
    By cwf in forum Programming
    Replies: 1
    Last Post: 05-17-2010, 04:02 PM
  5. Query to find latest date
    By Lockrin in forum Access
    Replies: 2
    Last Post: 12-16-2009, 10:00 AM

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