1

I am trying to get the following result in a query.

SELECT * FROM rosterTbl
WHERE (IIF( ISNULL([Forms]![ReportsGUI]![cmbCounsellor]), rosterTbl.CounsellorID<>null,rosterTbl.CounsellorID=[Forms]![ReportsGUI]![cmbCounsellor]

I know the above query is incorrect but I need the result: If cmbCounsellor is Null Then all counsellors Else specific counsellor.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Umair Ansari
  • 418
  • 3
  • 15

2 Answers2

2

Perhaps something like this will work:

="SELECT * FROM rosterTbl WHERE CounsellorID " & IIf(IsNull([Forms]![ReportsGUI]![cmbCounsellor], "IS NOT NULL", "= " & [Forms]![ReportsGUI]![cmbCounsellor])

edit re: comments

In this particular case the WHERE condition will be used to control the records to be included in a report, so the compete SELECT statement is not really required. Instead, the "WHERE" clause can be passed to the report via the WhereCondition argument of DoCmd.OpenReport, something like this:

DoCmd.OpenReport "MyReport", acViewPreview, , "CounsellorID " & IIf(IsNull([Forms]![ReportsGUI]![cmbCounsellor], "IS NOT NULL", "= " & [Forms]![ReportsGUI]![cmbCounsellor])
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
0

This is the way to do this within query:

SELECT * from rosterTbl 
WHERE [Forms]![ReportsGUI]![cmbCounsellor] IS  NULL
OR 
rosterTbl.CounsellorID 
= ([Forms]![ReportsGUI]![cmbCounsellor])
Umair Ansari
  • 418
  • 3
  • 15