473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

AND/OR query in Access 2007.

111 100+
I have a qury which takes two parameters ProjectId and ResourceCategory. I have created a form which has these two comboboxex for user to enter the parameters. When user hits preview report, this query runs and report bound to this query opens.
I want something like this:
1. If user enters both ProjectID and ResourceCategory both, the query should filter with "ProjectId and ResourceCategory"
2. If user only enters only ProjectID, it should not mind about the Resource Category.
Any hints??
Thanks
Apr 16 '10 #1

✓ answered by patjones

You need to choose the appropriate structure for your WHERE clause depending on the circumstance in question. Let's say your two combo boxes are cboProjectID and cboRsrcCategory. You could do something like

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL_Filter As String
  2.  
  3. If  Not IsNull(Me.cboProjectID) And Not IsNull(Me.cboRsrcCategory) Then
  4.      strSQL_Filter = " WHERE tbl.ProjectID = " & Me.cboProjectID & " AND tbl.ResourceCategory = '" & Me.cboRsrcCategory & "'"
  5. Else
  6.      strSQL_Filter = " WHERE tbl.ProjectID = " & Me.cboProjectID
  7. End If
  8.  
  9. [ReportName].Recordsource = "SELECT tbl.[Field1], tbl.[Field2], ..." &_
  10.                             "FROM tbl" & strSQL_Filter
  11.  

I've assumed that ProjectID is a number and ResourceCategory is text, so you might need to modify the filter with or without ' ' appropriately depending on the field types. I've also written the SQL statement very generically with table name "tbl", field names "Field1", "Field2", etc., so modify it to include whatever your table and field names are, what you want in your report, etc.

I would also set the "Limit To List" property on the combo boxes to Yes; this will prevent the user from typing whatever they want into the boxes, and therefore help you avoid SQL injection. Otherwise the query needs to be modified with parameters to avoid SQL injection attacks.

Pat

6 2024
yarbrough40
320 100+
In the onclick event of your button do a contitional statement....

Expand|Select|Wrap|Line Numbers
  1.  'first type out the two possible query string variables
  2.  
  3. Dim Qstring1, Qstring2 as String
  4.  
  5. Qstring1 = "SELECT [MyTable]].[ProjectId], [MyTable].[ResourceCategory] FROM [MyTable];"
  6.  
  7. Qstring2 = "SELECT [MyTable]].[ProjectId] FROM [MyTable];"
  8.  
  9. 'then do your condition and chose your data source for your report
  10.  
  11. if ResourceCategory.value = "" then
  12. Report1.RecordSource = Qstring2
  13. Else
  14. Report1.RecordSource = Qstring1
  15. End If
  16.  
Apr 16 '10 #2
patjones
931 Expert 512MB
You need to choose the appropriate structure for your WHERE clause depending on the circumstance in question. Let's say your two combo boxes are cboProjectID and cboRsrcCategory. You could do something like

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL_Filter As String
  2.  
  3. If  Not IsNull(Me.cboProjectID) And Not IsNull(Me.cboRsrcCategory) Then
  4.      strSQL_Filter = " WHERE tbl.ProjectID = " & Me.cboProjectID & " AND tbl.ResourceCategory = '" & Me.cboRsrcCategory & "'"
  5. Else
  6.      strSQL_Filter = " WHERE tbl.ProjectID = " & Me.cboProjectID
  7. End If
  8.  
  9. [ReportName].Recordsource = "SELECT tbl.[Field1], tbl.[Field2], ..." &_
  10.                             "FROM tbl" & strSQL_Filter
  11.  

I've assumed that ProjectID is a number and ResourceCategory is text, so you might need to modify the filter with or without ' ' appropriately depending on the field types. I've also written the SQL statement very generically with table name "tbl", field names "Field1", "Field2", etc., so modify it to include whatever your table and field names are, what you want in your report, etc.

I would also set the "Limit To List" property on the combo boxes to Yes; this will prevent the user from typing whatever they want into the boxes, and therefore help you avoid SQL injection. Otherwise the query needs to be modified with parameters to avoid SQL injection attacks.

Pat
Apr 19 '10 #3
AccessBeetle
111 100+
Thanks for the insight.
This means that there is no way we can trick "one" query to serve AND/OR purpose. We have to implement it in VBA.
Thanks for help. !!
Apr 19 '10 #4
patjones
931 Expert 512MB
Well, there is a way that you could write it out in a single statement...

Expand|Select|Wrap|Line Numbers
  1. [ReportName].Recordsource = "SELECT tbl.[Field1], tbl.[Field2], ..." &_
  2.                             "FROM tbl " & _
  3.                             "WHERE tbl.ProjectID = " & Me.cboProjectID & IIf(IsNull(Me.cboRsrcCategory),""," AND tbl.ResourceCategory = '" & Me.cboRsrcCategory & "'")

Here the IIf statement picks out either a zero-length string, or the AND part of the query depending on whether or not something was selected in the second combo box. Personally though, I think the way I laid it out before makes the process clearer, even if it does require a few more lines of code.

Pat
Apr 19 '10 #5
AccessBeetle
111 100+
I agree. Good to know that we can use "iif" like this in query.
Thanks.
Apr 19 '10 #6
NeoPa
32,556 Expert Mod 16PB
Assuming ResourceCategory is a string value, then this could equally be done as :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM   tbl
  3. WHERE  [ProjectID]=Forms!FormName![cboProjectID]
  4.   AND  Nz(Forms!FormName![cboRsrcCategory],'') In([ResourceCategory],'')
This would avoid the necessity of resaving the QueryDef or even supplying a different RecordSource to the Report. This is one area where SQL in VBA string variables is not a good fit. The SQL can be passed into the Report code itself instead, but the more standard way (if indeed the SQL in string variable root is preferred) is to pass it in the call as the WhereCriteria parameter.
Apr 20 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: maffonso | last post by:
Hi guys, I have built a cross reference query (columns is year). I would like to change the caption and adjust others things. The best way would be to wrap the query in a form, but soon 2007 will...
16
by: Sam Durai | last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes even 0 rows) to a big table (of rows around 4 billion). I used the PK of the big table as merge key but merge does a table scan...
3
by: mnjkahn via AccessMonster.com | last post by:
I'm running Access 2003, modifying a query that has over 45 fields. When I right click on the field name in Query Design View, and then click Build, Access crashes before the Build window...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
3
by: Richard Hollenbeck | last post by:
I hope this isn't too confusing. The following query runs pretty fast by itself, but when I want to use it in a report (pasted below the query), it takes at least fifteen seconds to run! Then I...
7
by: bruce.dodds | last post by:
Access seems to be handling a date string conversion inconsistently in an append query. The query converts a YYYYMM string into a date, using the following function: CDate(Right(,2) & "/1/" &...
12
by: Kevin Blount | last post by:
I'm having a very odd issue, that arose this morning after working fine yesterday... here's a very simple script: 1: <?php 2: $test = $_GET; 3: echo "Hello" . $test . "<p>"; 4: ?>
3
by: pmacdiddie | last post by:
I have this query that when run on Access 2007 works, when run in 2002 it fails with the following error: Incorrect Syntax near '.'. INSERT INTO tblQualitySurvey ( Company, Job_Num,...
3
by: Vaudousi | last post by:
Hi All Asking help again. I am a casual user of Access. I have a simple application which works correctly under Access 2003. I tried it under 2007. One of the module refuses to work. I have : ...
15
by: OzNet | last post by:
I have a query (with calculated fields) in Access (2007) and the data changes depending on the dates and staff person selected. I need to produce a series of graphs based on the data in this query...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.