I have 300+ queries which fetch information from my database. Right now, there is no time condition specified in the queries, so if I wanted to filter all of them by a certain time period, i.e. Between #07/01/2009# And #08/01/2009#, I would have to manually go in each query and add this condition.
All of my queries are populating data into 4 main reports. What I am trying to do is apply a time filtering criteria like the one above to all of my queries at once, so that I can create a weekly report, as well as the Totals report (which just means there's no time condition).
Is there any easy way to add a single parameter before pulling my report that would filter all of my queries at once, and to pull the Totals report if it the parameter field were blank?
You can use a reference to a form control within the WHERE clause of a query. So, for example, if I have an open form named frmDatePicker which includes a text box control named txtStartdate, I could use that control's value as a WHERE condition.
SELECT *
FROM MyTable
WHERE my_date_field >= Forms!frmDatePicker!txtStartDate;
That approach can work, but I have no idea whether it's an appropriate fit for your situation. You would have to modify all of your queries which include that date condition. That could be a one-time-only change. But if you ever change the form and/or control name, you'd have the revise the queries again. (So try not to do that!)
Edit: If you want to allow the user to leave txtStartDate blank, so as not to filter on that date at all, try a WHERE clause like this:
WHERE
my_date_field >= Forms!frmDatePicker!txtStartDate
Or Forms!frmDatePicker!txtStartDate Is Null;
Related
I am trying to create a report in Microsoft Access that allows a user to select a variety of criteria from a form (including the option to not select any options for a specific criteria, resulting in no restriction on that field), which are then used as criteria in the record source on the report. I have been successful in doing so with all but two of my criteria, which are querying against multi-valued fields (The user selects a single option from the combobox, but the table being queried allows for multiple values in the field).
The control source for my combobox ("OSHA Carcinogens") is as follows:
SELECT
TblCarcinogens.ID,
TblCarcinogens.[Carcinogen Name]
FROM TblCarcinogens
UNION
SELECT
Null AS AllChoice,
"(All)" AS Bogus
FROM TblCarcinogens
ORDER BY TblCarcinogens.[ID];
The relevant code from my where clause is as follows:
((IIf([Forms]![FrmRPIFilters]![OSHA Carcinogens] Is Not Null,
[RPIMaster].OSHA Carcinogens].[Value]=
[Forms]![FrmRPIFilters]![OSHA Carcinogens],
[RPIMaster].[OSHA Carcinogens].[Value]=
[RPIMaster].[OSHA Carcinogens].[Value]))
<>False));
This method seems to work for anything other than multi-valued fields, but also seems pretty convoluted. I'm open to suggestions about streamlining the where clause in general as well if there is a better way to do it.
When the query is run with an option selected in the OSHA Carcinogens field on FrmRPIFilters, the result set includes only records which have no value stored in this field.
Does anyone have experience with this type of where clause and know what I'm missing?
tldr: Can not update records from query because of aggregate functions. What workarounds do you suggest?
I have a table containing decision criteria to which a user can assign a relative weight. I calculate the absolute weight in an SQL query using an aggregate function (as described here Divide the value of each row by the SUM of this column).
qryDecisionCriteria
name relative_weight absolute_weight (calculated)
price 2 50 %
quality 1 25 %
experience 1 25 %
I would like to present the query result in a form, where the user can update the relative weights, and then sees the absolute_weights.
However, the query results are not updatable, because the query involves an aggregate function.
What alternative methods or workarounds could I use, so that a user can edit relative_weights and view absolute_weights as a kind of visual feedback?
I read about temporary tables here http://www.fmsinc.com/MicrosoftAccess/query/non-updateable/index.html but I'm not sure, how to implement this.
Maybe I could also create an additional "edit form" based on a simple query, that is automatically invoked when the user selects a record in qryDecisionCriteria data?
Or maybe just display data from two queries (one updatable, one with the calculated field) next to each other in the form?
Which options would you recommend and why?
Make the Record Source for the form the updatable base query. In the text box which shows the calculated absolute weight set the control source to
=DSum("relative_weight","<base table name>")/Forms!<Form Name>!relative_weight
You'll need to be sure that you do two things with this
When you drag fields onto a form in Access it makes the name of the control the same as the control source column. this is really annoying and can cause a lot of headaches. Rename your control to something like txtColumnName. That way Forms!<Form Name>!relative_weight is guaranteed to reference the field and not the textbox.
in the AfterChange event for the relative_weight textbox you should add an event handler in which the following code is run
txtabsolute_weight.Requery
This will make sure the formula is recalculated whenever someone changes a weight. Otherwise they need to hit F5.
In Crystal Reports, I want to add a WHERE field <> date to filter out dates that have a NULL value from my database in my report.
I'm using a legacy FoxPro database on the backend which generates an SQL statement from my report, but doesn't appear to have anyway of adding a WHERE clause to the generated statement.
When accessing the FoxPro backend directly, dates with psudo-NULL values have a date of 1899-12-30, but when they are pulled from FoxPro through Crystal they appear as 12/30/99 (which is maybe the same date just displayed in MM/DD/YY format).
I noticed that the report had an existing Parameter Field that prompts the user to filter out the original query down to a specific date range. I tried to add my own in addition to the Parameter Field, but discovered that what I needed with my WHERE field <> date is not an available option since there are only 3 types of Field Parameters mainly:
Discrete
Accept single and discrete values.
Ranged
Accept a lower and upper value in order to select everything in this range.
Discrete and Ranged
A combination of the two above
None of these appear able to filter the results of the query using a WHERE NOT type of clause, is there some other way to do this?
Add this to your record-selection formula:
// remove actual nulls
AND Not(Isnull({table.date_field}))
// remove old dates
AND {table.field} <> date(1899,12,30)
// remove dates not in select parameter value
AND {table.field} IN {#date_parameter}
All I really needed to do was add some criteria to the WHERE clause of the SQL statement, simple enough in an SQL client, but when you're doing this in Crystal Reports v10 it's a bit difficult to find, unless you know what you are looking for...
So what I needed to do was:
Select the field to filter by in the report (in the Details section)
Click the Select Expert button on the Experts toolbar.
In the Select Expert dialog the name of your field should appear in a tab.
Below you can select the WHERE criteria used to filter the records.
I guess this is a silly question, but I'm having problems figuring this out.
I have two tables with loads of information used to make reports.
I have a query that joins these two tables.
The same query adds two simple filters: A date range and an ID checker.
The reports are supposed to be printed withing a date range with a specific ID.
Every time I need to change the query data range I need to manually edit it and change the parameters.
How can I make a form and pass this information to the query so I won't have to manually update the query every week?
I made a new form with two date fields and I would need some code to pass this information to the query before opening it, but DoCmd.OpenQuery Method doesn't permit I pass any information.
Ideally I'd prefer to use a SQL command to set the query and then open it, is this even possible?
Actually, I am using this method often:
Declare variables in a Module and write a function for each one that simply returns the value. You can then use the function call within the Query-Designer (e. g. criteria = Between getMyValue1() AND getMyValue2())
You can then set the global variables before opening the query.
(This has worked better for me than Query-Parameters or direct references to form fields within queries.)
You could also put a condition in your query that refers to a field in your form:
SELECT ... WHERE ID = [Forms]![FormName]![TextField]
Open the report with a filter:
Dim sWhere as String
sWhere = "Tbl_Swift.Data >= #10/01/2012# AND Tbl_Swift.Data <= #10/10/2012#"
DoCmd.OpenReport "rptMyReportName", acViewPreview, ,sWhere
(I'm not sure if "BETWEEN" will work or not, although I think it should if you'd write it properly.)
You have two options.
Do not put values of parameters in the query. Access will always ask for these values on every execution of query.
use DATE() function to get the current date and calculate the week start and end accordingly. This way, you will never have to provide the date.
I have created a query to calculate the sum of all of the profit values in a table, I tried to output this to a textbox on the main form of my database and I just the error #NAME?.
Has anyone tried this before and are there any major things I am missing?
We would need to see some code/design details to understand why your text box gets that #Name error.
Without those details, I'll just suggest you consider a DSum() expression, instead of a query, to load the text box. And DSum() is kind of like a SELECT query, but returns only a single value instead of a result set.
DSum("YourNumericField", "YourTable")
Examine the DSum online help topic for more details. You might find the optional Criteria parameter useful (like a WHERE clause in a SELECT statement) if you ever want to sum only a subset of rows from your table.
DSum("YourNumericField", "YourTable", "account_status = 'ACTIVE'")
If you have create query to calculate the sum called "querySum". On the form property sheet, make sure selection type you change to FORM, go to Data and select "querySum" as record source.
And then, click text box, go to the property sheet and choice Data > Control Source. So you can choice column from query to the text box.
Otherwise, if you want to use VBA. You can do like this
DSum("NumericField", "YourTable")
Or with condition
DSum("NumericField", "YourTable", "type = 'Payment'")