Trying to use VBA variable in Access UPDATE Statement

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-15 11:07:33

问题


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

回答1:


As IID is a string shouldn't it be

    rs.Open "UPDATE Stuff SET Stuff.Quantity = Quantity-" & QUV & " WHERE (((Stuff.[ItemID])='" & IID & "'));", con  

(single quotes around IID)



来源:https://stackoverflow.com/questions/38329361/trying-to-use-vba-variable-in-access-update-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!