Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use a SQL Statement with more than one variable from a form

Posted on 2011-09-19
4
Medium Priority
?
218 Views
Last Modified: 2012-06-21
I have a report where the record source is a SQL statement that uses variables from a form.

The criteria for the SQL Statement/Query of the Report is using the choices from the combo boxes and/or text boxes on the form (frmReports).

I started out with just 4 (cboReportCateg, cboCategSelect, txtBeginDT and txtEndDT) combo and text box choices as the criteria, and this was working fine.  I then added a fifth combo box (cboDept) and for some reason it doesn't recognize the choice in the 5th combo box, so it is not pulling the correct data (so if the user makes a selection in the 3 combo boxes cboReportCateg, cboCategSelect, cboDept (and they may or may not choose dates, the data is pulling everything, and not just the data that matches the Dept that is selected.

I have posted the report's SQL statement in the code box below.  I have the OR statement with the txtBeginDT and txtEndDT criteria as NULL because I want the users to be able to run the report without any dates, if necessary.  Otherwise I allow for them to put in dates if they want to also.

Please review the SQL statement of the report and let me know what I am doing wrong.

Thanks,

gdunn59
SELECT
tblEmployee_Audits.Employee,
tblEmployee_Audits.Manager_Name,
tblEmployee_Audits.Quality_Review_Date,
tblEmployee_Audits.Auditor_Name,
tblEmployee_Audits.Audit_Type,
tblEmployee_Audits.Department,
tblEmployee_Audits.Region,
tblEmployee_Audits.System,
tblEmployee_Audits.InquiryNum,
tblEmployee_Audits.Audit_Date,
tblEmployee_Audits.Audit_Notes
FROM tblEmployee_Audits
WHERE (((tblEmployee_Audits.Employee)=Forms!frmReports!cboCategSelect)
And
((tblEmployee_Audits.Quality_Review_Date) Between Forms!frmReports!txtBeginDT And Forms!frmReports!txtEndDT))
And (((tblEmployee_Audits.Department)=Forms!frmReports!cboDept))
Or 
(((tblEmployee_Audits.Employee)=Forms!frmReports!cboCategSelect))
And 
(((tblEmployee_Audits.Department)=Forms!frmReports!cboDept)
And
((Forms!frmReports!txtBeginDT) Is Null)
And
((Forms!frmReports!txtEndDT) Is Null));

Open in new window

0
Comment
Question by:gdunn59
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
hnasr earned 2000 total points
ID: 36563457
Try to reduce the query using few columns.

Select f1, f2 ....

Where ((True) AND (True)) OR ((True) AND (True))



The problem can be in the Where clause.
Revise your statement:
Where () OR ()
Where (() AND ()) OR (() AND ())
Fill in the inner brackets.
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 36563807
Also, if you may have NULLs in columns, move the test for NULL so that it is PRIOR to the tests that expect an actual value because testing for a vlue first can cause issues.
0
 
LVL 1

Author Comment

by:gdunn59
ID: 36584086
I tied hnasr's suggestion and that didn't work.  Not sure if I'm just not doing the SQL Statement/Query correct or not.

I have added to the Code section my SQL Statement/Query.

Thanks,
gdunn59

SELECT tblEmployee_Audits.Employee, tblEmployee_Audits.Manager_Name, tblEmployee_Audits.Quality_Review_Date, tblEmployee_Audits.Auditor_Name, tblEmployee_Audits.Audit_Type, tblEmployee_Audits.Department, tblEmployee_Audits.Region, tblEmployee_Audits.System, tblEmployee_Audits.InquiryNum, tblEmployee_Audits.Audit_Date, tblEmployee_Audits.Audit_Notes
FROM tblEmployee_Audits
WHERE (((tblEmployee_Audits.Employee)=[Forms]![frmReports]![cboCategSelect]) AND ((tblEmployee_Audits.Quality_Review_Date) Between [Forms]![frmReports]![txtBeginDT] And [Forms]![frmReports]![txtEndDT]) AND ((tblEmployee_Audits.Department)=[Forms]![frmReports]![cboDept])) OR (((tblEmployee_Audits.Employee)=[Forms]![frmReports]![cboCategSelect]) AND ((tblEmployee_Audits.Department)=[Forms]![frmReports]![cboDept]) AND (([Forms]![frmReports]![txtBeginDT]) Is Null) AND (([Forms]![frmReports]![txtEndDT]) Is Null));

Open in new window

0
 
LVL 31

Expert Comment

by:hnasr
ID: 36584182
Try to attach a simple database with relevant test data.
Show the expected output as a table.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question