So here's my issue: I'm trying to run an SQL query to add a record to a table. One of the fields, however, is the autonum key from the record for another table that's just been generated with another query, which is being run on a recordset from yet another query on another table. (I'm trying to work with a system designed by someone else -- don't get me started.)
In the code below, the commented line is the query I'm trying to run. ItemID needs to be the ID for the record in tblInventory just created by the query above it. I was hoping that since that query was just executed, I could just reference the tblInventory.ID field, but that didn't work.
My first-born to whoever can tell me a way to just get the ID from the tblInventory record just created, without trying to set up nested recordsets and my brain and the database exploding.
Code:
Set db = CurrentDb()
intPONUM = Me.PONUM
strSQLItems = "select * from tblPOItems where ponum = " & intPONUM
Set rsItem = db.OpenRecordset(strSQLItems)
recCount = rsItem.RecordCount
If rsItem.RecordCount > 0 Then
Do While Not rsItem.EOF
If Not IsNull(rsItem!ITEMNUM) Then
intItem = rsItem!ITEMNUM
intNewQty = rsItem!ReQty
DoCmd.RunSQL "UPDATE tblInventory SET tblInventory.QTY = tblInventory.QTY + " & intNewQty & " WHERE tblInventory.item = '" & intItem & "'"
' DoCmd.RunSQL "INSERT INTO tblInventoryByLocation ( ItemID, LocQTY, BinLocID ) SELECT " & tblInventory.ID & " AS Expr1, " & intNewQty & " AS Expr2, " & "71 AS Expr3"
'
rsItem.MoveNext
Else
rsItem.MoveNext
End If
Loop