473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with IIf statement in the OR clause

I have been trying to build a user friendly search engine for a small
database I have created. I'm having some particular problems with one
of my date fields.

Here's the setup:

I'm using Access 97 (I know it's old. But, it's the tool they
give me to work with)
My working knowledge of SQL is on the low side.
My working knowledge of VBA is beginner.

I have three tables that concern this query. The main table is called
'tblBenIssues' and is the central table. Each instance of
'BenIssues' can have many comments (stored in tblComments') and
many locations (stored in 'tblDPTOwnerHist').

I need this query to read inputs from 38 different search criteria
fields on a search screen (those fields reference the three tables
listed above), and then display the results of that query on a results
summary form.

My main problem has always been dealing with null fields. To get around
the problem of null values not being returned when criteria fields were
blank, I created a small function which inserts a "*" into a
criteria string. I then set all my text fields to accept zero length
strings, and defaulted them to "". The query I build then calls the
function from the criteria line, and even blank fields are returned
with the "*" criteria.

I have three queries. The first query searches the comments table for
matches, then sends the matches on to another query which checks those
matches for location history matches. The results of this 2nd query are
then sent to a 3rd and final query, which matches the criteria vs. the
main table.

This works perfectly (so far as I can tell) for all of my fields except
date fields. For those I've always just had the functions dummy in
dates that will capture every single record (I.E. 01/01/1801 -
01/01/2099). This takes care of 3 out of the 5 date fields I have.
However, 'Date Closed' (date issue was closed) and 'End Date'
(Time stamp of when an issue left a department) can sometimes be null.

Now we get to my problem. I thought I could easily take care of this by
putting the following criteria in the Access Query builder.
CRITERIA: Between [forms]![frmsearch]![DateEnd1] And
[forms]![frmsearch]![DateEnd2]
OR: IIf([forms]![frmsearch]![dateend1] = #01/01/1801#,Is
Null,#1/1/1801#)

The idea here is that it will search the primary criteria first. If
those criteria fields are dummy dates, 'Is Null' is inserted in the
OR statement so all records will be returned. If the dates are not
dummy dates, then 01/01/1801 (a date for which no matches will ever be
found) is inserted in the OR statement.

The problem? Access does not seem to be evaluating the OR statement.
Even when I set both the True and False parts of the IIF statement to
'Is NULL', the query results behave as if the OR statement wasn't
even there. I tried changing the forms! reference in dozens of
different ways thinking that perhaps it was not evaluating the
information I thought it was, but no dice. However, if I remove the Iif
statement and just say 'Is Null', it evaluates the 'Is Null'
just fine.

Here is a copy paste of the SQL statement from the 2nd query (where the
problem is)

SELECT tblDptOwnerHist.HistID, tblDptOwnerHist.IssueID,
tblDptOwnerHist.OwnerID, tblDptOwnerHist.Department,
tblDptOwnerHist.DateBegin, tblDptOwnerHist.DateEnd,
tblDptOwnerHist.Reason, tblDptOwnerHist.SubReason,
tblDptOwnerHist.SubDepartment
FROM qryBadMojo INNER JOIN tblDptOwnerHist ON qryBadMojo.IssueID =
tblDptOwnerHist.IssueID
WHERE (((tblDptOwnerHist.OwnerID) Like srcOwnerID())
AND ((tblDptOwnerHist.Department) Like srcDepartment())
AND ((tblDptOwnerHist.DateEnd) Between [forms]![frmsearch]![dateend1]
And [forms]![frmsearch]![dateend2]) AND ((tblDptOwnerHist.Reason) Like
srcReason())) OR
(((tblDptOwnerHist.DateEnd)=IIf(IsNull([forms]![frmsearch]![dateend1]),([tblDptOwnerHist].[DateEnd])
Is Null,#1/1/1801#)));

All other parts of the query are working as intended. The only part of
it that isn't working is the part after the OR statement.

Any comments or suggestions will be greatly appreciated!

On an unrelated side note, I have a sinking feeling that I'm going
about this whole process in the most fantastically inefficient way
possible. Comments about efficiency are also welcome =)

Nov 13 '05 #1
3 4594
An***********@bcbsmn.com wrote in
news:11**********************@o13g2000cwo.googlegr oups.com:
I have been trying to build a user friendly search engine for
a small database I have created. I'm having some particular
problems with one of my date fields.
[[SNIP]]
OR
(((tblDptOwnerHist.DateEnd)=IIf(IsNull([forms]![frmsearch]! [dat eend1]),([tblDptOwnerHist].[DateEnd]) Is Null,#1/1/1801#)));

All other parts of the query are working as intended. The only
part of it that isn't working is the part after the OR
statement.
At first glance, you have the statement
([tblDptOwnerHist].[DateEnd]) IS null nested in your IIF
statement.That won't work.

To handle the null, add a field to the grid:
exprN: Isnull([tblDptOwnerHist].[DateEnd]) AND
IsNull([forms]![frmsearch]![dateend1])

Make an entry on a separate criteria row of the word 'true'
without the quotes, which matches when both contain a date and
both are null. You use another criteria row to match the dates.

Any comments or suggestions will be greatly appreciated!

On an unrelated side note, I have a sinking feeling that I'm
going about this whole process in the most fantastically
inefficient way possible. Comments about efficiency are also
welcome =)

It is usually more efficient to build SQL for searching in code,
and only add the filters required.

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #2

Thank you for the response Bob, I will give your suggestion a try.

I considered building an SQL statement by concatenating the parts of
the query together in a string. However, as far as I know a string is
limited to 256 characters. The SQL statements that would result from
this query would typically exceed 256 characters, and I couldn't
think of a way around that limitation.

I'm sure there has got to be a solution for it..

Nov 13 '05 #3
An***********@bcbsmn.com wrote in
news:11*********************@z14g2000cwz.googlegro ups.com:

Thank you for the response Bob, I will give your suggestion a
try.

I considered building an SQL statement by concatenating the
parts of the query together in a string. However, as far as I
know a string is limited to 256 characters. The SQL statements
that would result from this query would typically exceed 256
characters, and I couldn't think of a way around that
limitation.

I'm sure there has got to be a solution for it..

Number of characters in a Text field= 255.

There are two kinds of strings: variable-length and fixed-length
strings.

A variable-length string can contain up to approximately 2
billion (2^31) characters.
A fixed-length string can contain 1 to approximately 64K (2^16)
characters.

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nicolas Payre | last post by:
Hi, I have the following SQL that I want to use to update a table. It doesn't work ! Does someone knows why? ** I Know it could be done easy with a CURSOR FOR LOOP, but still... Thanks for...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
10
by: JMorrell | last post by:
First post to this community so am not sure if this is the correct place. Here goes. I have a MS Access db that keeps track of employees sick and annual leave balances. In it, I have a report,...
14
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN...
15
by: Hemant Shah | last post by:
Folks, We have an SQL statement that was coded in an application many years ago (starting with DB V2 I think). When I upgraded to UDB 8.2, the optimizer does not use optimal path to access the...
3
by: dskillingstad | last post by:
I'd appreciate any help I can get. I'm not sure what I'm doing wrong, but.... I've searched these groups for some solutions but no luck. I have an unbound form (frmSearch), with several unbound...
8
by: Jeff Gilbert | last post by:
Hello all. I'd appreciate some help with this one: First the DDL: CREATE TABLE ( NOT NULL , NULL , NOT NULL , (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , NOT NULL...
2
by: iulian.ilea | last post by:
What is wrong int this SQL statement? select top 10 DOCInt.*, DOCDet.* , Cate.*, Arti.*, .* from DOCInt INNER JOIN DOCDet ON DOCInt.CodDoc=DOCDet.CodDoc LEFT JOIN Cate ON...
10
by: amitabh.mehra | last post by:
Hi I havent used MQT before. Read the online tips and tutorials but none seems to give any hint for my problem. I have a base table (base_table) as: st varchar(25) default...
0
by: aashiss03 | last post by:
hi everyone I m restorimg a database , during which i m facing the peoblem Here is the details description the problem. RESTORE DATABASE NSWD FROM "C:\db2_backups" TAKEN AT...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.