What I am trying to do is material allocation. There are structures.Each structure needs different types of material to be fabricated. This info is in a table as below.
Structure ID--Material Code--Required Qty--Status
ST1 -------------MAT2---------- 3---- ---AVAILABLE
ST1 -------------MAT3-----------3-------AVAILABLE
ST2--------------MAT1-----------5------ NOT AVAILABLE
ST3--------------MAT3-----------2------- NOT AVAILABLE
And I have a query holds the stock qty
Material Code----Stock Qty
MAT1------------ 4
MAT2------------ 2
MAT3------------4
Below code should check each row in Structure table, if quantity is less than or equal to stock qty, Status should be AVAILABLE and it should deduct the required quantity from stock quantity to calculate next row correctly. But is not working. It calculates First items correctly till they get NOT AVAILABLE then it says for all items NOT AVAILABLE even they are AVAILABLE. I guess recordset may stuck somewhere?
Private Sub RunAllocation_Click()
Dim db As DAO.Database
Dim STOCK As DAO.Recordset
Dim WP As DAO.Recordset
Dim VirtualStock As Double
Set db = CurrentDb()
Set STOCK = db.OpenRecordset("InventoryTotalQ") 'STOCK QUERY
Set WP = db.OpenRecordset("WorkPack") 'STRUCTURE TABLE
VirtualStock = STOCK!Stock_Qty 'VIRTUAL STOCK TO DEDUCT REQUIRED FROM STOCK QUANTITY
If STOCK.RecordCount = 0 Then Exit Sub
STOCK.MoveFirst
Do Until STOCK.EOF
If WP.RecordCount = 0 Then Exit Sub
WP.MoveFirst
Do Until WP.EOF
If STOCK!Part_No = WP!Part_No Then 'IF MATERIAL EXIST IN STOCK START TO CHECK QUANTITY
If VirtualStock >= WP!Required Then 'IF STOCK QTY IS BIGGER THAN REQUIERD QTY.
WP.Edit
WP![Status] = "AVAILABLE"
WP.UPDATE
VirtualStock = VirtualStock - WP!Required 'REDUCE REQUIRED QTY. FROM STOCK TO CALCULATE NEXT ROW AS PER CORRECT VALUES
Else
WP.Edit
WP![Status] = "NOT AVAILABLE"
WP.UPDATE
End If
End If
WP.MoveNext
Loop
STOCK.MoveNext
Loop
STOCK.Close
WP.Close
Set STOCK = Nothing
Set WP = Nothing
Set db = Nothing
End Sub
Structure ID--Material Code--Required Qty--Status
ST1 -------------MAT2---------- 3---- ---AVAILABLE
ST1 -------------MAT3-----------3-------AVAILABLE
ST2--------------MAT1-----------5------ NOT AVAILABLE
ST3--------------MAT3-----------2------- NOT AVAILABLE
And I have a query holds the stock qty
Material Code----Stock Qty
MAT1------------ 4
MAT2------------ 2
MAT3------------4
Below code should check each row in Structure table, if quantity is less than or equal to stock qty, Status should be AVAILABLE and it should deduct the required quantity from stock quantity to calculate next row correctly. But is not working. It calculates First items correctly till they get NOT AVAILABLE then it says for all items NOT AVAILABLE even they are AVAILABLE. I guess recordset may stuck somewhere?
Private Sub RunAllocation_Click()
Dim db As DAO.Database
Dim STOCK As DAO.Recordset
Dim WP As DAO.Recordset
Dim VirtualStock As Double
Set db = CurrentDb()
Set STOCK = db.OpenRecordset("InventoryTotalQ") 'STOCK QUERY
Set WP = db.OpenRecordset("WorkPack") 'STRUCTURE TABLE
VirtualStock = STOCK!Stock_Qty 'VIRTUAL STOCK TO DEDUCT REQUIRED FROM STOCK QUANTITY
If STOCK.RecordCount = 0 Then Exit Sub
STOCK.MoveFirst
Do Until STOCK.EOF
If WP.RecordCount = 0 Then Exit Sub
WP.MoveFirst
Do Until WP.EOF
If STOCK!Part_No = WP!Part_No Then 'IF MATERIAL EXIST IN STOCK START TO CHECK QUANTITY
If VirtualStock >= WP!Required Then 'IF STOCK QTY IS BIGGER THAN REQUIERD QTY.
WP.Edit
WP![Status] = "AVAILABLE"
WP.UPDATE
VirtualStock = VirtualStock - WP!Required 'REDUCE REQUIRED QTY. FROM STOCK TO CALCULATE NEXT ROW AS PER CORRECT VALUES
Else
WP.Edit
WP![Status] = "NOT AVAILABLE"
WP.UPDATE
End If
End If
WP.MoveNext
Loop
STOCK.MoveNext
Loop
STOCK.Close
WP.Close
Set STOCK = Nothing
Set WP = Nothing
Set db = Nothing
End Sub
Last edited: