Run Update query within VBA code

前端 未结 2 762
礼貌的吻别
礼貌的吻别 2021-01-19 11:16

I am trying to make a small inventory program using Access but I don\'t have much knowledge, whatever I have done so far is by googling stuff. I Have managed to make code to

2条回答
  •  借酒劲吻你
    2021-01-19 11:25

    With parameters:

    Dim db As Database
    Dim qdf As QueryDef
    
    Set db = CurrentDb
    sSQL = "UPDATE Stock SET Stock_Qty = Stock_Qty + [p1] " & _
         " WHERE Stock_PartNo = [p2] AND Stock_Location = [p3]"
    
    ''Temporary query
    Set qdf = db.CreateQueryDef("", sSQL)
    ''No need to worry about quotes etc
    qdf.Parameters("p2") = Me!Combo52
    
    ''Subtract
    qdf.Parameters("p1") = Me.Text25 * -1
    qdf.Parameters("p3") = Me.From
    qdf.Execute dbFailOnError
    
    ''Add
    qdf.Parameters("p1") = Me.Text25
    qdf.Parameters("p3") = Me.To
    qdf.Execute dbFailOnError
    

提交回复
热议问题