MSAccess Query Against Multivalued Field with Bogus Condition - sql

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?

Related

how to get query criteria in access vba

Now I am trying to create customize query design view form with Access VBA. I want to show fields name and criteria of saved query.
For example, SQL statement that executed by QueryDef.sql is
SELECT AllCars.ID, AllCars.CarName
FROM AllCars
WHERE (((AllCars.ID) Between 15 And 25) AND ((AllCars.MinPay)<1000)) ;
I would like to get criteria expressions( ID = Between 15 And 25, MinPay = <1000) and field names(ID , CarName) from it.
I can get field name but I am not sure how to get query criteria expression in Access VBA.
I tried to split sql statement but I think this is not a best way for multiple criteria expressions.
If getting criteria in access vba may be possible, could someone help me?
Thanks in advance.
Change the code that creates the saved query: instead of saving all the SQL together, save the field names separately and the expressions separately.
To keep all the fields together but in a way that you can later separate them, put the character ";" between the field names, and to separate them you will use the split function.
You can do the same for the expressions, and when you read the saved data you associate the position in the array of the expression with the position of the field in the array and thus you connect them together to show the user which expression applies to which field.
In the same way you build the query

Why isn't custom SQL table column selectable/joinable in Tableau?

I have a custom SQL table I make in Tableau as a stand-alone datasource
SELECT SUM(AMOUNT)/100 AS MONTHLY_REWARDS,CLINIC_ID,DATE_TRUNC('month',INSERTED_AT) AS MONTH,'Total Amount' AS TYPE
FROM "MY_SCHEMA".REWARDS
WHERE CLINIC_ID IS NOT NULL
GROUP BY CLINIC_ID,MONTH,TYPE
I'm not modifying or casting CLINIC_ID in any way in this summary. Yet when I access this field in a workbook I can no longer use it as a User Filter, but I can for MONTH or TYPE from my summary.
And when I use just the REWARDS table as the datasource (no custom SQL), it has no trouble using it as a User Filter.
I've tried right-clicking in the Worksheet and converting to string.
When I do, it becomes a User Filter option, but using it as a filter results in no data (because it is actually an Int in the data). When I convert it back to Number (decimal), it is no longer a User Filter.
Finally, if I try to have the custom SQL with the rest of my tables and join on Clinic ID, it says the types do not match even though they come directly from the same table with no modifications.
What am I doing wrong and how can I start using Custom SQL as a table in these scenarios? I'd like to be able to use Clinic ID from the Custom SQL as a User Filter as well as a key to join to other tables if I want.

Access form - how to make text field have a control source from a SQL query?

Background
I have two tables:
Projects
EmployeeID
Employee
EmployeeID
Name
I have a query I am basing a form on containing, among other items:
SELECT e.Name FROM Projects p JOIN Employee e ON e.EmployeeID=p.EmployeeID
When I make a form in Access based on this query, I can very easily display e.Name on my form because it is joined from the query.
My real example is of course considerably more complicated than this simple example. The above works fine for read-only queries and scales well. However I would like to use a Splitform view and this becomes very slow with many joins for even small numbers of recordsets. Considering a large percentage of my joins are simple like the above, I am hoping for a way to remove as many as possible.
On the form, e.Name will be read only and not be update-able.
Similar question
In this question I am able to successfully change a combo-box into a lookup. The accepted answer allows a combo box to control Projects.EmployeeID by displaying Employee.Name field in the combo box.
Possible work-arounds
I know one way I could do this is use a combo-box but disable it. This would look a bit weird since it'd have the drop down selector but not be selectable.
Alternatively, I could make it a completely unbound field and write VBA code to update the form each time the recordset changes by running quick queries, getting the text value I am searching for, and updating a label accordingly.
Neither of these are overly appealing, however.
Question
How can I display a single text field on an Access split-form which is the result of a very simple query lookup, based on an ID from the main table field?
You can use Dlookup to return this reference very simply
Construct a Dlookup formula like:
=DLookUp("Name", "Employee", "EmployeeID =" & "[EmployeeID]"
and use this as the ControlSource for the textbox.
Some notes:
The & is important as it binds the formula to the single record displayed on the form
[EmployeeID] refers to the current record displayed on the form. This assumes that "EmployeeID" is included in the query for the form, whether bound to the Projects table or included in the query

Edit Parameter Field Crystal reports with NOT value?

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.

MS Access / SQL - Applying a time condition to multiple queries at once

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;