Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Update query within VBA code

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 store data using unbound forms, now I want to update some particular fields of a table by data entered in an unbound form for another table. The 2 tables in question are Transaction and Stock. Stock has 3 fields PartNo, Location and Qty. I want the QTY in stock to be deducted from the location where stock is moving out of, and to be added to where it is going to.

Sample data in table Stock

Stock_PartNo    Stock_Location  Stock_Qty
2288            SAWRH001        85
2288            SAWRH002        54
3214            SAWRH003        544
4567            SAWRH001        32
5555            SAWRH002        128
5555            SAWRH005        874
5678            SAWRH002        321
6544            SAWRH004        465
6666            SAWRH003        45
6666            SAWRH004        87
7777            SAWRH003        365
7890            SAWRH002        352
8765            SAWRH005        57
8888            SAWRH004        54
9999            SAWRH005        21

Here is my code for an unbound form:

Private Sub Command39_Click()

Dim db As Database, rsCust As Recordset


    Set db = CurrentDb
    Set rsCust = db.OpenRecordset("Transaction", DB_OPEN_DYNASET)

        rsCust.AddNew
        rsCust("Trans_PartNo") = Me!Combo52
        rsCust("Trans_Desc") = Me!Text19
        rsCust("Trans_Disp") = Me!Text21
        rsCust("Trans_Recv") = Me!Text23
        rsCust("Trans_Qty") = Me!Text25
        rsCust("Trans_Date") = Me!Text29
        rsCust.Update

        MsgBox "Material transfer information has been updated"

        Call ClearControls

    rsCust.Close
    db.Close
End Sub

This data would be stored in a table called Transaction which is just a record history of what was moved from one place to another. What I want is that table Stock should be updated using this form, so if PartNo 2288 is transferred from SAWRH001 to SAWRH005 then it should update table Stock automatically. As far as I understand I need to embed a SQL query but I don't know how to do that in VBA.

like image 989
user1687929 Avatar asked Sep 21 '12 06:09

user1687929


People also ask

How do I run an update query in Excel?

Refresh a query in a worksheet In Excel, select a cell in a query in a worksheet. Select the Query tab in the ribbon, and then select Refresh > Refresh.


1 Answers

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
like image 162
Fionnuala Avatar answered Oct 19 '22 09:10

Fionnuala