Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 14th, 2005, 10:56 AM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multiple field query PLUS

I want to create a SQL query for an Access database. I want the query to return records based on parameters entered by the user, unless no parameter for a particular field is entered, in which case it would return all records where that field exists.

Example (and I hope this is clear):

The database is a list of documents of county records, containing a document name, a county name, a document summary, etc. I want the user to be able to look up records either by county, or document name, or both. So the query form will request the user to enter a parameter in the appropriate field. If they enter no parameter, the query is to assume all records containing any value for that field are being requested.

Some one suggested the statement below, but I get an
"Invalid use of vertical bars in expression" error.

Anyone know the correct Access syntax?

select * from records
where dName like '%' || nvl('&dName','') || '%'
and county like '%' || nvl('&county','') || '%';

Thanks for your help,

Durwood

  #2 (permalink)  
Old July 14th, 2005, 11:01 AM
Friend of Wrox
 
Join Date: Jan 2005
Location: Kansas City, Missouri, USA.
Posts: 471
Thanks: 0
Thanked 1 Time in 1 Post
Default

Durwood,

How is the user choosing the parameters?

Kevin

dartcoach
  #3 (permalink)  
Old July 14th, 2005, 03:17 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It will be done in ASP. What I need is an example of how to query an Access database so that if no parameter is passed it assumes that all data in the field satisfies the query.

Thanks!
  #4 (permalink)  
Old July 15th, 2005, 03:02 PM
Registered User
 
Join Date: Oct 2004
Location: Tupelo, MS, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dmichael
Default

Would something similar to this work?

If the fields queried were A, B, and C.

Select * from mytable
where ((mytable.A = form.A) OR isnull(form.A)) AND
((mytable.B = form.B) OR isnull(form.B)) AND
((mytable.C = form.C) OR isnull(form.C));

This way, if the Field on the form contains data it will be used to select data. If the field on the form is empty, that field is ignored for the purposes of the query.

Hope this helps.
:)
Darron
  #5 (permalink)  
Old July 18th, 2005, 11:57 AM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Bingo! That may well be it.
Thanks,

Durwood

  #6 (permalink)  
Old July 26th, 2005, 09:08 AM
Authorized User
 
Join Date: Jun 2004
Location: , , USA.
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i think durwood would benefit by learning some VBA. I think it's bad practice to refer to forms in queries. Just think... The more forms you have, the more form specific queries you need. You should just have a couple of general information queries, and use VBA to filter them.

"Life is a Database"


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Values In One Field lefty4080 Access 1 December 12th, 2007 08:59 AM
Multiple Field in an Event MRuzly Access VBA 6 September 5th, 2007 06:45 AM
multiple field search ddnk77872 PHP Databases 0 May 15th, 2007 02:55 AM
multiple checkbox store one field mateenmohd Classic ASP Basics 0 June 11th, 2006 01:39 AM
Multiple Joins in Multiple Table Search query pookster Access 4 September 23rd, 2004 03:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.