Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
AND OR Logic in Query or Form
Jeff 

15 years ago
I am trying to run a query with criteria specified on a form. The form has four combo boxes from which users can choose values. (Based off the contents of the queried table.) Users will not necessarily specify answers in all four boxes. I would like the query to run the four different criteria with OR logic unless two or more boxes have answers specified. In that case, I want it to treat only the answered boxes as AND searches and ignore any unanswered boxes. If it helps, this will be used to look up sales quotations.




Richard Says:

This is going to be a little tricky. Your best bet would be to set up a LIST BOX on your form and then dynamically fill in the RowSource property of the list box with the data that you want using a little VBA.

For example, let's say you have two text boxes FirstName and LastName. You want the user to be able to search based on either or both of these fields. Put a command button on your form that loads the data into your list box like this:

If IsNull(FirstName) AND IsNull(LastName) Then
   MsgBox ("You must enter one!")
   Exit Sub
End If
If IsNull(FirstName) then ' just search on last name
  MyListBox.RowSource = "SELECT * FROM CustomerTable WHERE FirstName='" & FirstName & "'"
ElseIf IsNull(LastName) then ' just search on first name
  MyListBox.RowSource = "SELECT * FROM CustomerTable WHERE LastName='" & LastName & "'"
Else ' search on both names
  MyListBox.RowSource = "SELECT * FROM CustomerTable WHERE LastName='" & LastName & "' AND " & _
       FirstName='" & FirstName & "'"
End If

So as you can see, using a little VBA logic, you can get quite creative with your parameters. If you have four different boxes and you want to mix AND and OR conditions, then you can lay all of that down in your code.

I cover this in GREAT detail in my Access 311 course. It's called Dynamic SQL, and it basically involves changing the data under an object (list box, combo box, form, etc.) using your VBA code and some SQL statements.

Here's a link for more information:

http://599cd.com/site/courselist/access/access311?key=AlExBlg

Hope this helps.

Richard

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/22/2026 2:02:42 PM. PLT: 0s