Run Update query within VBA code

前端 未结 2 760
礼貌的吻别
礼貌的吻别 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
    
    0 讨论(0)
  • 2021-01-19 11:27
    db.Execute "UPDATE Transaction SET Stock_Qty = Stock_Qty - " & Val(Me!Text25) _
    & " WHERE Stock_PartNo = '" & Me!Combo52 _
    & "' AND Stock_Location = '" & fromLocation & "'", dbFailOnError
    
    db.Execute "UPDATE Transaction SET Stock_Qty = Stock_Qty + " & Val(Me!Text25) _
    & " WHERE Stock_PartNo = '" & Me!Combo52 _
    & "' AND Stock_Location = '" & toLocation & "'", dbFailOnError
    

    Refer the link here for example code.

    0 讨论(0)
提交回复
热议问题