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
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 - Dim strSQL_Filter As String
-
-
If Not IsNull(Me.cboProjectID) And Not IsNull(Me.cboRsrcCategory) Then
-
strSQL_Filter = " WHERE tbl.ProjectID = " & Me.cboProjectID & " AND tbl.ResourceCategory = '" & Me.cboRsrcCategory & "'"
-
Else
-
strSQL_Filter = " WHERE tbl.ProjectID = " & Me.cboProjectID
-
End If
-
-
[ReportName].Recordsource = "SELECT tbl.[Field1], tbl.[Field2], ..." &_
-
"FROM tbl" & strSQL_Filter
-
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
In the onclick event of your button do a contitional statement.... - 'first type out the two possible query string variables
-
-
Dim Qstring1, Qstring2 as String
-
-
Qstring1 = "SELECT [MyTable]].[ProjectId], [MyTable].[ResourceCategory] FROM [MyTable];"
-
-
Qstring2 = "SELECT [MyTable]].[ProjectId] FROM [MyTable];"
-
-
'then do your condition and chose your data source for your report
-
-
if ResourceCategory.value = "" then
-
Report1.RecordSource = Qstring2
-
Else
-
Report1.RecordSource = Qstring1
-
End If
-
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 - Dim strSQL_Filter As String
-
-
If Not IsNull(Me.cboProjectID) And Not IsNull(Me.cboRsrcCategory) Then
-
strSQL_Filter = " WHERE tbl.ProjectID = " & Me.cboProjectID & " AND tbl.ResourceCategory = '" & Me.cboRsrcCategory & "'"
-
Else
-
strSQL_Filter = " WHERE tbl.ProjectID = " & Me.cboProjectID
-
End If
-
-
[ReportName].Recordsource = "SELECT tbl.[Field1], tbl.[Field2], ..." &_
-
"FROM tbl" & strSQL_Filter
-
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
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. !!
Well, there is a way that you could write it out in a single statement... - [ReportName].Recordsource = "SELECT tbl.[Field1], tbl.[Field2], ..." &_
-
"FROM tbl " & _
-
"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
I agree. Good to know that we can use "iif" like this in query.
Thanks.
NeoPa 32,556
Expert Mod 16PB
Assuming ResourceCategory is a string value, then this could equally be done as : - SELECT *
-
FROM tbl
-
WHERE [ProjectID]=Forms!FormName![cboProjectID]
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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/" &...
|
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: ?>
|
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,...
|
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 :
...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |