473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to use query result in code

stonward
145 100+
Hi Guys,

I can't believe I'm stuck on this - I get the feeling I'm missing something very obvious....

I have a payments table and a subform based upon it. My CustomerOrders table can have any number of payments made against each Order.

When the user opens an (unpaid) order, I need to run a query to find the total payments made (so far) against that order number. No problem there, but how do I get that value 'into' my variable 'totalReceived'? I have tried placing the query's SQL into VBA directly, and also tried creating a recordset based upon the query. All to no avail. Am I going about this the wrong way?!

Thanks for your time (again!)

RPE
Jan 15 '13 #1

✓ answered by TheSmileyCoder

There are various approaches that can be used. I presume the payments are in a table such as tbl_Payments, and linked by OrderID. If you want it in a variable, you could use DSum.
Dim TotalReceived as double
Expand|Select|Wrap|Line Numbers
  1. TotalReceived=Dsum("Amount","tbl_Payments","OrderID=" & me.txtOrderID)
Where txtOrderID is a textbox containing the orderID. You can replace this with a variable if you desire.

7 1626
TheSmileyCoder
2,322 Expert Mod 2GB
There are various approaches that can be used. I presume the payments are in a table such as tbl_Payments, and linked by OrderID. If you want it in a variable, you could use DSum.
Dim TotalReceived as double
Expand|Select|Wrap|Line Numbers
  1. TotalReceived=Dsum("Amount","tbl_Payments","OrderID=" & me.txtOrderID)
Where txtOrderID is a textbox containing the orderID. You can replace this with a variable if you desire.
Jan 15 '13 #2
NeoPa
32,556 Expert Mod 16PB
Alternatively, if you have a subform control that is already designed to show those payments then a control can be set up to count the occurrences of a particular field (which must be associated with a control) from your subform. You may need to ensure the form goes to the last record then back in order to ensure an accurate count, but it saves running a separate query for data you already have access to.
Jan 15 '13 #3
stonward
145 100+
thanx guys.

Didn't think of dsum....and I like domain functions despite what I've read from some, um, purists...!

Life-savers again!

RPE
Jan 16 '13 #4
NeoPa
32,556 Expert Mod 16PB
I expect, like me, those (we) purists have to work with larger Access projects sometimes, and have too much experience of how over-use of the Domain Aggregate functions can slow things down unacceptably not to mention the issue RPE, but ultimately you know your project best, so it makes sense to go with what you're happiest with.

I'm happy as long as I've put the warning in, whether you (as the OP) see it as an issue or not. It means it's there for all to see, so no-one can feel they've not been given the whole story if they later run into problems.
Jan 16 '13 #5
TheSmileyCoder
2,322 Expert Mod 2GB
While I agree that all the Domain functions should be used with care, I feel that they also certainly have their place. Inside loops however is certainly not one of them.
Often times it will be faster to include the information as part of the original query, if at all possible.
Jan 17 '13 #6
NeoPa
32,556 Expert Mod 16PB
Smiley:
While I agree that all the Domain functions should be used with care, I feel that they also certainly have their place.
Absolutely. I agree. I use them myself in many cases.

I was simply trying to get the points across (that many people - even some quite experienced ones don't realise) that :
  1. They are not without cost in performance. Considering the work they do that should be expected, but many of our members probably don't have the time or inclination for such consideration. Hence it's worth putting that info out there.
  2. Forms and Reports can use aggregation across groups in a way very similar to doing it within a query. As this doesn't cause the underlying query, or something very similar, to be reprocessed, it's a lower cost option.

Nevertheless, everything in context. Many times one needs totals or other aggregation the overheads are so small anyway that adding a simple Domain Aggregate call is going to be totally unnoticeable as far as performance goes. It's only when you're dealing with data of unpredictable size (EG. an application that keeps growing with continued use.) that it matters.
Jan 17 '13 #7
TheSmileyCoder
2,322 Expert Mod 2GB
An often overlooked issue in development is upsizing. How well will your database react to upsizing? Its one thing to have a test environment with maybe 10-200 records per table and a single user, but once you go live with 10.000 items or more, what used to have no discernible performance hit is suddenly dragging the database down to a grinding halt.

I will admit that I have had to revisit many functions and improve their speed, and I do find it quite satisfying that I sometimes find a performance increase (in a single function) of a factor of 10. However it can be hard to test this untill you get that many records in your system. But its a very learning experience to see reaction times drop from 5 seconds to 0.5 seconds.


If you do use domain functions ensure that:
  1. You do not use them inside a loop with many iterations
  2. You use them on indexed fields
  3. You do not use them inside a query (with few exceptions)
Jan 17 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Surajit Laha | last post by:
I am firing a query like: SELECT TaskName, StartDate FROMTasks WHERE StartDate >= '01-Aug-2003' Now the result comes as: TaskName StartDate -------------------------- Task1 ...
3
by: Jack | last post by:
Hi, I have a form when loaded, retrieves record from an access table. Among other fields there is a check box called FinalUpdate. This is tied to a field in Access of type Yes/No. The form...
2
by: Wei Wang | last post by:
Hi, I want to do a select in dynamic command, something like: TRIGGER FUNCTION DECLARE table_name_suffix text; temp_result RECORD; temp_result2 RECORD;
2
by: Martin Sarsale | last post by:
Dear All: Im looking for solutions (Free Software is better) to do query result caching. Thanks to the people from #postgresql I know that postgres doesn't do that by himself and the solution...
3
by: milam | last post by:
Greetings, I would like to use a query result as a column name in another query, I can't seem to get this to work using Subqueries. Is there a good way to do this? Example: Table...
1
by: RookieDan | last post by:
Greetings fellow Accessers! Im new but in Access, but I have some background in different coding. I have a programme loading customer data into Access belonging to BMW dealers in Europe. ...
7
by: Muddasir | last post by:
i am having problem in printing the query result from MySQL db... actually i am developing a very simple search module. when the user select category from the given categories in drop down...
1
ddtpmyra
by: ddtpmyra | last post by:
how can I capture the query result in PHP? I have two queries below: # Fetch the file information $query ="update filestorage set approved ='Y' where FileID = {$id}"; $query1 ="select...
2
by: reeba | last post by:
I want to store the query result, in an servlet, into an xml file and display the contents of the xml file on the browser...... my code is as follows: public void doPost(HttpServletRequest...
1
by: titli | last post by:
Hi Friends, I have some code in an already built application. Now the output pivots in .xls files is not matching with input data. The output .xls has summarized results whereas , the input...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.