Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Filter in Access- SQL Expand / Collapse
Author
Message
Posted Wednesday, January 9, 2013 1:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, January 11, 2013 11:52 AM
Points: 28, Visits: 50
This is my sql query in access. However, I am not sure where I need to add or filter on District. District needs to equal 1.

open attachment

SELECT [Austin Police Department].Name, [Austin Police Department].Location, [Austin Police Department].[Phone Number], [Austin Police Department].District, [Austin Police Department].[Position Title]
FROM [Austin Police Department]
WHERE ((([Austin Police Department].Name) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*")) OR ((([Austin Police Department].Location) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*")) OR ((([Austin Police Department].[Phone Number]) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*")) OR ((([Austin Police Department].District) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*")) OR ((([Austin Police Department].[Position Title]) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*"))
ORDER BY [Austin Police Department].District;


  Post Attachments 
2.JPG (7 views, 116.58 KB)
Post #1404963
Posted Wednesday, January 9, 2013 1:56 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 21, 2014 4:34 PM
Points: 48, Visits: 9,470
Hi

Because you have multiple criteria specified as OR criteria, you need to select District = 1 on each of those criteria.

There WHERE clause will then look something like the following:

WHERE
((([Austin Police Department].Name) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*") AND [Austin Police Department].District = 1)
OR
((([Austin Police Department].Location) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*") AND [Austin Police Department].District = 1)
OR
((([Austin Police Department].[Phone Number]) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*") AND [Austin Police Department].District = 1)
OR
((([Austin Police Department].District) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*"))
OR
((([Austin Police Department].[Position Title]) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*") AND [Austin Police Department].District = 1)

I hope this is clear enough.

Cheers
Post #1404998
Posted Wednesday, January 9, 2013 2:18 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, January 11, 2013 11:52 AM
Points: 28, Visits: 50
BUT I dont want district 2,3,4 to show up in list box before I search. When I search it only shows district 1.
Post #1405008
Posted Thursday, January 10, 2013 12:29 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 21, 2014 4:34 PM
Points: 48, Visits: 9,470
I'm sorry, I seem to have misunderstood your initial post so perhaps it would be best if you gave me some background to your enquiry.

Firstly, what is it that you are building and what is the purpose of this query?

Secondly, what sort of control is [Forms]![Austin Police Department- District 1]![txtSearch2]?

Cheers
Post #1405234
Posted Thursday, January 10, 2013 8:17 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, January 11, 2013 11:52 AM
Points: 28, Visits: 50
The list box is retrieving the data from the query. Right now the list box shows every district(1,2,3,4). I want the list box to show district 1 only. In other words, I want to be able to filter on district 1 when I search.

Look at my attachments.


  Post Attachments 
3.JPG (7 views, 161.31 KB)
Post #1405486
Posted Friday, January 11, 2013 1:36 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 22, 2014 2:17 PM
Points: 100, Visits: 794
Hi, what happens if you use
WHERE [Austin Police Department].District = 1 AND ((([Austin Police Department].Name) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*")) OR ((([Austin Police Department].Location) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*")) OR ((([Austin Police Department].[Phone Number]) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*")) OR ((([Austin Police Department].[Position Title]) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*"))
?
Post #1405814
Posted Friday, January 11, 2013 2:41 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 21, 2014 4:34 PM
Points: 48, Visits: 9,470
Hi

On the assumption that the query you have shown in your initial post is the rowsource for your list box, if you want only District 1 records to be displayed, you have to include this value (ie 1) in the District column for each Criteria row specified in the Query Designer. For instance, the Criteria row with the selection in the Name column should also have a 1 specified in the District column.

The SQL generated by this is probably going to look something like the WHERE I have already posted (I should have explained it as per this post right from the get go), or Access may modify it to look like the alternative suggested by grovelli. Faced with this problem I would probably manually alter the SQL as per grovelli's method.

All the best.
Post #1405843
Posted Friday, January 11, 2013 7:43 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, January 11, 2013 11:52 AM
Points: 28, Visits: 50
I found the solution yesterday. thank you for your help. here is the query

SELECT [Austin Police Department].Name, [Austin Police Department].Location, [Austin Police Department].[Phone Number], [Austin Police Department].District, [Austin Police Department].[Position Title]
FROM [Austin Police Department]
WHERE ((([Austin Police Department].Name) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*") AND (([Austin Police Department].District)="1")) OR ((([Austin Police Department].Location) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*") AND (([Austin Police Department].District)="1")) OR ((([Austin Police Department].[Phone Number]) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*") AND (([Austin Police Department].District)="1")) OR ((([Austin Police Department].District) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*" And ([Austin Police Department].District)="1")) OR ((([Austin Police Department].District)="1") AND (([Austin Police Department].[Position Title]) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*"));
Post #1406034
Posted Friday, January 11, 2013 7:57 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, August 22, 2014 2:17 PM
Points: 100, Visits: 794
I doubt it's going to work because you cannot set District to 1 and something else at the same time
OR ((([Austin Police Department].District) Like "*" & [Forms]![Austin Police Department- District 1]![txtSearch2] & "*" And ([Austin Police Department].District)="1"))
Post #1406047
Posted Friday, January 11, 2013 8:22 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, January 11, 2013 11:52 AM
Points: 28, Visits: 50
well its working right now. there is no problems with the database.
Post #1406067
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse