Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Translating my SQL into vba

Author  Topic 

djbomb95
Starting Member

9 Posts

Posted - 2012-09-08 : 03:36:13
Ok, I have a perfectly fine SQL query, and it does exactly what I want it to do. This is it below.

SELECT Sum(Price) AS TotalPrice
FROM PizzaOrder
WHERE PizzaOrder.PizzaID In (SELECT PizzaID
FROM PizzaOrder
WHERE OrderID = 89);

My issue arises when I come accross to vba, trying to get the value that this query would return into a textbox. The way I've translated it goes as follows:

txtPrice = DSum("Price", "PizzaOrder", "PizzaID IN " & DLookup("PizzaID", "PizzaOrder", "OrderID = " & txtOrderID & "") & "")

txtPrice is the name of the textbox. I've used a dsum() to replace the Sum() in sql, and my main need is to find an alternative to the "Where... In (Subquery)" syntax. Dlookup only returns the first value, but I am sure I will require more than one value to be returned. Any help would be greaty appreciated. Thanks.

chadmat
The Chadinator

1974 Posts

Posted - 2012-09-08 : 04:16:42
Why don't you write a stored procedure, do the sum in there, and just call the proc from your app.

-Chad
Go to Top of Page

djbomb95
Starting Member

9 Posts

Posted - 2012-09-08 : 06:44:58
Hello there Chad,

I am relatively new to access vba. Can I ask specifically what you mean by a stored procedure. I am of the understanding it's a bit of a messy external piece of code. Is there a simple-ish explanation or how-to you could maybe link or provide? I am googling, but it seems foreign at this stage.
Thanks
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-08 : 06:52:18
I am not very familiar with VBA, but in VB.Net there is no equivalent statement to the SQL statement you posted. You will need to put the prices into an array and sum it either using a loop or a LINQ query. SQL is good at set-based operations, whic

What Chad was suggesting was that, instead of getting the prices of the Pizza from the database and then summing it up in the VB code, why not sum it up in the database itself and then display that in the txtPrice box.

Also, you probably could simplify your SQL query to this:
SELECT Sum(Price) AS TotalPrice
FROM PizzaOrder
WHERE PizzaOrder.PizzaID = 89;
Go to Top of Page

djbomb95
Starting Member

9 Posts

Posted - 2012-09-08 : 06:59:30
Oh, the actual query is correct. In an order there is more than one pizza, and PizzaID and OrderID are two separate things. I think the reason I was trying to sum the price in the code is because the variables are shown on the UI. I know you can't create a query with reference to the forms.

My OrderID is going to come from a textbox, txtOrderID, and everything else relies on that variable. Would it be worth having the query as the rowsource of a listbox, and then hiding the listbox and making the textbox equal to it? My goal really is to get the value of the sum() into the txtPrice. But there are many orders, and inside those orders are many pizzas.

Thanks
-David
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-08 : 07:06:32
You are absolutely right - I overlooked that PizzaId and OrderId are different columns.

From what I understand, it seems like you can create a stored procedure like shown below and call that to get the price for any given order id.
CREATE PROCEDURE dbo.CalculatePrice
@OrderID INT,
@TotalPrice DECIMAL(19,2) OUTPUT
AS
SELECT @TotalPrice = SUM(Price)
FROM PizzaOrder
WHERE PizzaOrder.PizzaID IN (SELECT PizzaID
FROM PizzaOrder
WHERE OrderID = @OrderID);
This works for just one order id; if you are presenting one order id and its corresponding price to the user, one at a time, this would be fine. If you want to present a number of order id's and the corresponding prices all at the same time, you could call this stored proc multiple times, but there are more efficient ways as well.
Go to Top of Page

djbomb95
Starting Member

9 Posts

Posted - 2012-09-08 : 07:10:40
So to create this, I am needing to create a query, and put this into the SQL view?

Sorry if I seem a little slow. I think I can grasp what you are performing by making this, but how do I update and show that sum in my textbox?

Thanks
-David
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-09-08 : 07:19:19
You would need to do two things:

1. In a SQL Server Management studio, in your database, create the stored procedure. (i.e., copy the code below to query window and run it (just once))

2. In your VB code, call the stored procedure - this page has an example of how to do this: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

To use the example on that page, modify your stored procedure from what I had posted previously as:
USE YourDBNameHere
GO
CREATE PROCEDURE dbo.CalculatePrice
@OrderID INT
AS
SELECT SUM(Price) AS TotalPrice
FROM PizzaOrder
WHERE PizzaOrder.PizzaID IN (SELECT PizzaID
FROM PizzaOrder
WHERE OrderID = @OrderId);


If you are not comfortable with using SSMS and creating stored procedures you could even run the query as an adhoc query from VB. If you choose to do that, you would simply build the query string as shown below (the 89 coming from your text box) and follow the example on the link I posted above.
SELECT SUM(Price) AS TotalPrice
FROM PizzaOrder
WHERE PizzaOrder.PizzaID IN (SELECT PizzaID
FROM PizzaOrder
WHERE OrderID = 89);
Go to Top of Page
   

- Advertisement -