Changing query criteria with VBA

andymartnez

Registered User.
Local time
Today, 18:42
Joined
Jul 2, 2013
Messages
18
Hey, I'm working on making a search form for my database and have had fairly decent success in getting it functional.

On my search form are a few textboxes, comboboxes and an option group that all work with a query to search the database. the option group controls a set of 3 textboxes that are meant to function as a "keyword" search. All the keyword search does is not much more than simply searching through a field in a table labeled "keywords" that have been stored by whoever entered the record as a way of finding the record using the keyword search.

Going back to the option group, the two controls it has are "and" and "or", which should, in theory modify the query's criteria for the keyword field by making the search boxes be connected via "and" statements or "or" statements respectively.

I wanted to know if there was a way to add VBA code to my "search" button (the control that fires the query) to quickly change the query's criteria depending on whether the "and" or the "or" is selected. additionally I want it to modify the criteria depending on how many of the 3 keyword search text boxes were used. (I currently have a problem where if "or" is the criteria then if one keyword box is left blank it simply selects all of the records).

My "or" as it pertains to my problem with search results(the entire block of code is on one line of the query criteria):
Code:
Like "*" & [Forms]![search_frm]![keyword_1_search] & "*" 
Or Like "*" & [Forms]![search_frm]![keyword_2_search] & "*" 
Or Like "*" & [Forms]![search_frm]![keyword_3_search] & "*"
 
Last edited:
Here is an example of a Search text box. On the After Update - it sets the SQL criteris in a Listbox to the key word.
It can search all the columns in a ListBox. So Jun would show the name June, a town Juno, and a month column June for example.
This way, if the user is searching for a string in any field in the list box - the list box shortens.
Code:
            StrSQL = "SELECT * from vWells_ListBox " & _
              "WHERE ((([well Name] & [state] & [status] & [API #] & [Permit_File_No] & [WLastKnown]) Like '*' & [Forms]![Home_2]![Search1] & '*')) ; " '
        Me.lst_id_wells.RowSource = StrSQL
Maybe this can get you pointed in the right direction.
 
you could try:

WHERE instr(0,[myField],[Forms]![search_frm]![keyword_1_search])+instr(0,[myField],[Forms]![search_frm]![keyword_2_search])+instr(0,[myField],[Forms]![search_frm]![keyword_3_search])>0

If the keyword ="" or null or is not found in myField, instr will return 0, otherwise it will return a value which is the position in myField where it starts - for your purposes it doesn't matter where, just that it matches.
 
you could try:

WHERE instr(0,[myField],[Forms]![search_frm]![keyword_1_search])+instr(0,[myField],[Forms]![search_frm]![keyword_2_search])+instr(0,[myField],[Forms]![search_frm]![keyword_3_search])>0

I should make this the query criteria? Will it still function as an "or" statement would?
 
Will it still function as an "or" statement would?

yes - best thing to do is try it!

