Solved

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

Posted on 2011-09-19
4
206 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
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
hnasr earned 500 total points
Comment Utility
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
Comment Utility
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
 

Author Comment

by:gdunn59
Comment Utility
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 30

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now