Private Sub Command88_Click()
On Error GoTo Err_Command88_Click
'This code is to assign FG stock to orders
Dim db As DAO.Database
Dim db1 As DAO.Database
Dim rst As DAO.Recordset
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim strSelect As String
Dim strOrder As String
Dim strWhere As String
Dim strSql As String
Dim strSelect1 As String
Dim strOrder1 As String
Dim strWhere1 As String
Dim strSql1 As String
Dim intFG As Integer
Dim intInsp As Integer
Dim intHold As Integer
Dim intOffSite As Integer
Dim intRaw As Integer
Dim Scrap As Double
Dim stDocName As String
Dim strTxt As String
Dim dbmsg As DAO.Database
Dim msgrst As DAO.Recordset
Dim strmsgSql As String
Dim stLinkCriteria As String
Dim intRecNo As Integer
Dim intStock As Integer
Dim intDemand As Integer
Dim strStockFlag As String
y = 1
strTxt = "Reviewing the order lines covered by stock"
'Go to msgform sub
Debug.Print "goto msgform"
Call MsgFormSub(strTxt)
Debug.Print "return msgform"
'Stage 1 create the order demand table
stDocName = "QrySalesOrders"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Debug.Print "Sales Demand Table Written"
'Stage 2a load the FGparts on order
strSql = "SELECT dbo_CHCIW_AllStock.Stockcode, dbo_CHCIW_AllStock.FinishedQty, dbo_CHCIW_AllStock.Comp FROM dbo_CHCIW_AllStock where (((dbo_CHCIW_AllStock.FinishedQty)>0)) or ((dbo_CHCIW_AllStock.Comp)>0) order by dbo_CHCIW_AllStock.Stockcode "
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSql, dbOpenDynaset)
'find number of records
rst.MoveFirst
rst.MoveLast
strTxt = "2a - The number of parts with stock are " & rst.RecordCount & " to review"
Debug.Print strTxt
'Go to msgform sub
Debug.Print "2b-goto msgform"
Call MsgFormSub(strTxt)
Debug.Print "2c-return msgform"
rst.MoveFirst
With rst
While Not .EOF
Debug.Print rst!StockCode; " FG="; rst!FinishedQty; " Comp="; rst!Comp
rst.MoveNext
Wend
Debug.Print "2d-End of records"
rst.MoveFirst
While Not rst.EOF
'Stage 2b load the FG stock on for !MStockCode
'This needs to go in loop
strSelect = "SELECT dbo_CHCIW_AllStock.Stockcode, dbo_CHCIW_AllStock.FinishedQty, dbo_CHCIW_AllStock.Comp FROM dbo_CHCIW_AllStock "
strWhere = "Where (dbo_CHCIW_AllStock.Stockcode = '" & RTrim(rst!StockCode) & "')"
strSql = strSelect + strWhere
Debug.Print "2e-"; strSql
Set rst1 = db.OpenRecordset(strSql, dbOpenDynaset)
intFG = rst1!FinishedQty + rst1!Comp
Debug.Print "2f-"; rst1!StockCode; " FG Stock=" & intFG
'Stage 2c - Load the sales order for MStockCode
strSelect = "SELECT TblSalesDemand.MStockCode, TblSalesDemand.MLineShipDate, TblSalesDemand.MBackOrderQty, TblSalesDemand.FG, TblSalesDemand.StockFlag, TblSalesDemand.Priority FROM TblSalesDemand "
strOrder = "ORDER BY TblSalesDemand.MStockCode, TblSalesDemand.MLineShipDate "
strWhere = "Where (((TblSalesDemand.MStockCode) ='" & RTrim(rst1!StockCode) & "'))"
strSql = strSelect + strWhere + strOrder
Debug.Print "2g-"; strSql
Set rst2 = db.OpenRecordset(strSql, dbOpenDynaset)
With rst2
'find number of records
If rst2.RecordCount > 0 Then rst2.MoveFirst
If rst2.RecordCount > 0 Then rst2.MoveLast
If rst2.RecordCount > 0 Then strTxt = "2h-The number order lines are " & rst2.RecordCount & " to review" Else
strTxt = "2h-There are no order lines to review " & rst2.RecordCount
Debug.Print strTxt
intRec = rst2.RecordCount
If rst2.RecordCount > 0 Then rst2.MoveFirst
While Not rst2.EOF
Debug.Print RTrim(rst2!MStockCode); " "; rst2!MLineShipDate; " "; rst2!MBackOrderQty; " FG Stock "; rst2!FG; " Flag "; rst2!StockFlag
If rst2.RecordCount > 0 Then rst2.MoveNext
Wend
Debug.Print "2i- End of records"
If rst2.RecordCount > 0 Then rst2.MoveFirst
'consider the stock
For i = 0 To (intRec)
If rst2.RecordCount > 0 Then
rst2.Edit
If intFG >= rst2!MBackOrderQty Then rst2!StockFlag = "G"
If intFG >= rst2!MBackOrderQty Then rst2!FG = rst2!MBackOrderQty
If intFG < rst2!MBackOrderQty Then rst2!StockFlag = "R"
If intFG < rst2!MBackOrderQty Then rst2!FG = intFG
Debug.Print "2j"; i; " stock calc="; intFG; " - "; rst2!MBackOrderQty; " = "; intFG - rst2!MBackOrderQty
rst2.Update
If intFG - rst2!MBackOrderQty > 0 Then intFG = intFG - rst2!MBackOrderQty Else intFG = 0
Debug.Print "2j"; i; " "; rst2!MLineShipDate; " "; rst2!MBackOrderQty; " FG Stock "; rst2!FG; " Flag "; rst2!StockFlag; " remaining FG Stock "; intFG
If intFG = 0 Then rst2.MoveLast
If intFG > 0 Then rst2.MoveNext
End If
i = i + 1
'MsgBox (RTrim(rst!Stockcode) & " " & rst2.EOF & " " & rst2.RecordCount) & " i= " & i
Next i
Debug.Print "2K - Order lines Now Considered for "; rst!StockCode
End With
Debug.Print "2l - Move to next stock code"
rst.MoveNext
y = y + 1
strTxt = "2m-record " & y & " out of " & rst.RecordCount & " reviewed"
Debug.Print strTxt
'Go to msgform sub
Debug.Print "2b-goto msgform"
Call MsgFormSub(strTxt)
Debug.Print "2c-return msgform"
Wend
End With
'Stage 3 Consider the CA stock against the orders remember ca parts can have more than one FG
y = 1
'stage 3a find the ca parts with orders not covered by FG stock
strSql = "SELECT TblSalesDemand.Component FROM TblSalesDemand WHERE (((TblSalesDemand.MBackOrderQty) > 0)) GROUP BY TblSalesDemand.Component"
Set rst = db.OpenRecordset(strSql, dbOpenDynaset)
rst.MoveLast
rst.MoveFirst
strTxt = "Stage 3 - There are " & rst.RecordCount & " CA records to evaluate"
'MsgBox sqlTxt
'MsgBox strTxt
Debug.Print strTxt
'Go to msgform sub
Debug.Print "3-goto msgform"
Call MsgFormSub(strTxt)
Debug.Print "3-return msgform"
'stage 3b load the ca stock
intRecNo = 1
While Not rst.EOF
sqlSelect = "SELECT dbo_CHCIW_AllStock.CAStockCode, dbo_CHCIW_AllStock.Problem, dbo_CHCIW_AllStock.Hold, dbo_CHCIW_AllStock.Insp, dbo_CHCIW_AllStock.SCret, dbo_CHCIW_AllStock.Offsite, dbo_CHCIW_AllStock.SCtogo, dbo_CHCIW_AllStock.ToFettle, dbo_CHCIW_AllStock.Scun FROM dbo_CHCIW_AllStock"
sqlWhere = " WHERE (((dbo_CHCIW_AllStock.CAStockCode) = '" & RTrim(rst!Component) & "'))"
strSql = sqlSelect + sqlWhere
'MsgBox (strSql)
Set rst1 = db.OpenRecordset(strSql, dbOpenDynaset)
Debug.Print "Stock info: " & RTrim(rst1!CAStockCode) & " Problem=" & rst1!Problem & " Hold=" & rst1!Hold & " Insp=" & rst1!Insp & " SCret=" & rst1!SCret & " Offsite=" & rst1!Offsite & " SCtogo=" & rst1!SCtogo & " ToFettle=" & rst1!ToFettle & " Scun=" & rst1!Scun
'stage 3c - load the order rs for the ca parts
sqlSelect = "SELECT TblSalesDemand.Component, TblSalesDemand.MLineShipDate, TblSalesDemand.MBackOrderQty, TblSalesDemand.FG, TblSalesDemand.Problem, TblSalesDemand.Hold, TblSalesDemand.Insp, TblSalesDemand.SCret, TblSalesDemand.Offsite, TblSalesDemand.SCtogo, TblSalesDemand.Raw, TblSalesDemand.Scun, TblSalesDemand.StockFlag FROM TblSalesDemand "
sqlWhere = "WHERE (((TblSalesDemand.MBackOrderQty) > 0)and ((TblSalesDemand.Component) = '" & RTrim(rst!Component) & "'))"
sqlOrder = " ORDER BY TblSalesDemand.MLineShipDate, TblSalesDemand.MBackOrderQty "
strSql = sqlSelect + sqlWhere + sqlOrder
Debug.Print sqlTxt
'MsgBox (strSql)
Set rst2 = db.OpenRecordset(strSql, dbOpenDynaset)
rst2.MoveFirst
rst2.MoveLast
MsgBox ("rst2 records" & rst2.RecordCount)
With rst2
.MoveFirst
For i = 1 To .RecordCount
Debug.Print i & " " & RTrim(rst2!Component) & " Date " & rst2!MLineShipDate & " Qty " & rst2!MBackOrderQty & "FG= " & rst2!FG & " Problem=" & rst2!Problem & " Hold= " & rst2!Hold & " Insp= " & rst2!Insp & " Scret=" & rst2!SCret & "Offsite= " & rst2!Offsite & " Sctogo =" & rst2!SCtogo & " Raw= " & rst2!Raw & " Scun =" & rst2!Scun & "StockFlag =" & rst2!StockFlag
.MoveNext
Next i
rst2.MoveFirst
End With
strTxt = "Stage 3c - Evaluating record " & intRecNo & " out of " & rst.RecordCount & " CA records, StockCode: " & RTrim(rst!Component)
'Debug.Print strTxt
'Go to msgform sub
'Debug.Print "3-goto msgform"
Call MsgFormSub(strTxt)
Debug.Print "3-return msgform"
'Stage 3d start a loop for each order line to see if covered by stock
For y = 1 To 8
strStockFlag = ""
Select Case AssignStock
Case y = 1
intStock = rst1!Insp
intDemand = rst2!MBackOrderQty - rst2!FG
strStockFlag = "A"
Case y = 2
intStock = rst1!Hold
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp
strStockFlag = "B"
Case y = 3
intStock = rst1!Problem
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold
strStockFlag = "C"
Case y = 4
intStock = rst1!SCret
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem
strStockFlag = "D"
Case y = 5
intStock = rst1!Offsite
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem - rst1!SCret
strStockFlag = "E"
Case y = 6
intStock = rst1!SCtogo
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem - rst1!SCret - rst1!Offsite
strStockFlag = "F"
Case y = 7
intStock = rst1!ToFettle
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem - rst1!SCret - rst1!Offsite - rst1!SCtogo
strStockFlag = "G"
Case y = 8
intStock = rst1!Scun
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem - rst1!SCret - rst1!Offsite - rst1!SCtogo - rst1!ToFettle
strStockFlag = "H"
End Select
Debug.Print "loop number y= " & y; " Demand =" & intDemand & " Stock=" & intStock & " Flag=" & strStockFlag
intSelDemFlag = 0
Do While Not rst2.EOF
rst2.Edit
If intSelDemFlag > 0 Then 'decides if the intDemand formula should take into account remaining stock
Select Case AssignDemand
Case y = 1
intDemand = rst2!MBackOrderQty - rst2!FG
Case y = 2
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp
Case y = 3
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold
Case y = 4
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem
Case y = 5
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem - rst1!SCret
Case y = 6
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem - rst1!SCret - rst1!Offsite
Case y = 7
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem - rst1!SCret - rst1!Offsite - rst1!SCtogo
Case y = 8
intDemand = rst2!MBackOrderQty - rst2!FG - rst1!Insp - rst1!Hold - rst1!Problem - rst1!SCret - rst1!Offsite - rst1!SCtogo - rst1!ToFettle
End Select
End If
If intDemand > intStock Then
If y = 1 Then rst2!Insp = intStock
If y = 2 Then rst2!Hold = intStock
If y = 3 Then rst2!Problem = intStock
If y = 4 Then rst2!SCret = intStock
If y = 5 Then rst2!Offsite = intStock
If y = 6 Then rst2!SCtogo = intStock
If y = 7 Then rst2!Raw = intStock
If y = 8 Then rst2!Scun = intStock
y = 8 ' Set y to move onto next stock level
rst2.MoveLast
Exit Do
End If
If intDemand <= intStock Then
If y = 1 Then rst2!Insp = intDemand
If y = 2 Then rst2!Hold = intDemand
If y = 3 Then rst2!Problem = intDemand
If y = 4 Then rst2!SCret = intDemand
If y = 5 Then rst2!Offsite = intDemand
If y = 6 Then rst2!SCtogo = intDemand
If y = 7 Then rst2!Raw = intDemand
If y = 8 Then rst2!Scun = intDemand
rst2!StockFlag = strStockFlag
intStock = intStock - intDemand
rst2.MoveNext
End If
'rst2.Update
Debug.Print "loop number=" & y; " Demand =" & intDemand & "Remaining Stock=" & intStock & " Flag=" & strStockFlag
Debug.Print "CaStockCode " & rst2!Component & " Date " & rst2!MLineShipDate & " Qty " & rst2!MBackOrderQty & "FG= " & rst2!FG & " Problem=" & rst2!Problem & " Hold= " & rst2!Hold & " Insp= " & rst2!Insp & " Scret=" & rst2!SCret & "Offsite= " & rst2!Offsite & " Sctogo =" & rst2!SCtogo & " Raw= " & rst2!Raw & " Scun =" & rst2!Scun & "StockFlag =" & rst2!StockFlag
intSelDemFlag = intSelDemFlag + 1
Loop
Next y
intRecNo = intRecNo + 1
Debug.Print "outside loops"
rst.MoveNext
Wend
Debug.Print "End routine"
Exit_Command88_Click:
Exit Sub
Err_Command88_Click:
MsgBox Err.Description
Resume Exit_Command88_Click