473,386 Members | 1,819 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,386 software developers and data experts.

Issues with Search Form = IIF Statements

I'm having a brain blockage day....

Scenario:

Search Form with 6 text boxes
Query has same six fields
Each has this IIF: IIf(IsNull([Forms]![CashItems1]![District]),"",[Forms]![CashItems1]![District])
with each dictating the correct text box of course.

SQL dictates... If text box 1 isNull do nothing or do this OR if text
box 2 isNull do nothing or do this... etc... works fine if I only
enter search criteria into one of the six fields on the form.... but
when I try to enter criteria into 2 or more... its hosed. if I use
AND... they'll all need to have criteria entered. They would like it
so if they happen to have 3 pieces of information to look up a Members
Charge-offs they'll be able to.

Did I make sense? Would greatly appreciate any help...

Christa.
Nov 13 '05 #1
2 2214
mi************@yahoo.com (misschristalee) wrote in message news:<5c**************************@posting.google. com>...
I'm having a brain blockage day....

Scenario:

Search Form with 6 text boxes
Query has same six fields
Each has this IIF: IIf(IsNull([Forms]![CashItems1]![District]),"",[Forms]![CashItems1]![District])
with each dictating the correct text box of course.

SQL dictates... If text box 1 isNull do nothing or do this OR if text
box 2 isNull do nothing or do this... etc... works fine if I only
enter search criteria into one of the six fields on the form.... but
when I try to enter criteria into 2 or more... its hosed. if I use
AND... they'll all need to have criteria entered. They would like it
so if they happen to have 3 pieces of information to look up a Members
Charge-offs they'll be able to.

Did I make sense? Would greatly appreciate any help...

Christa.


I have a search form with many search fields. In order to build the
SQL string I need to have a criterion for the first field no matter
what so that there's always a WHERE and something following it. The
first field is a text field so I use (in form code):

If Not IsNull(txtDistrict.Value) Then
strWhere = " WHERE [District] LIKE '" & txtDistrict.Value & "'"
Else
strWhere = " WHERE (([District] LIKE '*') OR ([District] Is Null))"
End If

The second strWhere simply returns everything. So your IIF statements
could look something like:

