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.
Related
I have a MS 2010 Access Report that needs to be populated by records that a user pre-chooses. For example.
Form1 has 2 fields, LOT# (which is record id) and CheckoffBox (unbound).
User checks which Lot#'s (records) he wants displayed, then clicks REPORT button. Report only shows selected records.
Can someone help me create the code?
In order to allow the user to use a check box to select multiple records, the list presented in form 1 must be based on a table that has LOT# and a yes/no field. (ie form 1 must be based on a query or a table that ahs this yes/no field).
If only one user will ever use this at any one time then you can simply add a yes/No field to the table that has all the LOT# values.
On your report you then only print those lot numbers that are checked.
Your report simply has to have SQL with a where clause that only print records that have the Yes/No field ticked. (=true =-1).
If many users are going to use this form at the same time it gets more complicated as obviously they all need to have their own "set of yes/no fields".
So you would need to copy the table with the LOT# into a temp table for use by each user (it might have a primary key of USERID, LOT#, or it might be a table with just LOT# and yes/no fields, that only a user can access - ie in their own copy of the database file that has the front end forms).
Anyway, assuming it's a single user, creating the above should be quite straight forwards.
On opening form 1, you might use the form_open event to run a SQL statement that sets all the Yes/No values to No. Although you might not want to do this.
Use
docmd.SetWarnings false
Docmd.runsql "UPDATE theTable SET YesnoField = 0 WHERE YesnoField = -1;"
docmd.SetWarnings true
Let me know if you can take it from here.
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 have a form with search fields and then the search results are shown in a subform below the search fields.
By default the subform loads all records prior to any search criteria being entered.
As this database grows the number of possible records to search will get quite large, so I don't really want the subform to load all records before the user attempts to makes a search.
What's the most performance-friendly way of loading the search results subform without showing any of the records to begin with?
I've considered setting the subform recordsource SQL to search for something I know will never be in the results... but I'm thinking that still requires the records to be loaded first and then filtered (might be wrong about this though).
Ideally I'd like the search results subform to load with the field names of the recordset only, but with no records until the user attempts a search.
Set the subform's record source to a query which returns a single manufactured row.
SELECT
0 AS id,
'' AS fname,
'' AS lname,
'' AS email
That will not pull any records from your table.
After you gather the user's search criteria, build the new SELECT and assign it as the record source.
I am able to fetch a sql query from excel using the odbc connection:
But the issue is that, instead of just give a stack query setup and make user can only click the refresh button, I wish I could have some fields before the result, which user can enter some custom variables(e.g date from, date to)
so when they click refresh button, excel will take the variables of what user inputted into the pre-defined sql query and fetch the result from my sql server.
To do this sort of thing you need to run one query to fetch the initial values of your custom variables from the DBMS. For example, if you want to let the user change the date range, do this.
Use a query fetch the minimum and maximum date from the date column of your table.
SELECT MIN(date) mindate, MAX(date) maxdate
FROM table
Display those dates and let the user edit them on your form.
Put the results of those edited items into your second query, and retrieve the data the user asked for.
SELECT (whatever)
FROM table
WHERE date BETWEEN user-min-date AND user-max-date
The same kind of steps will work to populate a dropdown or listbox with a category
SELECT DISTINCT category
FROM table
ORDER BY category
Then
SELECT (whatever)
FROM table
WHERE category = user-selected-category
I think this addresses your question. Please clarify if it doesn't.
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.