Filter in Access- SQL

  • 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;

  • 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

  • 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.

  • 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

  • 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.

  • 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] & "*"))

    ?

  • 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.

  • 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] & "*"));

  • 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"))

  • well its working right now. there is no problems with the database.

  • I got same problems, thank you for the information.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply