I looked into your code a little more.
In your Private Sub theUPC_Exit(Cancel As Integer) routine you have created the Update query in vba, and as well you have a stored query qryUpdateInventory.
I can see where you have commented out areas for testing.
Since you already have A = theUPC, you have the value of Forms!frmPulledStock!theUPC, so when you construct strSQL you could use the variable A.
Code:
strSQL = "UPDATE tblPurchased " _
& " SET tblPurchased.InStock = No WHERE " _
& " ((tblPurchased.ID) In (SELECT First(B.ID) FROM tblPurchased As B " _
& " HAVING (((B.UPC)='" & A & "') AND((([B]![InStock])=Yes)))));"
and then use conDatabase.Execute strSQL
OR you can change qryUpdateInventory to
Code:
UPDATE tblPurchased
SET tblPurchased.InStock = No
WHERE
tblPurchased.ID In
(
SELECT First(B.ID) AS Expr1
FROM tblPurchased as B
WHERE ((([B]![InStock])=Yes)) and
B.UPC=[Forms]![frmPulledStock]![theUPC]
);
When you have a subquery, you alias the tablename so the second use of the table is clear to SQL. That's why I used tblPurchased as B. You're using the table twice and this keeps the references clear.
I moved the [Forms]![frmPulledStock]![theUPC] to the subquery, since you need
the UPC and the InStock =Yes to select the proper record.
Hope this helps.
Here is the revised code for the procedure if you choose to use it.
I added an Error routine. I commented the DoCmd.SetWarnings False
I would use the strSQL as per the proc. I commented out the DoCmd.OpenQuery
You do not need the button to Run the Query on the Form and can remove it.
The Update runs when you move out/Exit of the UPC field.
Code:
Private Sub theUPC_Exit(Cancel As Integer)
Dim A, B, C, D As Variant
Dim strAnswer As String
Dim iCounter As Integer, iQty As Integer
Dim conDatabase As ADODB.Connection
Dim strSQL As String
On Error GoTo theUPC_Exit_Errorm
A = theUPC
If IsNull(A) = True Then
Exit Sub
Else
End If
B = DLookup("UPC", "tblItems", "UPC = [theUPC]")
If IsNull(B) = True Then
MsgBox "This item isn't in the DataBase!", vbOKOnly
Else
C = DCount("ID", "tblPurchased", "InStock = yes AND UPC = [theUPC]")
D = DLookup("ID", "tblPurchased", "InStock = yes AND UPC = [theUPC]")
iQty = txtQty
If iQty > C Then
MsgBox "We don't have that many!", vbOKOnly
Exit Sub
Else
'DoCmd.SetWarnings False
DoCmd.Beep
Set conDatabase = CurrentProject.Connection
strSQL = "UPDATE tblPurchased " _
& " SET tblPurchased.InStock = No WHERE " _
& " ((tblPurchased.ID) In (SELECT First(B.ID) FROM tblPurchased As B " _
& " HAVING (((B.UPC)='" & A & "') AND((([B]![InStock])=Yes)))));"
Debug.Print strSQL
For icount = 1 To iQty
' = DoCmd.OpenQuery "qrySelectOldest"
' DoCmd.OpenQuery "qryUpdateInventory"
conDatabase.Execute strSQL '<<< do this
Next
conDatabase.Close
Set conDatabase = Nothing
End If
End If
On Error GoTo 0
Exit Sub
theUPC_Exit_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure theUPC_Exit of VBA Document Form_frmPulledStock"
End Sub