Solved How to grab the last updated record in a table

nector

Member
Local time
Tomorrow, 00:06
Joined
Jan 21, 2020
Messages
452
I have two forms, one the parent form also called Invoice header, and the sub form referred as detail Ines. Now when users are entering data in the sub form the form calculate also the remaining balance of the stock being sold, so in short, the running balances per each product latest balance is automatically enter in the field called currentstock.

Now I want to make a report which will show the latest balance per each product shown also on the invoice detail table line report so that the salespeople are able to see the current stock position by just looking at the invoice.

Challenges

Sometimes the salespeople will capture the same product may be four times and each line will show the current stock balance, but on the report, I Just want the last lines per each product to show on the report so that it's easier to read and understand, Example if Mango Juice has ten lines the report should not show all the ten line with balances but only the last line captured with the latest stock balance.

Below is a code I use to help capture the balances:

Code:
Private Sub ProductID_AfterUpdate()
Dim db As Database
Dim rs As Recordset
Dim strSQL As String
Dim qdf As DAO.QueryDef
Dim prm As DAO.Parameter
Set db = CurrentDb
Set qdf = db.QueryDefs("QryinvoiceResidualBalance")
For Each prm In qdf.Parameters
prm = Eval(prm.Name)
Next prm
Set qdf = Nothing
strSQL = "SELECT Nz(Sum(StockBalance),0)As Balance FROM [QryinvoiceResidualBalance] Where [ProductID] =" & Me.ProductID & "AND [WHID] =" & Me.Parent!WareHouse
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
Me.CurrentStock = Nz(rs!Balance, 0)
rs.Close
Set rs = Nothing
Set db = Nothing
Set qdf = Nothing
Set prm = Nothing
End Sub

Private Sub Quantity_AfterUpdate()
Me.CurrentStock = Nz(Me.CurrentStock, 0) - Nz(Me.Quantity, 0)
Me.Requery
DoCmd.GoToRecord , , acNewRec
End Sub


The code above is faster in terms of capturing data. Any idea about how to go about that? All the two tables have indexes
 
If you want to rely on autonumber field always increasing with each new record, use that field to pull the 'last' record. Build aggregate query that returns the Max(ID) for each product then join that query to original table. If you need more assistance, provide sample data or even attach file to post.

SQL constructed in VBA as posted will fail due to lack of space in front of AND.

Or use a couple of domain aggregate function expressions - DMax and DLookup.

As for saving balances at all, review http://allenbrowne.com/AppInventory.html
 
Last edited:
Okay many thanks here are the small database attached, all what is required is to grab the following:

Latest Closing Balance:

(1) Mango Juice = 7
(2) Orange Juice = 20
(3) Pineapple Mix = 5

My query is also there to easy the checking
 

Attachments

I don't think it is possible to keep track of stock in the sales lines. If you have received stock of that item after the last sale of an item, the stock in the sales line is no longer correct.
You can calculate the stock at any time by adding all incoming and outgoing changes.
 
Similar to the balance of your bank account, you should never store the current quantity of your stock. Rather, you should always calculate stock levels by summing the deposits and withdrawals over time. You should have an tOrder table with a date, and links to a Customer and/or Supplier. You should also have a tOrderDetail table, where each row links to an order, a product, and contains a Quantity and Price. Stock is then calculated by querying a ProductID from tOrderDetail joined to tOrder to sum the Quantity field for dates <= tOrder.Date. This allows you to calculate a stock level for any product on any date, including into the future.
 
Okay many thanks here are the small database attached, all what is required is to grab the following:

Latest Closing Balance:

(1) Mango Juice = 7
(2) Orange Juice = 20
(3) Pineapple Mix = 5

My query is also there to easy the checking


You might benefit from studying the way inventory is handled in the Northwind Developer Edition Template.

Calculated balances should not be stored, as others have previously explained.

You need to calculate and display Stock On Hand, or Inventory Balance in queries.
 
Similar to the balance of your bank account, you should never store the current quantity of your stock. Rather, you should always calculate stock levels by summing the deposits and withdrawals over time. You should have an tOrder table with a date, and links to a Customer and/or Supplier. You should also have a tOrderDetail table, where each row links to an order, a product, and contains a Quantity and Price. Stock is then calculated by querying a ProductID from tOrderDetail joined to tOrder to sum the Quantity field for dates <= tOrder.Date. This allows you to calculate a stock level for any product on any date, including into the future.
Well noted
 
i am not sure about this, to always calculate from the "ins" and "outs".
i always calculate it from the "last inventory" + "ins" (after inventory) - "out" (after inventory).
 
i am not sure about this, to always calculate from the "ins" and "outs".
i always calculate it from the "last inventory" + "ins" (after inventory) - "out" (after inventory).
This is the way Northwind Developer Edition handles current inventory. It's referred to as "a stock take". Another advantage of this approach is that it helps account for shrinkage, which is the problem of stock "going missing" after the last inventory, or stock take. Shrinkage due to breakage, theft, or other mishap is very common.
 

Users who are viewing this thread

Back
Top Bottom