allocation function within a query

G

Guest

I have a query I am using to pull available inventory from a warehouse to
replenish 30+ trucks. The query pulls the quantity required on each truck
as well as the avaible quantity in the warehouse.

How do I create a query or some programming that will allocate inventory as
in the example below:

Warehouse has 20 of part A

Truck 1 needs 5 of Part A -- Query allocates 5 of Part A to truck 1
Truck 2 needs 7 of Part A --- Query allocates 7 of Part A to Truck 2
Truck 3 needs 12 of part A -- Query allocates remainder - 8 of Part A to
truck 3
Truck 4 needs 3 of Part A -- Query allocates 0 of Part A to Truck 4 as
their are none left

Any help would be much appreciated!!
 
P

pietlinden

Jason said:
I have a query I am using to pull available inventory from a warehouse to
replenish 30+ trucks. The query pulls the quantity required on each truck
as well as the avaible quantity in the warehouse.

How do you determine which truck to start with? Does it matter?
 
P

pietlinden

could you post your structure for the delivery table?
You're delivering various quantities of multiple parts, right?
 
G

Guest

The structure is rather lengthy but the table lists the following:

Truck #, Part #, Qty Required of that part by that truck, and Qty Available
to Pull from

Most of the trucks require the same parts so I need to figure out how to
cycle through the parts until the Qty Available to Pull goes to 0.
 
P

pietlinden

Jason said:
The structure is rather lengthy but the table lists the following:

Truck #, Part #, Qty Required of that part by that truck, and Qty Available
to Pull from

Most of the trucks require the same parts so I need to figure out how to
cycle through the parts until the Qty Available to Pull goes to 0.

How do you know what Qty Available is? Isn't this calculated on the
fly? Check out Allen Browne's QOH/Inventory solution here:
http://www.allenbrowne.com/AppInventory.html

Also, there's an example of how to handle this type of situation in
Access Cookbook, by Getz and a few others. (see Amazon or the
bookstore).

here's my first try... I'm sure I'm not doing it right, but in the
absence of any other answers...

Option Compare Database
Option Explicit

Public Sub RunAllocation()
'Get list of items to allocate - but only those that have any
quantity in inventory.
Dim rs As DAO.Recordset

Set rs = DBEngine(0)(0).OpenRecordset("qtot_CurrentQOH")
'SQL:
'SELECT Inventory.ItemID, Inventory.QOH AS InitialQOH,
[QOH]-nz([SumOfQtyAllocated]) AS QOH_Current
'FROM Inventory LEFT JOIN qtotQtyAllocated ON Inventory.ItemID =
qtotQtyAllocated.ItemID
'WHERE (((Inventory.ItemID) In (SELECT ItemID FROM Delivery WHERE
DateAllocated IS NULL)));

Do Until rs.EOF
Debug.Print "Allocating " & rs.Fields("ItemID") &
rs.Fields("QOH_Current")
Allocate rs.Fields(0)
Debug.Print "Finished " & rs.Fields("ItemID") & ": " &
rs.Fields("QOH_Current")
Debug.Print
rs.MoveNext
Loop
rs.Close
Set rs = Nothing

End Sub

Public Sub Allocate(ByVal strItem As String)
'--this is probably the most roundabout way of doing this, but I
couldn't figure out how to do
'==it any other way. No, it's all my own code. So any mistakes are
mine.

Dim db As DAO.Database
Dim rsD As DAO.Recordset '--Deliveries with Inventory in first
record.
Dim intQOH As Integer
Dim intAllocated As Integer
Dim intQtyNeeded As Integer
Dim strSQL As String

strSQL = "SELECT Inventory.ItemID, Delivery.OrderDate,
Inventory.QOH, Delivery.TruckID, Delivery.QtyNeeded,
Delivery.QtyAllocated, Delivery.DateAllocated"
strSQL = strSQL & " FROM Delivery INNER JOIN Inventory ON
Delivery.ItemID = Inventory.ItemID"
strSQL = strSQL & " WHERE (((Inventory.ItemID) = '" & strItem & "')
And ((Delivery.DateAllocated) Is Null))"
strSQL = strSQL & " ORDER BY Inventory.ItemID, Delivery.OrderDate;"


Set db = CurrentDb
'clear out old delivery info
'db.Execute "UPDATE Delivery SET QtyAllocated=0, DateAllocated=Null
WHERE DateDelivered IS NULL;", dbFailOnError

Set rsD = db.OpenRecordset(strSQL)
'rsD.MoveFirst
intQOH = rsD.Fields("QOH")
Debug.Print "Initial QOH: " & intQOH
Do Until rsD.EOF Or intQOH = 0
rsD.Edit

'--If there is Qty in inventory to fill the order, fill it,
'--otherwise, don't fill it.
intQtyNeeded = rsD.Fields("QtyNeeded")
If intQOH >= rsD.Fields("QtyNeeded") Then
intAllocated = rsD.Fields("QtyNeeded")
intQOH = intQOH - intAllocated
rsD.Fields("QtyAllocated") = rsD.Fields("QtyNeeded")
Else
intAllocated = intQOH
rsD.Fields("QtyAllocated") = intAllocated
intQOH = 0
End If

rsD.Fields("DateAllocated") = Now
'---this is just feedback for me...
Debug.Print "Needed: " & intQtyNeeded, "Allocated: " &
intAllocated, "QOH: " & intQOH
rsD.Update
rsD.MoveNext



Loop

rsD.Close
Set rsD = Nothing
Set db = Nothing
End Sub


NOTE: Do NOT try this on live data. It WILL screw up your data.

What it does...
1. loops through all the items that have corresponding "orders" and
gets a total on hand.
2. processes each of those items and allocates inventory in date
sequence. Any items that are out of inventory will not get allocated.
(but remainders will). So some orders may be short. (Since the query
for Qty is based on Ordered - Delivered, you should be OK.)

But I'd get a real expert's opinion. I'm just a hack.

Pieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top