Getting Error When Using Parameter Variable in Access - ms-access

I am getting an error when running a query in Access that states that my expression is incorrect or too complex. I am trying to filter a query with a parameter [Report Date] on a DateValue expression. The query runs without the parameter, unfiltered of course. It will work if I create a table and write a new query with the parameter but this will need to be run daily and that seems cumbersome. Syntax below:
SELECT tbl_ReturnedEquipment.DateProgrammed, DateValue([tbl_ReturnedEquipment]![DateProgrammed]) AS Expr1, tbl_ReturnedEquipment.CSID, tbl_ReturnedEquipment.NewID
FROM tbl_ReturnedEquipment
WHERE (((DateValue(tbl_ReturnedEquipment!DateProgrammed))=[Report Date]) And ((tbl_ReturnedEquipment.NewID) Is Not Null) And ((tbl_ReturnedEquipment.EquipType)=2));

Related

How do I use FIND_IN_SET() with a '?' parameter in SSRS Report Builder 3.0?

My SSRS Server is using an ODBC connection to pull data from a MySQL server. I have a query that's making use of the FIND_IN_SET method. My requirements state that the report should provide options for a start and end date parameter and a comma delimited list of "usernames". The start and end date parameters work fine so I've left them out of my sample code for brevity.
When I run the query in MySQL Workbench, I get results. When I run the query in the query designer of Report Builder 3.0, I get results, IF AND ONLY IF, I hard code the value of the parameter being used for the FIND_IN_SET() method. When I try to use a '?' parameter for the value, and paste in the exact same value as when hard coded, I get no results.
This query works in Workbench and the Report Builder's Query Designer:
select pwk.username,
sum(case when pwk.event = '/some-value/' then pwk.count else 0 end)
as order_detail_count
from table pwk
where find_in_set(pwk.username, 'foosername#test.net,foo2#test.com,username#test.org')
This same query returns nothing when I use the '?' parameter and copy/paste the above value when running the query:
select pwk.username,
sum(case when pwk.event = '/some-value/' then pwk.count else 0 end)
as order_detail_count
from table pwk
where find_in_set(pwk.username, ?)
It seems as though the Report Builder is not using the text parameter the way I expect it to but I have no way to determine how to use it "correctly". Has anyone ever run into this problem?
I would expect the results to be identical but that's not the case. Is there a way to use FIND_IN_SET with a text parameter? Or is there some other way to accomplish this? I'm perfectly okay with using something other than FIND_IN_SET if it will work. I tried using IN() but that didn't even work in WorkBench. My users just need to be able to enter multiple usernames as a parameter.
If it's relevant, I'm using a Tablix to display the data.

SSRS dataset filter

Hi I'm trying to build a report in ssrs which gives the output based on current weekending date where end day is saturday. I'm pulling my data from SSAS Cube and I have a weekending date field so I applied a filter in the query designer to get the current weekending date using a parameter I used this expression:
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today)
When I'm executing my query, it throws an error saying the restriction imposed by the constrained flag in the STRTOSET function were violated.
Please let me know how can I fix this.
The error is thrown because you are passing a string that doesn't correspond with a valid member in your cube.
Note the Query Designer builds a MDX query based on the dimensions and members you select, this query uses the STRTOSET() function to convert your string parameter in a valid member.
...
STRTOSET(#CurrentWeekendDate,CONSTRAINED)
...
If you pass the current weekend date to your parameter it produces:
STRTOSET('2016-01-10',CONSTRAINED)
As 2016-01-10 is not a valid member in your Date dimension it throws the error.
You have to pass something like this depending on your cube:
[Date].[Date Key].&[2005-01-01T00:00:00]
So in SSRS you have to set your parameter to Text and use this expression:
CORRECT EXPRESSION:
="[200 Date].[Week Ending Date].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-ddThh:mm:ss"
)
& "]"
UPDATE: If [Week Ending date] level doesn't include time:
="[Date].[Date Key].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-dd"
)
& "]"
Go through this tutorial if you get stuck.
Let me know if this helps.

initialize a parameter in MS Access query

In MS Access 2010 I am trying to declare and initialize a variable in a query, then display the contents of that variable. The reason for doing this is to use the parameter in a more complicated query as part of a filter. Please note that for this particular case this task must be done in a query object, not in VBA. Here is the code thus far:
PARAMETERS #Date DATE;
SELECT TOP 1 FORMAT(LastUpdated, "yyyy-mm-dd") AS #Date FROM Table1 GROUP BY FORMAT(LastUpdated, "yyyy-mm-dd") ORDER BY FORMAT(LastUpdated, "yyyy-mm-dd") DESC;
SELECT #Date;
This results in an error message: "Characters found after end of SQL statement."
If this can be modified to work the last line of code will be replaced with the more complex query that needs to use #Date in a filter. The other requirement is that it has to be contained within one query object.
In Access you don't need to use the # to prefix parameters. Parameters are inferred as any column which cannot be resolved. So if your table does not have a date column then using date is sufficient for "declaring" a parameter. However maybe something like p_date can separate it from the data type DATETIME/DATE
I don't know what exactly you are expecting with this
FORMAT(LastUpdated, "yyyy-mm-dd") AS #Date
It seems to be trying to assign an alias to FORMAT(LastUpdated, "yyyy-mm-dd") as the parameter value of #Date?
An Access query cannot return multiple result sets so your SELECT #Date; is ultimately what is causing the "Characters found after end of SQL statement." error message.

