I have a query that is using a form named "DateRange2" to change the criteria of one field "Dept1" in order to filter this query. Then the I designed a report that can be opened by using a command button 'cmdOpenReport' with form VBA code:
Option Compare DatabasePrivateSub Form_Open(Cancel AsInteger)
Me.Caption =Me.OpenArgs
EndSub
PrivateSub cmdOpenReport_Click()
If IsNull([SrchDateFrom])Or IsNull([SrchDateTo])Then
MsgBox "Please enter your date range.", _
vbInformation + vbOKOnly,"No Date Range!"
DoCmd.GoToControl "SrchDateFrom"
Else
If [SrchDateFrom] > [SrchDateTo] Then
MsgBox "Date 'To' Value is earlier that Date 'From', please enter logic date range.", _
vbInformation + vbOKOnly,"Illogic Date Range!"
DoCmd.GoToControl "SrchDateFrom"
Else
Me.Visible =False
EndIf
EndIf
EndSub
And My question is: How to to change the criteria of a query depending on a form? to show only the entries of the selected department; however, in case of NO department is selected, then I want the report to show all the entries!
Adding that; when I manually set the criteria to: Like [Forms]![DateRange2]![SrchDept] & "*", then it shows all the entries. But alternatively when I set the criteria to: [Forms]![DateRange2]![SrchDept], then it shows the selected department only. However, this doesn't solve my problem because I actually want to shift between the two cases by using the form named "DateRange2".
Please help me in a very simple way as I am just a beginner.
Also I was trying something like:
Expr1: IIf(IsNull([Forms]![DateRange2]![SrchDept]), [Correspondence]![Dept1] = Like ([Forms]![DateRange2]![SrchDept]) & "*",[Correspondence]![Dept1] =([Forms]![DateRange2]![SrchDept])),
but this doesn't work with me!
Please help me!