instr(0,[myFieldinstr(0,[myField],[Forms]![search_frm]![keyword_1_search]) will return 0 if [Forms]![search_frm]![keyword_1_search] is not in [MyField] or a number if it is. so for all of them 0+0+0=0 (no matches) or 6+0+0=6 (there is a match) which is >0
 
Okay so I tried it and it doesn't seem to work properly. I'm getting the "criteria is too long to be evaluated" error.

Additionally, will this override the other criteria I have in my query? The way I currently have it (before I added your suggestion) it would allow a user to use as many or as few of the criteria as he/she wished. I just don't want to have this new statement override that and cut off any other criteria when it finds a match.

If my method seems too complicated, is there a way to have a criterion parse multiple words from a single input on a single textbox? (Similar to a google search).

EDIT: Would you like me to post the full SQL statement for the query? (The big ugly one you find in SQL View).
 
will need to see the full sql to be able to help
 
Code:
SELECT database_tbl.ID, database_tbl.model_name, database_tbl.date_created, database_tbl.thumbnail, database_tbl.texture_type, database_tbl.path, database_tbl.author, database_tbl.polygons, 
database_tbl.textures, database_tbl.licensing_rights, database_tbl.categories, database_tbl.subcategories, database_tbl.keywords, database_tbl.notes, database_tbl.associated_program, database_tbl.file_type, *
FROM database_tbl
WHERE (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((database_tbl.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*") 
AND ((database_tbl.polygons) Like Nz([Forms]![search_frm]![polygons_search_low],"*")) 
AND ((database_tbl.categories) Like "*" & [Forms]![search_frm]![category_search] & "*") 
AND ((database_tbl.subcategories) Like "*" & [Forms]![search_frm]![subcategory_search] & "*") 
AND ((database_tbl.associated_program) Like "*" & [Forms]![search_frm]![associated_program_search] & "*") 
AND ((database_tbl.keywords) Like "*" & [Forms]![search_frm]![keyword_1_search] & "*" Or (database_tbl.keywords) Like "*" & [Forms]![search_frm]![keyword_2_search] & "*" Or (database_tbl.keywords) Like "*" & [Forms]![search_frm]![keyword_3_search] & "*")
AND ((database_tbl.file_type) Like "*" & [Forms]![search_frm]![file_type_search] & "*")) 
OR (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((database_tbl.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*") 
AND ((database_tbl.polygons) Between [Forms]![search_frm]![polygons_search_low] And [Forms]![search_frm]![polygons_search_high]) 
AND ((database_tbl.categories) Like "*" & [Forms]![search_frm]![category_search] & "*") 
AND ((database_tbl.subcategories) Like "*" & [Forms]![search_frm]![subcategory_search] & "*") 
AND ((database_tbl.associated_program) Like "*" & [Forms]![search_frm]![associated_program_search] & "*") 
AND ((database_tbl.keywords) Like "*" & [Forms]![search_frm]![keyword_1_search] & "*" Or (database_tbl.keywords) Like "*" & [Forms]![search_frm]![keyword_2_search] & "*" Or (database_tbl.keywords) Like "*" & [Forms]![search_frm]![keyword_3_search] & "*")
AND ((database_tbl.file_type) Like "*" & [Forms]![search_frm]![file_type_search] & "*"));
 
sorry = can't see what I posted in your sql so I can't answer the question
so I tried it and it doesn't seem to work properly. I'm getting the "criteria is too long to be evaluated" error.
 
sorry = can't see what I posted in your sql so I can't answer the question

I put my original stuff back in sorry.

Code:
SELECT database_tbl.ID, database_tbl.model_name, 
database_tbl.date_created, database_tbl.thumbnail, 
database_tbl.texture_type, database_tbl.path, database_tbl.author, 
database_tbl.polygons, database_tbl.textures, 
database_tbl.licensing_rights, database_tbl.categories, 
database_tbl.subcategories, database_tbl.keywords, database_tbl.notes, 
database_tbl.associated_program, database_tbl.file_type, *
 
FROM database_tbl
 
WHERE (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((database_tbl.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*") 
AND ((database_tbl.polygons) Like Nz([Forms]![search_frm]![polygons_search_low],"*")) 
AND ((database_tbl.categories) Like "*" & [Forms]![search_frm]![category_search] & "*") 
AND ((database_tbl.subcategories) Like "*" & [Forms]![search_frm]![subcategory_search] & "*") 
AND ((database_tbl.associated_program) Like "*" & [Forms]![search_frm]![associated_program_search] & "*") 
AND ((database_tbl.file_type) Like "*" & [Forms]![search_frm]![file_type_search] & "*") 
AND ((InStr(0,[myField],[Forms]![search_frm]![keyword_1_search])+InStr(0,[myField],[Forms]![search_frm]![keyword_2_search])+InStr(0,[myField],[Forms]![search_frm]![keyword_3_search]))>"0")) OR (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((database_tbl.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*")
AND ((database_tbl.polygons) Between [Forms]![search_frm]![polygons_search_low] And [Forms]![search_frm]![polygons_search_high]) AND ((database_tbl.categories) Like "*" & [Forms]![search_frm]![category_search] & "*") 
AND ((database_tbl.subcategories) Like "*" & [Forms]![search_frm]![subcategory_search] & "*") 
AND ((database_tbl.associated_program) Like "*" & [Forms]![search_frm]![associated_program_search] & "*") 
AND ((database_tbl.file_type) Like "*" & [Forms]![search_frm]![file_type_search] & "*") 
AND ((InStr(0,[myField],[Forms]![search_frm]![keyword_1_search])+InStr(0,[myField],[Forms]![search_frm]![keyword_2_search])+InStr(0,[myField],[Forms]![search_frm]![keyword_3_search]))>"0"));
 
You hadn't provided any field names so I made some up - should have explained better.

You need to substitute myField with whatever field in the query is required
 
You hadn't provided any field names so I made some up - should have explained better.

You need to substitute myField with whatever field in the query is required

Oops. When I tested it in my query I used the field "keywords" which is my keyword field. Just forgot to put that back in when I pasted it here.
 
OK - so does that mean it is working or you still have a problem?

If still a problem, it is important you post the code unedited
 
Okay here's the whole untouched thing, all I'm doing is making it line up nicely:

Code:
SELECT database_tbl.ID, database_tbl.model_name, 
database_tbl.date_created, database_tbl.thumbnail, 
database_tbl.texture_type, database_tbl.path, database_tbl.author, 
database_tbl.polygons, database_tbl.textures, 
database_tbl.licensing_rights, database_tbl.categories, 
database_tbl.subcategories, database_tbl.keywords, database_tbl.notes, 
database_tbl.associated_program, database_tbl.file_type, *
 
FROM database_tbl
 
WHERE (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((database_tbl.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*") 
AND ((database_tbl.polygons) Like Nz([Forms]![search_frm]![polygons_search_low],"*")) 
AND ((database_tbl.categories) Like "*" & [Forms]![search_frm]![category_search] & "*") 
AND ((database_tbl.subcategories) Like "*" & [Forms]![search_frm]![subcategory_search] & "*") 
AND ((database_tbl.associated_program) Like "*" & [Forms]![search_frm]![associated_program_search] & "*") 
AND ((database_tbl.file_type) Like "*" & [Forms]![search_frm]![file_type_search] & "*") 
AND ((InStr(0,[keywords],[Forms]![search_frm]![keyword_1_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_2_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_3_search]))>0)) 
OR (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((database_tbl.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*") 
AND ((database_tbl.polygons) Between [Forms]![search_frm]![polygons_search_low] And [Forms]![search_frm]![polygons_search_high]) 
AND ((database_tbl.categories) Like "*" & [Forms]![search_frm]![category_search] & "*") 
AND ((database_tbl.subcategories) Like "*" & [Forms]![search_frm]![subcategory_search] & "*") 
AND ((database_tbl.associated_program) Like "*" & [Forms]![search_frm]![associated_program_search] & "*") 
AND ((database_tbl.file_type) Like "*" & [Forms]![search_frm]![file_type_search] & "*") 
AND ((InStr(0,[keywords],[Forms]![search_frm]![keyword_1_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_2_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_3_search]))>0));
 
Rambling about possibilities since I am unable to verify, so various things to try.

1. I see you have the instr code in twice -is this correct? I can see there is an OR but they seem to be a repeat of each other except for polygons. In which case your critera could be written as (I've remove unecessary brackets for readability, just have the ones in red):
Code:
WHERE database_tbl.model_name Like "*" & [Forms]![search_frm]![model_name_search] & "*" 
AND database_tbl.texture_type Like "*" & [Forms]![search_frm]![texture_type_search] & "*" 
[COLOR=red]AND (database_tbl.polygons Like Nz([Forms]![search_frm]![polygons_search_low],"*" 
OR database_tbl.polygons Between [Forms]![search_frm]![polygons_search_low] And [Forms]![search_frm]![polygons_search_high])[/COLOR]
AND database_tbl.categories Like "*" & [Forms]![search_frm]![category_search] & "*" 
AND database_tbl.subcategories Like "*" & [Forms]![search_frm]![subcategory_search] & "*" 
AND database_tbl.associated_program Like "*" & [Forms]![search_frm]![associated_program_search] & "*" 
AND database_tbl.file_type Like "*" & [Forms]![search_frm]![file_type_search] & "*")
AND InStr(0,[keywords],[Forms]![search_frm]![keyword_1_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_2_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_3_search])>0

2. Having selected the fields you want you have also selected all (*)
database_tbl.associated_program, database_tbl.file_type, *

FROM database_tbl

Also, it is possible that you are running out of characters for the query you could try:

SELECT *

FROM database_tbl

WHERE (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![
...

Alternatively - a suggestion is to alias the table as T which you do like this (this one most closely addresses your "criteria is too long to be evaluated" error):

Code:
SELECT T.ID, T.model_name, 
T.date_created, T.thumbnail, 
T.texture_type
etc...
 
FROM database_tbl [COLOR=red]AS T

[/COLOR]WHERE (((T.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((T.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*") 
etc...

Other things to consider are to drop the underlines - good practice is not to use spaces or underlines - and consider abbreviating your field names.

Not sure if any of these will do the job but worth a try
 
I have the code doubled up for a purpose, I'd change it but I don't want to have it mess up anything.

I'm still getting the "criteria is incorrect or too long error" and I shortened my SQL to this:

Code:
SELECT *
 
FROM database_tbl
 
WHERE (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((database_tbl.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*") 
AND ((database_tbl.polygons) Like Nz([Forms]![search_frm]![polygons_search_low],"*"))
AND ((database_tbl.categories) Like "*" & [Forms]![search_frm]![category_search] & "*") 
AND ((database_tbl.subcategories) Like "*" & [Forms]![search_frm]![subcategory_search] & "*") 
AND ((database_tbl.associated_program) Like "*" & [Forms]![search_frm]![associated_program_search] & "*") 
AND ((database_tbl.file_type) Like "*" & [Forms]![search_frm]![file_type_search] & "*") 
AND ((InStr(0,[keywords],[Forms]![search_frm]![keyword_1_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_2_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_3_search]))>0)) 
OR (((database_tbl.model_name) Like "*" & [Forms]![search_frm]![model_name_search] & "*") 
AND ((database_tbl.texture_type) Like "*" & [Forms]![search_frm]![texture_type_search] & "*") 
AND ((database_tbl.polygons) Between [Forms]![search_frm]![polygons_search_low] And [Forms]![search_frm]![polygons_search_high]) 
AND ((database_tbl.categories) Like "*" & [Forms]![search_frm]![category_search] & "*") 
AND ((database_tbl.subcategories) Like "*" & [Forms]![search_frm]![subcategory_search] & "*") 
AND ((database_tbl.associated_program) Like "*" & [Forms]![search_frm]![associated_program_search] & "*") 
AND ((database_tbl.file_type) Like "*" & [Forms]![search_frm]![file_type_search] & "*") 
AND ((InStr(0,[keywords],[Forms]![search_frm]![keyword_1_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_2_search])+InStr(0,[keywords],[Forms]![search_frm]![keyword_3_search]))>0));

Should I have my old keyword search still in the criteria for the keyword field? or is your code taking over for that?
 
Here's how it looks now:

Code:
SELECT T.ID, T.model_name, T.date_created, T.thumbnail, T.texture_type, T.path, T.author, T.polygons, T.textures, T.licensing_rights, T.categories, T.subcategories, T.keywords, T.notes, T.associated_program, T.file_type, *

FROM database_tbl AS T

WHERE (((T.model_name) Like "*" & Forms!search_frm!model_name_search & "*") 
And ((T.texture_type) Like "*" & Forms!search_frm!texture_type_search & "*") And ((T.polygons) Like Nz(Forms!search_frm!polygons_search_low,"*")) 
And ((T.categories) Like "*" & Forms!search_frm!category_search & "*") 
And ((T.subcategories) Like "*" & Forms!search_frm!subcategory_search & "*") 
And ((T.associated_program) Like "*" & Forms!search_frm!associated_program_search & "*") 
And ((T.file_type) Like "*" & Forms!search_frm!file_type_search & "*") 
And ((InStr(0,[keywords],Forms!search_frm!keyword_1_search)+InStr(0,[keywords],Forms!search_frm!keyword_2_search)+InStr(0,[keywords],Forms!search_frm!keyword_3_search))>0)) 
Or (((T.model_name) Like "*" & Forms!search_frm!model_name_search & "*") 
And ((T.texture_type) Like "*" & Forms!search_frm!texture_type_search & "*") 
And ((T.polygons) Between Forms!search_frm!polygons_search_low 
And Forms!search_frm!polygons_search_high) 
And ((T.categories) Like "*" & Forms!search_frm!category_search & "*") 
And ((T.subcategories) Like "*" & Forms!search_frm!subcategory_search & "*") 
And ((T.associated_program) Like "*" & Forms!search_frm!associated_program_search & "*") 
And ((T.file_type) Like "*" & Forms!search_frm!file_type_search & "*") And ((InStr(0,[keywords],Forms!search_frm!keyword_1_search)+InStr(0,[keywords],Forms!search_frm!keyword_2_search)+InStr(0,[keywords],Forms!search_frm!keyword_3_search))>0));

When running it as I have above, I get the same error as before.

When I remove the * from the select then it says the "object is closed or does not exist"
 
Well I'm now at a loss - the latest error implies a spelling error either of a field name or the name of a control on your form (which I presume is open!).

The earlier error would seem to be addressed by reducing the length of the code - although I appreciate this hasn't removed the error.

So I think you need to work back to the beginning to find which part of the code is causing a problem.

But first, one thing not yet done is to check for nulls so rewrite the instr as follows:
Code:
InStr(0,[keywords],nz(Forms!search_frm!keyword_1_search))+InStr(0,[keywords],nz(Forms!search_frm!keyword_2_search))+InStr(0,[keywords],nz(Forms!search_frm!keyword_3_search))
and see if this solves the problem.

If not then next, since it was presumably the last entered, remove the instr code to see if this solves the problem. If it doesn't, keep removing a criteria until you don't get a problem.
 
Yeah that didn't fix it so I took out the InStr code and reverted it to my old method and it works fine. I kept the aliasing, however.

Is there truly no way to do what I'm trying to do?
 

Users who are viewing this thread

Back
Top Bottom