Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Sep 2002
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts

    SQL instead of a query ? (Access 2000)

    Is it possible to write an expression in SQL instead of writing a thrird query? I have 2 queries, called inflow and outflow
    and in a thrid query i substract the sum of the cartons.It works perfect in this way, but i need to make this more flexible,in order
    to use more variables. Therefore i tried to write the queries in an SQL form, and after that i have placed the result in the OnOpen event of a report
    like that:

    Me.RecordSource = Inflow.Sum([order details].cartons) - Outflow. Sum([order details].cartons
    However i receive the message "Not a valid expression"

    I am afraid what i am doing contradict to the basic rules in Acces and i ask for help.I will repeat the 2 queries i have as follows:

    Inflow

    SELECT products.grade, products.size, Sum([order details].cartons) AS SumOfcartons, Sum([order details].Quantity) AS SumOfQuantity
    FROM products INNER JOIN ((customers INNER JOIN orders ON customers.Customerid = orders.customerid) INNER JOIN [order details] ON orders.orderid =

    [order details].OrderID) ON products.Productid = [order details].ProductID
    WHERE (((orders.customerid)=118) AND ((orders.orderdate)>#12/1/2002#))
    GROUP BY products.grade, products.size;


    Outflow :
    SELECT products.grade, products.size, Sum([order details].cartons) AS SumOfcartons, Sum([order details].Quantity) AS SumOfQuantity
    FROM products INNER JOIN ((customers INNER JOIN orders ON customers.Customerid = orders.customerid) INNER JOIN [order details] ON orders.orderid =

    [order details].OrderID) ON products.Productid = [order details].ProductID
    WHERE (((orders.orderdate)>#1/1/2002#) AND ((orders.customerid)<>118) AND ((customers.afid)=2))
    GROUP BY products.grade, products.size;

    AFter that i tried the following in the OnOpen event of a report:

    Dim Inflow As String
    Dim OutflowAs String
    Inflow = " SELECT products.grade, products.size, Sum([order details].cartons) AS SumOfcartons, Sum([order details].Quantity) AS SumOfQuantity
    FROM products INNER JOIN ((customers INNER JOIN orders ON customers.Customerid = orders.customerid) INNER JOIN [order details] ON orders.orderid =

    [order details].OrderID) ON products.Productid = [order details].ProductID
    WHERE (((orders.customerid)=118) AND ((orders.orderdate)>#12/1/2002#))
    GROUP BY products.grade, products.size "

    Outflow = " SELECT products.grade, products.size, Sum([order details].cartons) AS SumOfcartons, Sum([order details].Quantity) AS SumOfQuantity
    FROM products INNER JOIN ((customers INNER JOIN orders ON customers.Customerid = orders.customerid) INNER JOIN [order details] ON orders.orderid =

    [order details].OrderID) ON products.Productid = [order details].ProductID
    WHERE (((orders.customerid)=118) AND ((orders.orderdate)>#12/1/2002#))
    GROUP BY products.grade, products.size "



    Me.RecordSource = Inflow.Sum([order details].cartons) - Outflow. Sum([order details].cartons

  2. #2
    Star Lounger
    Join Date
    Apr 2002
    Location
    Nottingham, Nottinghamshire, England
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: SQL instead of a query ? (Access 2000)

    P.

    While you're waiting for someone who actually knows what they're talking about<img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    the rowsource for you form has to be a valid sql statement, try something along the lines of

    dim strSQL as string

    strSQL = "SELECT " & _
    "Inflow.grade, " & _
    "Inflow.size, " & _
    "[inflow].[SumOfcartons]-[Outflow].[SumOfcartons] AS NoOfCartons, " & _
    "[inflow].[SumOfQuantity]-[Outflow].[SumOfQuantity] AS Quantity " & _
    "FROM Inflow INNER JOIN Outflow ON (Inflow.size = Outflow.size) AND (Inflow.grade = Outflow.grade);"

    me.RecordSource = strSQL

    you can build the sql with the qbe then paste it into your code and then alter it as you wish.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: SQL instead of a query ? (Access 2000)

    [quote]i need to make this more flexible,in order to use more variables[/url]That statement needs more explanation. *Why* are you trying to use more variables? I seem to recall it being explained previously that you can't use SQL like that, just as you can't subtract one query from another. A query returns a record (or records, or even a single value) but not until it is run, and it doesn't have any methods. You can't just hang a label on it like "outflow", wrap it in some kind of Sum method and voila, return a value.
    Charlotte

Posting Permissions

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