I'm using Access 2003 and have a form that allows the user to pick from a variety of filters, and I use VBA to update the recordset of a subform based on those filters (I generate a SQL statement in VBA). This subform can have duplicate client ids, and now I'm trying to get to a unique list of client ids.
Is there any easy way to query out the unique client ids using VBA if I have the source SQL for the subform? I've thought of these options:
Write all the ids to a temp table and then query that table (seems
like more work/resources than are necessary)
Somehow apply a query to a recordset object in VBA (is this possible?); I'd set the recordset object equal to the SQL query and then try to run a SELECT DISTINCT client_id FROM <the recordset object>, but I'm not finding any information that leads me to believe this is possible
Generate a new SQL query based on the original one (I was hoping SELECT DISTINCT client_id FROM ('original select query text here') would work, but it gave me a syntax error in the FROM statement
Aim for the third alternative. This should work if you alias the subquery, and 'original select query text here' can fit.
SELECT DISTINCT sub.client_id
FROM
(
'original select query text here'
) AS sub
If Access still chokes, show us what you have for 'original select query text here'.
You're right about alternative #1 ... that is wasteful.
Alternative #2 is not possible because Access won't let you run a query using a recordset object as the FROM source.
Related
I really don't know how to start this. I have a report, that takes records from a query. The query itself takes the records from a table. Now I want to be able to select the records in the query by the id manually. But there is no connection between the records, so I can't say 'give me all records between id x and y'. Also, there is not the count of records always changing. I want a user to be able to say: 'okay, I need to print all the data of these x ids'. how can I archive this?
you can create the object using Querydef and used this to change your query at runtime based on the user slected criteria.
try like this
Dim query_1 as string
query_1 = "Select * from Table1 where ID = 'Your ID'"
CurrentDb.QueryDefs("Report_Query").sql = query_1
than open the report or make pdf ..
You could populate a multiselect listbox from the same record source as the report, and then use the selected records from that to drive the actual report data.
You would probably want to build a PK IN(1,3,99,104...) style criteria by looping through the selected items from the listbox.
I got a query which takes out some fields from a table.
How do I use the same query to create a sum of the values of those fields and update the sum to a single field in another table?
If you must do this, the only way I can see to accomplish without VBA uses DSum() domain aggregate function.
UPDATE tablename SET fieldname = DSum("some field", "some table", "some filter criteria here")
Trying to do this with a nested aggregate query in place of the DSum() returns error 'must be an updatable query'.
Really should re-think your db design.
Hey I'm working on a database in access. Is there anyway in access I can be able to change the criteria for a query using either a report or a form so that the result set changes every time I change the criteria but the query remains the same e.g.
SELECT customer FROM customers WHERE id = 2 this would be the query and the criteria is 2 I want to be able to change the id every time to get a different result set either via a report or a form or any other way other than the query itself
You can refer to a form in a query:
SELECT customer FROM customers WHERE id = Forms!MyForm!txtID
You can open a report using a WHERE statement.
I'm pretty new to Access, so this may be very easy or obvious to some, or maybe it can't be done at all. I have searched this site, but I may not even be using the proper keywords. So far, I haven't found anything that helps me do what I am trying to do.
I have a form with a continuous subform in it. I have an unbound txt field on the main form called txtPO_num. In this form I run a query that displays all records that meet specified criteria in the continuous subform. I also have a control in the subform called PO_Num. My question is, how do I get the main form field value in txtPO_num to populate the PO_Num control in only the records displayed in the subform?
If there is another way to accomplish this I would be interested in knowing that too.
Execute an UPDATE statement which targets the same records which are included in the subform.
You have a SELECT query with a WHERE clause which identifies the records included in the subform's recordset. Build an UPDATE statement using the same WHERE clause. For example, if the SELECT were ...
SELECT field1, field2
FROM YourTable
WHERE field2 = 'foo';
... the UPDATE could be ...
UPDATE YourTable
SET field1 = 'new value'
WHERE field2 = 'foo';
Execute the UPDATE statement using the DAO database object's .Execute method.
I'm having a hard time with a query in MySQL.
I'm working with Delphi XE and I'm sending a query with some parameter to MySQL. Here's an example for the query:
SELECT * FROM users u WHERE u.id IN (:idUsers);
The ':idUsers' is the variable that will receive the parameter I send with Delphi, which is a string containing that is formatted like this, ex.: 1,2,3
The problem is that with this string, I receive only the first user (id = 1). From what I can see, its just like MySQL is adding some quote ('') at the beginning and at the end of the string I send, like if it was '1,2,3' instead of 1,2,3. I've tried this select :
SELECT * FROM users u WHERE u.id IN ('1,2,3');
and it does indeed return only the first user..
I had a function in MSSQL that was splitting the string I sended and returning a temporary table, but we recently switched to MySQL, and from what I read, MySQL doesn't allow returning a table.
Anyone has a clue on how to solve this problem? I've been scrapping the Web for an answer and haven't found one for this specific problem.
Thanks for the help.
Parameters don't work like that; they have no idea that what you're trying to provide is a comma-separated list of values. If you set ParamByName('WhatEver').AsString or Value, it thinks you mean a quoted string that contains everything that belongs in that parameter. So it's doing exactly what it appears to - it's passing IN ('1, 2, 3'), instead of the intended IN (1, 2, 3).
You'll need to either go back to parsing it out yourself and using a temp table, or build the WHERE clause dynamically and concatenating it before executing the query. (Actually, you could build the query dynamically, creating a new parameter for each of the items in the IN clause, and then loop through and assign values to each of those dynamically created parameters, but that gets very ugly very quickly.)
If you have a variable number of parameters in the IN clause, you could build a temporary table in MYSQL (which only contains a column called ID), load the parameter values into the temporary table and then do something like this:
SELECT * FROM users u WHERE u.id IN (SELECT ID FROM TEMPTABLE);
As long the TEMPTABLE only contains the values you want to query, the table space scan is acceptable.
You can then have a variable number of values. You could also make the table permanent, and store the parameters. Add a column for the User, and each user can store their individual parameters.