YourQuery.SQL = "SELECT * FROM tblSearch " &
"IIf(IsNull(Forms!CashItems1!District.Value), 'WHERE (([District] LIKE
'*') OR ([District] Is Null))', 'WHERE [District] LIKE '" &
Nz(Forms!CashItems1!District.Value,"") & "')" &
IIf(IsNull(Forms!CashItems1!County.Value), '', ' AND [County] LIKE '"
& Nz(Forms!CashItems1!County.Value, "") & "')"

and so on for the six search fields. Note: I have not tested the
string above. It's not very pretty but it fits in with the way you
are trying to do things. I don't know if the Nz's are needed on the
Not IsNull side of the IIf but it doesn't hurt to have them.

James A. Fortune
Nov 13 '05 #2
Hi Christa,

I'd suggest building an SQL string in code, where you can append as many
"AND's" as you need.
The other thing that I like to do is to display the result in a
datasheet-style subform ("sbfCustomers" in the example below), rather than
having to open an external query.

*Notes:
You will have to edit this code to include your control names, etc.
The following code from my form uses two combo-boxes to filter the data. I
use combo-boxes so that I can select from *existing* values.

Here is the SQL used as the Row Source of the 2 combo-boxes (which in this
case are purposely named the same as the fields that I want to search ...
"FirstName" and "LastName")
These combo-boxes both also use a UNION SELECT statement to add "(All)" to
their RowSource... http://www.mvps.org/access/forms/frm0043.htm
SELECT DISTINCT tblCustomers.FirstName From tblCustomers UNION Select
"(All)" as Bogus FROM tblCustomers;
SELECT DISTINCT tblCustomers.LastName From tblCustomers UNION Select
"(All)" as Bogus FROM tblCustomers;
(Using the "SELECT DISTINCT" as above in the combo-box's RowSource
eliminates duplication of any of the names)

You call this procedure from each of your combo-box's AfterUpdate event. The
easiest way is to just type "= fRequerySubform()" in each of the combo-boxes
"AfterUpdate" field directly in their respective property sheets.

The beauty of this approach is it's flexibility;
Null - valued combo-boxes (or combo-boxes with "(All)" selected) are
ignored, and are not appended to the WHERE portion of the SQL string.
If only one combo-box "qualifies", the "AND" is dropped.
If NO combo-boxes are specifying criteria, ALL records are returned.
All results are displayed immediately ... right in the subform!

Here is the (tested and working) code sample:
**************************************************
Option Compare Database
Option Explicit

Public Function fRequerySubform()
'What I want to do here is build the RecordSource for the subform
'ON-THE-FLY...to allow for a variable WHERE statement.

Dim MySQL As String
Dim whr As String

Dim Msg As String
Dim CR As String
CR = vbCrLf

Dim ctl As Control

'Build the SELECT portion of the SQL statement
MySQL = ""
MySQL = MySQL & "SELECT tblCustomers.* "
MySQL = MySQL & "FROM tblCustomers "

'Build the WHERE portion
whr = "" 'initialize variable

For Each ctl In Me.Controls
If ctl.ControlType = acComboBox Then
If Not IsNull(ctl) And InStr(1, ctl, "(All)") = 0 Then
whr = whr & "(tblCustomers."
whr = whr & ctl.Name
whr = whr & " = "
whr = whr & Chr$(39) & ctl & Chr$(39) 'This assumes that "ctl"
contains Textual data. You may need to edit this if not Text.
whr = whr & " ) AND "
End If
End If
Next ctl

If Len(whr) > 0 Then
'Lose the last 5 characters (" AND ")
whr = Left$(whr, Len(whr) - 5)
'Insert the WHERE (If there is one) into the SQL statement...
MySQL = MySQL & "WHERE (" & whr & ") " & CR
End If

MySQL = MySQL & "ORDER BY tblCustomers.LastName, tblCustomers.FirstName "
MySQL = MySQL & ";"

'Debug.Print MySQL
Me![sbfCustomers].Form.RecordSource = MySQL

End Function

*****************************************
Here are 2 samples of the SQL that is generated by this function, and is
used as the subform's Recordsource:

SELECT tblCustomers.* FROM tblCustomers WHERE ((tblCustomers.FirstName =
'Don' ))
ORDER BY tblCustomers.LastName, tblCustomers.FirstName ;

SELECT tblCustomers.* FROM tblCustomers WHERE ((tblCustomers.LastName =
'Leverton' ) AND (tblCustomers.FirstName = 'Don' ))
ORDER BY tblCustomers.LastName, tblCustomers.FirstName ;
--
HTH,
Don
=============================
Use My*****@Telus.Net for e-mail
Disclaimer:
Professional PartsPerson
Amateur Database Programmer {:o)

I'm an Access97 user, so all posted code
samples are also Access97- based
unless otherwise noted.

Do Until SinksIn = True
File/Save, <slam fingers in desk drawer>
Loop

================================


"misschristalee" <mi************@yahoo.com> wrote in message
news:5c**************************@posting.google.c om...
I'm having a brain blockage day....

Scenario:

Search Form with 6 text boxes
Query has same six fields
Each has this IIF: IIf(IsNull([Forms]![CashItems1]![District]),"",[Forms]![CashItems1]![Distric
t]) with each dictating the correct text box of course.

SQL dictates... If text box 1 isNull do nothing or do this OR if text
box 2 isNull do nothing or do this... etc... works fine if I only
enter search criteria into one of the six fields on the form.... but
when I try to enter criteria into 2 or more... its hosed. if I use
AND... they'll all need to have criteria entered. They would like it
so if they happen to have 3 pieces of information to look up a Members
Charge-offs they'll be able to.

Did I make sense? Would greatly appreciate any help...

Christa.

Nov 13 '05 #3

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

Similar topics

2
by: James | last post by:
Hi there, I'm trying to create a simple DVD shopping cart. I have implemented a way to browse by genre and am trying to implement a way to search by selecting title, director and actor and typing...
8
by: horos | last post by:
hey all, Ok, a related question to my previous one on data dumpers for postscript. In the process of putting a form together, I'm using a lot of placeholder variables that I really don't care...
1
by: TH | last post by:
I am (still :) working on a recipe database. Now I am trying to figure out how to set it up for an ingredient search. What I want it to be able to do is three things: 1. Search based on just...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
4
by: keithsimpson3973 | last post by:
Please forgive me for being so stupid. I have searched this site and many others for what should be a simple thing. I have a vb 6 form with a textbox that I input a date into. I can't set the format...
2
by: caine | last post by:
I'm doing a search application for my project. My code can prompt alert popup window when the user doesn't key in any keywords. However, if the user keys in any keywords, it juz return "Please...
3
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I have a VB app, and I have been working at it for a while, and I am now at the stage where I want to create a search function. Now don't be scared! It is in the .Net compact framework,...
2
by: time_error | last post by:
Please bear with me - I’m quite new to MSSQL and the whole db domain. The db itself is pretty simple. There are approx. 15 tables. The two largest tables’ holds a total of 10 mill. entries. ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.