Filtering Microsoft Access Subform - ms-access

I have a subform populated with different tasks. I want to filter them on DUE_DT with a filter that happens automatically. Some of the records have a blank DUE_DT, which I would like to filer out. I would also like to filter out records whose DUE_DT is more than 30 days past. I tried using VB but I haven't been able to find the appropriate syntax.

Here is some syntax for appplying a filter to a sub form:
Forms!MyMainForm.ASubFormOnMyMainForm.Form.Filter = "whatever your filter is"
Forms!MyMainForm.ASubFormOnMyMainForm.Form.FilterOn = True

"I want to filter them on DUE_DT with a filter that happens automatically."
I'm not quite sure what you have in mind there, but I wonder whether that could be handled by a WHERE clause in the subform's record source query.
WHERE DUE_DT >= Date() - 30
That would be automatic in the sense that the filter would always apply.
Note you don't have to add another condition to explicitly exclude Nulls (such as DUE_DT Is Not Null) because Null is not >= Date() - 30.

Try this in VBA, should work on every form or module you put it in:
Form_subFormName.Filter = "DUE_DT IS NOT NULL AND DUE_DT >= #" & (Date - 30) & "#"
Form_subFormName.FilterOn = True
Replace the subFormName in Form_subFormName with the name of your subform, but keep the Form_.

Related

Group projects by Fiscal Year Date expression based on one field value, if field value is null then group projects based on another field value

I am running a report where the projects are grouped by FY based on Fields!EstSubstantial_Completion.Value. Expression below:
=IIf(Month(Fields!EstSubstantial_Completion.Value)=10,
year(Fields!EstSubstantial_Completion.Value)+1,
IIf(Month(Fields!EstSubstantial_Completion.Value)=11,
year(Fields!EstSubstantial_Completion.Value)+1,
IIf(Month(Fields!EstSubstantial_Completion.Value)=12,
year(Fields!EstSubstantial_Completion.Value)+1,
year(Fields!EstSubstantial_Completion.Value))))
The expression is working, but my supervisor would like the projects to first be grouped into a FY based on another date field Fields!Savings_Report_Date.Value first and then if the field is blank (null) reference the Fields!EstSubtantial_Completion.Value field as the date to determine FY grouping.
I am new to SSRS reports so I am unsure if there is a way to write this type of expression.
Thanks!
The easiest way to do this would be to add a calculated field to your dataset.
Go to the dataset properties, then the "Fields" tab, then add a new field, select calculated when prompted.
Give the field a name such as FYCalc and set the expression to
=IIF(Fields!Savings_Report_Date.Value = Nothing, Fields!EstSubtantial_Completion.Value, Fields!Savings_Report_Date.Value)
Now all you need to do is swap out Fields!EstSubtantial_Completion.Value in your current expression and use Fields!FYCalc.Value instead.
Optional :
When you have nested IIF statements, it's often easier to use the SWITCH function instead. It's much easier to read.
=SWITCH(
Month(Fields!FYCalc.Value)=10, year(Fields!FYCalc.Value)+1,
Month(Fields!FYCalc.Value)=11, year(Fields!FYCalc.Value)+1,
Month(Fields!FYCalc.Value)=12, year(Fields!FYCalc.Value)+1,
True, year(Fields!FYCalc.Value)
)
The final True acts like an Else
You could simplify this further like this
=IIF(
Month(Fields!FYCalc.Value) >= 10 and Month(Fields!FYCalc.Value) <= 12, year(Fields!FYCalc.Value)+1,
, year(Fields!FYCalc.Value)
)

Access Criteria IIF Statement Issue

I'm trying to qualify a field based on a date. If the frequency of the data is supposed to be weekly, I want to select one specific weekend date. If the frequency is monthly, I want the frequency to include the specific weekend date and all weeks thereafter. However, my statement below returns a blank table when False ("Monthly"). I'm guessing there's some specific formatting I have to do to the >= but I'm drawing a blank. Any suggestions?
=IIf([Frequency]="Weekly",[WK_end_Date],>=[WK_end_Date])
FYI... the false statement works correctly when I input only that specific criteria without the IIF statement.
Thanks,
Mark
You can't use IIf this way. Correct your Where clause like this:
Where
([Frequency] = "Weekly" And [YourDateField] = [WK_end_Date])
Or
([Frequency] <> "Weekly" And [YourDateField] >= [WK_end_Date])

ms Access '16 - Continuous form - OrderBy textbox value created with expression

