Results 1 to 4 of 4

Thread: Vba Access 2007, update stock from quantity

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    2

    Vba Access 2007, update stock from quantity

    Hi Guys,

    ive got 2 tables one product with stock, and one with transactionline

    Product has product id, productname, stock

    transactionline has

    transactionlineid, productid(fk),transactiontype, quantity

    if the form has transactiontype 1 then will deduct stock from quantity

    here is the vba code that i created(im a newbie) but gives me error at docmd.runsql mysql

    Does this works? (vba with access 2007)
    ------------------------------------------------
    dim yoursql,mysql,success as string

    yoursql= "SELECT product.productid,Sum([TransactionLine].Quantity) as "&_
    "SumofQuantity" & _
    " FROM [producut] INNER JOIN TransactionLine ON " & _
    "[product].ProductId = TransactionLine.ProductId " & _
    " GROUP BY [product].ProductId"

    DoCmd.RunSQL yoursql

    If TransactionTypeId = 1 Then
    mysql = "UPDATE Product SET Product.stock =" &_
    "product.stock - [Sumofquantity] "

    DoCmd.RunSQL mysql
    MsgBox success


    End If

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Vba Access 2007, update stock from quantity

    unless it's a type this will not help

    FROM [producut]

    should it not be

    FROM [product]

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    2

    Re: Vba Access 2007, update stock from quantity

    its a mistake...
    here is the code again
    vb Code:
    1. Private Sub button20_Click()
    2. Dim mysql, yoursql, recsource, success As String
    3.  
    4.  DoCmd.OpenForm "sumqtr", acViewDesign
    5.  recsource = Forms!sumqtr.RecordSource
    6.  DoCmd.Close acForm, "sumqtr", acSaveNo
    7.  
    8.  Dim cnn1 As ADODB.Connection
    9.  Dim myrecordset As New ADODB.Recordset
    10.  Set cnn1 = CurrentProject.Connection
    11.  myrecordset.ActiveConnection = cnn1
    12.  myrecordset.CursorType = adOpenDynamic
    13.  myrecordset.LockType = adLockOptimistic
    14.  
    15.  
    16.  yoursql = "SELECT Sum([TransactionLine].Quantity) as SumofQuantity" & _
    17.             " FROM [" & recsource & "] INNER JOIN [TransactionLine] ON [product].ProductId = [TransactionLine].ProductId " & _
    18.             " GROUP BY [product].ProductId"
    19.          
    20. myrecordset.Open yoursql
    21. DoCmd.RunSQL yoursql
    22.  
    23. If TransactionTypeId = 1 Then
    24.  
    25.  
    26.             mysql = "UPDATE product SET product.stock = product.stock" & _
    27.             " - [yoursql]"
    28.             DoCmd.RunSQL mysql
    29.             success = MsgBox("records updated", vbOKOnly, "Success")
    30.             MsgBox success
    31.            
    32.    End If

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Vba Access 2007, update stock from quantity

    What is the error message? and if you place a breakpoint on the line what is the value of myrecordset after you have opened it?

    If Myrecordset is populated, then I dont see the point of the DoCmd

    also have a look at yoursql and check that it is well formed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width