Hi all, still in this code
Every time I run the code, when the compiler goes to "DoCmd.RunSQL UpdSubStock" and "DoCmd.RunSQL UpdPartStock", it always shows Enter Parameter Value (UnitID) where it should not. I checked the query using msgbox (highlighted), and it shows the UnitID value correctly. Any idea why is this happening? Thanks
Code:
Private Sub ProcessButton_Click()
Dim orderQty As Long
Dim UnitID As Long
Dim SubID As Long
Dim PartID As Long
Dim subQty As Long
Dim partQty As Long
Dim subStock As Long
Dim newStock As Long
Dim subNeeded As Long
Dim partStock As Long
Dim partNeeded As Long
Dim dbs As Database
Dim subRS As DAO.Recordset
Dim partRS As DAO.Recordset
Dim selectSub As String
Dim selectPart As String
Dim updSubStock As String
Dim updPartStock As String
Set dbs = CurrentDb
UnitID = Me.UnitIDBox.Value
orderQty = Me.QtyBox.Value
selectSub = "SELECT * FROM [TblUnitSub] WHERE UnitID=" & UnitID
Set subRS = dbs.OpenRecordset(selectSub)
subRS.MoveFirst
Do While subRS.EOF = False
subStock = DLookup("Stock", "SubUnit", "SubUnitID=" & subRS("SubID"))
subNeeded = subRS("Qty") * orderQty
If subStock > subNeeded Then
newStock = subStock - subNeeded
updSubStock = "UPDATE SubUnit SET Stock=" & newStock & " WHERE UnitID=" & UnitID
MsgBox (updSubStock)
DoCmd.SetWarnings False
DoCmd.RunSQL updSubStock
DoCmd.SetWarnings True
MsgBox ("Stock is deducted, subStock now is " & newStock)
ElseIf subStock = subNeeded Then
newStock = 0
updSubStock = "UPDATE SubUnit SET Stock=0 WHERE UnitID=" & UnitID
MsgBox (updSubStock)
DoCmd.SetWarnings False
DoCmd.RunSQL updSubStock
DoCmd.SetWarnings True
MsgBox ("Stock is now 0.")
ElseIf subStock < subNeeded Then
MsgBox ("Stock is not enough, needed=" & subNeeded & ", available=" & subStock)
End If
subRS.MoveNext
Loop
selectPart = "SELECT * FROM TblUnitPart WHERE UnitID=" & UnitID
Set partRS = dbs.OpenRecordset(selectPart)
partRS.MoveFirst
Do While partRS.EOF = False
partStock = DLookup("Stock", "Part", "PartID=" & partRS("PartID"))
partNeeded = partRS("Qty") * orderQty
If partStock > partNeeded Then
partStock = partStock - partNeeded
updPartStock = "UPDATE Part SET Stock=" & partStock & " WHERE UnitID=" & UnitID
DoCmd.SetWarnings False
DoCmd.RunSQL updPartStock
DoCmd.SetWarnings True
MsgBox ("Part stock is deducted, part stock now is " & partStock)
ElseIf partStock = partNeeded Then
partStock = 0
updPartStock = "UPDATE Part SET Stock=0 WHERE UnitID=" & UnitID
DoCmd.SetWarnings False
DoCmd.RunSQL updPartStock
DoCmd.SetWarnings True
MsgBox ("Part stock is now 0.")
Else
MsgBox ("Part stock is not enough, needed=" & partNeeded & ", available=" & partStock)
End If
partRS.MoveNext
Loop
End Sub
Every time I run the code, when the compiler goes to "DoCmd.RunSQL UpdSubStock" and "DoCmd.RunSQL UpdPartStock", it always shows Enter Parameter Value (UnitID) where it should not. I checked the query using msgbox (highlighted), and it shows the UnitID value correctly. Any idea why is this happening? Thanks