I'm writing a query in Access 2013. The query will use the parameters from a form, pass those parameters to the query, and then display the results. I almost have it working. The problem with this form is that there are multiple date fields. There are two text fields (txtFromDate and txtToDate).
The idea is that user can enter date range one of two ways:
the user can click the date picker and select a date range to pass to the query;
the user can choose the Fiscal Year from a combobox.
I have created a separate table that is setup as follows:
Table: tbl_FiscalYear
Columns: FiscalYr_ID, FiscalYearName, FromDate, ToDate
In my query, the results work great (all parameters passed to query) if the user enters the manual dates in the txt fields. However, if they bypass the manual date entry and use the Fiscal Year combobox, the query results ignore all previous parameters and the results only reflect the appropriate date range per the Fiscal Year date setup in the fiscal year table.
I think the culprit is the OR statement at the end. Is there a way to incorporate both date search parameters into the form, and allowing the use of one or the other? Is my code not correct since it doesn't allow passing null values from the text date fields?
SELECT tbl_QUOTE.QUOTE_ID,
tbl_QUOTE.QUOTE_DATESTART,
tbl_QUOTE.QUOTE_DATEEND,
tbl_QUOTE.QUOTE_lookup_POC_ID,
tbl_QUOTE.QUOTE_lookup_LOC_ID,
tbl_QUOTE.QUOTE_lookup_TRAINER_NAME,
tbl_QUOTE.QUOTE_lookup_STATUS_ID,
tbl_QUOTE.QUOTE_lookup_TRAINER_ID,
tbl_QUOTE.QUOTE_lookup_MODS_ID,
tbl_POC.POC_AGENCY_lookup,
tbl_QUOTE.QUOTE_FINANCIAL_CD,
tbl_FISCALYEAR.FISCALYEARNAME
FROM tbl_fiscalyear, tbl_QUOTE
INNER JOIN tbl_POC ON tbl_QUOTE.QUOTE_lookup_POC_ID = tbl_POC.POC_ID
WHERE Nz([QUOTE_lookup_STATUS_ID],"") Like [Forms]![frm_QuoteReport]![cboStatusLookup] & "*"
AND Nz([tbl_POC].[POC_AGENCY_lookup],"") Like [Forms]![frm_QuoteReport]![cboAgency] & "*"
AND Nz([QUOTE_lookup_POC_ID],"") Like [Forms]![frm_QuoteReport]![cboPOC] & "*"
AND Nz([QUOTE_lookup_MODS_ID],"") Like [Forms]![frm_QuoteReport]![cboCourse] & "*"
AND Nz([QUOTE_lookup_LOC_ID],"") Like [Forms]![frm_QuoteReport]![cboLocation] & "*"
AND Nz([QUOTE_lookup_Trainer_ID],"") Like [Forms]![frm_QuoteReport]![cboTrainer] & "*"
AND Nz([Fiscalyr_ID],"") Like [Forms]![frm_QuoteReport]![cboFY] & "*"
AND Nz([Quote_Financial_Cd],"") Like [Forms]![frm_QuoteReport]![chkFundCd] & "*"
AND [QUOTE_DATESTART] Between [Forms]![frm_QuoteReport]![txtFromDate]
And [Forms]![frm_QuoteReport]![txtToDate]
OR [QUOTE_DATESTART]
BETWEEN (SELECT [tbl_fiscalyear].[fromdate]
FROM tbl_fiscalyear
WHERE fiscalyr_ID = [Forms]![frm_QuoteReport]![cboFY])
AND (SELECT [tbl_fiscalyear].[todate]
FROM tbl_fiscalyear
WHERE fiscalyr_ID = [Forms]![frm_QuoteReport]![cboFY]);
I believe I resolved it. I edited the final lines as follows:
OR [QUOTE_DATESTART] Between [Forms]![frm_QuotesReports]![txtFromDate]
And [Forms]![frm_QuotesReports]![txtToDate] <br/> AND [QUOTE_DATESTART]
BETWEEN (SELECT [tbl_fiscalyear].[fromdate] FROM tbl_fiscalyear WHERE
fiscalyr_ID = [Forms]![frm_QuotesReports]![cboFY]) AND (SELECT
[tbl_fiscalyear].[todate] FROM tbl_fiscalyear WHERE fiscalyr_ID = [Forms]!
[frm_QuotesReports]![cboFY])
Related
I have scoured the internet for options and the only one I have found that can do it is by using a $Proc however I am trying to avoid that.
I would think it would be pretty simple to use a parameter to select a different table depending on what the user chooses from a drop down.
Here it is:
- There are two tables the report needs to use,
* some_table_CY (current year table)
* some_table_STLY (same time last year table)
So I created a parameter that gives the user the option to select "Current_Year" or "Last_Year", depending on which one the user chooses the parameter would then be used in the select statement, something like this: "SELECT * FROM :pReportVersion"
However, it is not working. I need it to do this, not using a union since unioning these two tables causes HUGE performance issues and the query takes more than 4 hours to run which is not acceptable for a report that users need on request.
(This is querying oracle)
Use the Dataset expression and set it to:
="SELECT * FROM " & Parameters!ReportVersion.Value
For longer queries you may need to wrap each line with quotes, append with an ampersand and add a line feed:
="SELECT * " & VBCLRLF &
"FROM " & Parameters!ReportVersion.Value & VBCRLF &
"WHERE FIELD1 > 10 " & VBCRLF &
"AND FIELD2 = 'YES' "
you can still use the union..
Say you have a parameter called #year
set the available values to the following (specify values)
current year for label and 1 for value
last year for label and 2 for value
Then your dataset can be something like this:
select * from some_table_CY
where #year = 1
union all
select * from some_table_LY
where #year = 2
I have an MS Access db. I am writing an application in C# to access it. I have a field "FileName" of type "Short Text in MS Access. Data in FileName field is like "Test 11-12-2004 15.11.15".
Using a Date Range, I got to search records based on FileName field. I am not able to get - How do I compare the date of this format and retrieve the records ? FileName is a Text type and date is a substring of it. Retrieving only the date part and comparing with >= beginDate && <= endDate seems like a puzzle to me.
Can anyone suggest how do I write SQL query to perform this date range comparision and retrieve those records - "Select * from TestHead where FileName......" ????
Any help is appreciated.
Thanks a lot,
In your C# code, as you are going through the records, I'd split the string like this:
char[] delimiters = {' '};
string[] FileNameParts = FileName.Split(delimiters);
This will result in an array FileNameParts, the second element of which will contain the date, which you can convert to an actual date for use in the query:
DateTime FileNameDate = Convert.ToDateTime(FileNameParts(1))
Something along the lines of:
sSQL = "SELECT * FROM Table WHERE " & beginDate & " <= " & FileNameDate
I see this as preferable to adding a column to your table that contains the date substring of the FileName field, because then you constantly need to be updating that column whenever existing records are modified or new records are added. That means more clutter on the C# side, or an UPDATE query on the Access side which at least needs to get called periodically. Either way it would be more communication with the database.
I have three date columns. My report has 6 parameters: start and end date range for all three columns. Currently user has to select all date range but what if I want to allow user to only select one date range at a time. I cannot do "allow NULL values" option in the parameter because that see it as a field containing null value. I don't think it's possible to allow user to select only one parameter at a time so I'm trying an approach where there will be three parameters: one will consists of date field names. And rest two are based on date range of the field that is select from previous parameter. For example user selects a date field name from first and then date parameters will be cascaded and grab a value of date field based on the date field name that is selected in previous parameter. But I'm not sure exactly how to approach this. Any ideas?
I do that in some of my reports. The first parameter is "Does the range apply to A, B, or C" and the second and third parameters are the start and end data respectively. Well, I use integers, but dates should work the same as long as you format them.
The way it works, is you set the query in your dataset to be a function, and build it as mostly a quote but with the parameter values substituted in. A typical one might be
= "SELECT * FROM dbo.Trips WHERE " + Parameters!WhatField.Value + " between '" + FormatDateTime(Parameters!StartDate.Value, dateformat.shortdate) + "' and '" + FormatDateTime(Parameters!EndDate.Value, dateformat.shortdate) + "'"
Your parameter "WhatField" is a drop down list with 3 permitted values, make the value be the field name and the display be what your user wants to see as a description of the field.
I think you have to set the query for delayed evaluation somewhere (but I can't spot where right now, so maybe I'm mis-remembering), and you should set default values for your parameters that don't crash your report, but other than that it's fairly straightforward.
Oh, and to make the query a function it's just like a text query but hit the button to the right of the text box - it has a "fx" on it
If you need an even more complex query, you can put the whole query text in code (off the report properties) and call that function from the "fx" button to generate your query string.
I'm at my wits end here, I'm trying to get this to work
Set rex2 = db.OpenRecordset(" Select count(*) from events where event_date >= #" & Format(last_week_start, "mm/dd/yyyy") & "# and maildate <= #" & Format(last_week_end, "mm/dd/yyyy") & "# and contains(event_type, ""1st call attempted"") and work_ID contains ""UNS"";")
where event_type is the column name in the database and work_ID is also the column name in the database. I've tried it in numerous ways i.e.
WHERE event_type contains ""1st Call Attempted"
etc but I'm having no luck.
I'd change my code but in event_type there are way too many 1st call attempted categories to list.
I'm also open to using a left statement ie
Where left(event_type, 18) = " 1st Call attempted"
Anything to get this sodding thing working
Please help me.
Perhaps you should use the LIKE operator. You'd need to do something like this:
... WHERE event_type LIKE '1st Call attempted%' ...
and similarly for work_ID.
This will match any string that starts with "1st Call attempted" and ends in whatever, since it's like a * wildcard. If you executed it in Access, then you'd use a * instead of a %, but in OLEDB you need to use a %.
I have a db in Access and I'm trying to get a textbox to run my query and pass an other bounded textbox's value in as the criteria in DLookUp. I have the query running in design view and when I enter the criteria directly it returns the correct results. When I open the report it gives me the sum of all the possible rows. In other words it doesn't filter the rows.
I haven't used Access in about twelve years, thankfully, and everything I've done up to this point has been tutorial/example patchwork, but here it is...
SQL Query:
SELECT Sum(IIf(Attended=-1,1,0)) AS attendance
FROM Students_Classes_Attendance
WHERE (((CStr([Students_Classes_Attendance].[Class_Id]))=[classId]));
DLookUp as Control Source:
=DLookUp("[Total Attendance by Class]![attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
I'm lost at the moment. I'm guessing that the value isn't there before the query fires and since the criteria is an optional parameter that it's being passed null, but I would hope you'd get an error from that. Not that #Error is very meaningful anyway.
Does anyone know for certain the problem and the best way to correct it? Thanks.
Edit:
I did the changes recommended in the answer so now my DLookUp looks like...
=DLookUp("[attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
...still returns the total for all rows. Removing the criteria completely makes no difference either, which returns me to thinking it has something to do with the bound textbox not having a value.
DLookup uses the following syntax:
Syntax for numerical values:
DLookup("FieldName" , "TableName" , "Criteria = n")
Syntax for strings: (note the single apostrophe before and after the string value)
DLookup("FieldName" , "TableName" , "Criteria= 'string'")
Syntax for dates: (note the # before and after the date value)
DLookup("FieldName" , "TableName" , "Criteria= #date#")
I believe you just need to remove the table name from the first parameter. Try this:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = " & [Class_Id])
Keep in mind that if Class_Id is a Text Field, you need to surround it by single quotes:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = '" & [Class_Id] & "'")