Hey there! I've been having problems with trying to loop through textboxes in my form.
My code will loop through all textboxes in Transaction Product where the SQL will update the Inventory table.
But whenever I run the code, it just loop through 1 row of textboxes only. I've really been trying for hours googling this but I can't really fix it. :/
This is what my form look like: jiajian.sg/test.png
My code will loop through all textboxes in Transaction Product where the SQL will update the Inventory table.
But whenever I run the code, it just loop through 1 row of textboxes only. I've really been trying for hours googling this but I can't really fix it. :/
This is what my form look like: jiajian.sg/test.png
Code:
Option Compare Database
Dim ctl As Control
Private Sub cmdSaveAndUpdate_Click()
DoCmd.Save
' For each control (textbox, combo box, etc) in the subform,
For Each ctl In Form_TransactionProductsSubform.Controls
If TypeName(ctl) = "TextBox" Then
MsgBox "First Loop"
If ctl.Name = "txtProductID" Then
productID = Form_TransactionProductsSubform.txtProductID.Value
End If
If ctl.Name = "txtQuantity" Then
qtyPurchased = Form_TransactionProductsSubform.txtQuantity.Value
End If
End If
If ctl.Name = "txtQuantity" Then
MsgBox "qtyPurchased = " & qtyPurchased & " productID = " & productID
strSql1 = "UPDATE [Products] SET [Current Stock] = [Current Stock] - " & qtyPurchased & " WHERE [Product ID] = " & productID
DoCmd.SetWarnings (False)
DoCmd.RunSQL (strSql1)
DoCmd.SetWarnings (True)
MsgBox "RunSQL done."
End If
Next ctl
End Sub