Datediff & Group By not working?

I'm trying to build a query (QueryB) for it to be referenced in my MS Access control. I know I got the source expression syntax right, I have a very similar working control with QueryA.
I only changed the field and query names. However I keep getting the infamous #Name? error with QueryB. The difference between QueryA and QueryB is the SQL code. QueryA has a GROUP BY and SUM() and QueryB only has DATEDIFF(). I have tried adding the GROUP BY to QueryB, but kept getting [...execute query does not include the specified expression as part of aggregate function].
Query B:
SELECT IIF(DATEDIFF("d",Date_X,Date_Y)>100),
ROUND(IIF(DATEDIFF("d",Date_X,Date_Y)/30,2),
DATEDIFF("d",Date_X,Date_Y)
AS DATEDIFF_X_Y
FROM LAB_DATES GROUP BY LAB_DATES.ID;
This is in MS Access SQL.
ControlB source referencing QueryB in MS Access:
=DLookUp("[DATE_DIFF_X_Y]",
"[QueryB]",
"[LAB_DATES.ID] = " & [Forms]![Lab Results Form]![Textbox_DATE_ID])
When taking out the GROUP BY, this query runs fine but I get the #Name? error in the control. All data is from ODBC MySQL. Access is the front end.
Edit: I can just drop GROUP BY. But I will get the #Name? error. My goal is to display the date difference between Date_X and Date_Y.
[...execute query does not include the specified expression as part
of aggregate function]
That error message already indicates that using GROUP BY does not work with aggregate Functions.
DATEDIFF() is an aggregate function and does not work with a GROUP BY.
The reason for that is that GROUP BY reduces your dataresult and only displays one line per different entry of the column you are using.
What is your aim for that query anyways? I am sure there is a different solumtion for that problem. GROUP BY DATE.ID sounds like you are making a GROUP BY on the primary key which has no effect, because primary keys are unique per definition.
I think your problem is within your DLookup rather than your query. You do not need the GROUP BY at all, this will cause the query to error, you can simply make QueryB:
SELECT IIF(DATEDIFF("d",Date_X,Date_Y)>100),
ROUND(IIF(DATEDIFF("d",Date_X,Date_Y)/30,2),
DATEDIFF("d",Date_X,Date_Y) AS DATEDIFF_X_Y
FROM LAB_DATES;
The problem is that in your DLookup you are using:
[LAB_DATES.ID]
Where you actually want
[LAB_DATES].[ID]
i.e. In it's current form you are looking for a column called LAB_DATES.ID rather than a Column called ID in the object LAB_DATES. Changing your DLookup to this:
=DLookUp("[DATE_DIFF_X_Y]",
"[QueryB]",
"[LAB_DATES].[ID] = " & [Forms]![Lab Results Form]![Textbox_DATE_ID])
Should do the trick.

Access: Data Type Mismatch using boolean function in query criteria

I have a VBA function IsValidEmail() that returns a boolean. I have a query that calls this function: Expr1: IsValidEmail([E-Mail]). When I run the query, it shows -1 for True and 0 for False. So far so good.
Now I want to filter the query to only show invalid emails. I'm using the Query Designer, so I just add a value of 0 to the Criteria field. This gives me a "Data Type Mismatch" error. So does "0" (with quotes) and False. How am I supposed to specify criteria for a boolean function?
For a boolean column, "0" will definitely give you the "Data type mismatch in criteria expression" error. However, 0 or False without quotes should work. I don't understand why they are generating the same error.
See if you can produce a working query by editing the SQL directly. Create a new query, switch to SQL View and paste in this statement (replacing YourTableName with the name of your table).
SELECT IsValidEmail([E-Mail]) AS valid_email
FROM YourTableName
WHERE IsValidEmail([E-Mail]) = False;
Will your query run without error when you create it that way?
Update: Since that query also produced the same error, all I can suggest is trying this one without any criteria.
SELECT
IsValidEmail([E-Mail]) AS valid_email,
TypeName(IsValidEmail([E-Mail])) AS type_of_valid_email
FROM YourTableName;
However, that seems like a long shot because you already told us your earlier attempt without criteria ran without error. If this doesn't identify the problem, would you consider emailing me a stripped down copy of your database? Let me know if you're interested and I'll give you my email address.
The error was caused by the fact that some of the records in my table have a null E-Mail. My query has a where condition to exclude null E-Mail records, so when I ran it with no condition on the IsValidEmail column my function was only called for records with a non-null E-Mail. However, when I added the condition on IsValidEmail it called the function for every record, and the error came from trying to pass null to a function expecting a string.
Another way to say all that:
SELECT [E-Mail],
IsValidEmail([E-Mail]) <--Executed only for rows matching where clause
FROM Contacts
WHERE IsValidEmail([E-Mail]) = False; <-- Gets executed for all rows
Changing my query expression from IsValidEmail([E-Mail]) to IsValidEmail(nz([E-Mail],"X")) resolved the issue.