I have a form that shows the records from Table-A, it is a continuous form. I have a textbox that uses a DCount expression to count records from Table-B that share the same SOP-Number.
=DCount("*","[Table-B]","SOP = " & [SOP])
This works but I don't know how I would go about sorting that column based of the resulting values. The other columns are sorted thus:
" ORDER BY [FIELD NAME] ASC;"
So without a field I don't know how (or if) I can sort the form.
Could I create a RecordSet to store the values then sort by that field (I don't know if this is even possible)
Thank you
Dan
Access 2016 (365)
I'm not quite understanding why you won't know what fields you're pulling from Table B. But you can always order by the column number. So if you're looking to order by the first column, just put:
ORDER BY 1 ASC
You can't sort the recordsource but you can sort the form itself:
Me.OrderBy = "NumTasks DESC"
Me.OrderByOn = True
(assuming your textbox with the DCount control source is named NumTasks ).
You could change the datasource to
select ,DCount('''[Table-B],'SOP=' & SOP) from [Table-A]
order by DCount('*''[Table-B],'SOP=' & SOP)
This is not a good soulution i Table-A is big.

query to filter on specific data or no filter if blank

I have a query which filters records based on dates (start date and end date)selected in a previous form. I want the query to filter the specific date range, or output all records if the fields are left blank.
I am unfamiliar with SQL. is there a way to add an if-then statement?
I can use vba if necessary, but would like to use the Access GUI if it is possible.
If you have a parameter, used in WHERE clause (Criteria in query builder) and you want to show all records if parameter is empty, just add this parameter as new column and OR condition where indicate Is Null or, better add a column with expression Nz([MyParam],"") and in Condition area inORrow add""`. Unfortunately in query builder this construction may be quite complicated if you have few parameters, in SQL it looks much simpler, for instance in your case it will be something like this:
WHERE (MyDate >= [paramDateStart] and MyDate <= [paramDateEnd])
OR (Nz([paramDateStart],"")="" AND Nz([paramDateEnd],"") = "")
Sometimes simpler edit SQL and then switch to Design view
You can use these criteria for StartDate and EndDate respectively to compare them to themselves in case one (or both) of the search fields on the form is empty (Null):
>=Nz([Forms]![YourForm]![FromDate], [StartDate])
<=Nz([Forms]![YourForm]![ToDate], [EndDate])

If then record selection with multiple conditions

Using Crystal Reports 2011 to reference a View
My formula written as a "formula field"
#mySelection
IF {V_JOB.TASK} LIKE "*IN"
AND {V_JOB.CLOSED} = "Y"
AND {V_JOB.DATE} >= {?FrDate}
AND {V_JOB.DATE} <= {?ToDate}
THEN {V_JOB.JOB} ELSE "FALSE"
My record selection written in the "Record Selection Formula"
{V_JOB.LMO} = 'L' AND
{#mySelection}
This View contains several relevant fields. To make my record selection of the view, I want to display all records that are equal to string in .JOB, when string in .TASK like "*IN" and field .DATE = ?myDateRange and field .CLOSED = 'Y'
so I wrote the equation to do exactly that, but the displayed records are row JOB only when TASK,DATE,CLOSED are true. But I have multiple rows of the same JOB where TASK,DATE,CLOSED is false that I also want to see.
So if there are 30 records for Job A and only 1 of those records has #mySelection is "true" then I want to select ALL 30 records even if the other 29 are "false". The way it is written it only displays the 1 true record and not the other 29.
Can anyone provide some assistance on what I'm doing wrong? Is there a "show all" command or perhaps I can save "true" JOBs in an array and then reference the array as my record selection?
Where abouts are you inputting your formula within crystal? It is very important.
You should be able to achieve what you want by using conditional suppression in the details section of your report. Something like this...
//Untested//
IF {V_JOB.TASK} LIKE "*IN"
AND {V_JOB.CLOSED} = "Y"
AND {V_JOB.DATE} >= {?FrDate}
AND {V_JOB.DATE} <= {?ToDate}
THEN False ELSE True
This will suppress results where they do not equate to the above conditions.
I found your question a little unclear so this answer might not be perfect for your needs, but hopefully it's a shove in the right direction.
I was able to accomplish this using a sub-report to record select with a parameter reference from the main report
{V_JOB.LMO} = 'L' and
{V_JOB.JOB} = {?Pm-V_JOB_OPERATIONS.JOB}
Then I created a main report and linking a parameter to Job.Job in the sub report and did a record selection for my criteria in the main report.
{V_JOB.TASK} LIKE "*IN"
{V_JOB.CLOSED} = "Y"
{V_JOB.DATE} >= {?FrDate}
{V_JOB.DATE} <= {?ToDate}
I then put the sub-report in the details section but that created a duplicate record issue if there was more than one record for Job.Job that satisfied my selection. So to fix that I created a group for Job.Job, and put the sub-report in the group header and suppressed the detail.
I'm not sure this is the most efficient use of CPU power or the best way to program this in CR, but it gives me the correct results that I need, and relatively quickly for the size of the database.
Your Record Selection forumula should be something like this.
{V_JOB.LMO} = 'L' AND
{V_JOB.JOB}
Now you have all the records in your CR.
Use the below formula as a supression option for the field where you are displaying records.
#Supress
IF {V_JOB.TASK} LIKE "*IN"
AND {V_JOB.CLOSED} = "Y"
AND {V_JOB.DATE} >= {?FrDate}
AND {V_JOB.DATE} <= {?ToDate}
THEN true ELSE false
If your condition is true then all records will be displayed else all records will be supressed.
I would suggest you to not to apply these type of conditions in Record selection formula.