So as the title says I am trying to use a VBA variable within an update statement. This is being used because I want to loop through each cell in a range and subtract a value from the worksheet from a value within the table corresponding to that iterations cell reference. However, statement runs an error saying "too many parentheses." However, this is odd to me b/c I pulled the SQL syntax directly out of access. I guess I am missing something? I will post both versions of the code:
Code with vba variables :
Private Sub UPDIZZLE()
Dim rCell As Range
Dim rRng As Range
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strConnection As String
Dim QUV As Long
Dim IID As String
Set rRng = ActiveWorkbook.Sheets("Sheet1").Range("b2:b100")
For Each rCell In rRng.Cells
If rCell <> "" And rCell.Value <> 0 Then
IID = rCell
QUV = rCell.Offset(0,x).Value
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ashleysaurus\Desktop" & "\" & "xyzmanu3.accdb"
con.Open strConnection
rs.Open "UPDATE Stuff SET Stuff.Quantity = Quantity-" & QUV & " WHERE (((Stuff.[ItemID])=" & IID & "));", con
con.Close
End If
Next rCell
End Sub
code without variable:
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ashleysaurus\Desktop" & "\" & "xyzmanu3.accdb"
con.Open strConnection
rs.Open "UPDATE Stuff SET Stuff.Quantity = Quantity-1 WHERE (((Stuff.[ItemID])=Thingamagig));", con
con.Close
As
IID
is a string shouldn't it be(single quotes around IID)