Solved

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

Posted on 2011-09-19
4
209 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
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
 

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 30

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
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…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

863 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

24 Experts available now in Live!

Get 1:1 Help Now