views:

63

answers:

2

Hey all,

I tried using Access for queries and it returns extremely unexpected results (where it would return records from contacts table that didn't even appear in the temp_query:

 SELECT contacts.names_1, contacts.names_2, contacts.addresses, contacts.cities, contacts.us_states_and_canada, contacts.zip_codes
 FROM contacts, temp_query
 WHERE 
   (((temp_query.first_name) Like "*" & [contacts].[names_1] & "*") 
       AND ((temp_query.last_name) Like "*" & [contacts].[names_1] & "*")) 
  OR 
   (((temp_query.first_name) Like "*" & [contacts].[names_2] & "*") 
       AND ((temp_query.last_name) Like "*" & [contacts].[names_2] & "*"));

I thought the above would make sense since the strings in the first_name and last_name of temp_query can be in name_1 of contacts and the the first_name and last_name of temp_query can be in name_2 of contacts . But I don't think first_name can be in name_1 and last_name in name_2 and visa versa.

Someone recommended this, but this ultimately only returned a single record where it should have at least returned 70 (not to mention it took 35 minutes for the query to bring the result):

Criteria: (in first_name column): Like "*" & [contacts].[name1] & "*"  (in last_name column) like "*" & [[contacts].[name2] & "*"
Or:       (in first_name column): Like "*" & [contacts].[name2] & "*"  (in last_name column) Like "*" & [[contacts].[name1]  & "*"

So I'm not sure what shoud be the right criteria for the intended effect.

Thanks for any response.

+2  A: 

Do you know SQL? It would probably be better to open up the query in SQL view and just edit it there.

Then, when you go back to design view, the fields in the design view will reflect what you put in SQL view automatically to see what it was that you were missing.

Joseph
I know some mysql, but I looked at the resulting output and I can't figure why it's still not matching criteria properly.
JohnMerlino
Remou
Why would you say not at all suitable? I'm looking to check if first_name and last_name column of temp_query exists somewhere in the string of name_1 field of contacts or if the first_name and last_name fields of temp query exists somehwere in the string of name_2. If either is true, then return those records.
JohnMerlino
@Remou you're right, I read it too fast. I was thinking it might be doing a logical AND but I didn't think about concatenation.
Joseph
@JohnMerlino I think you may have misread my comment.
Remou
@Joseph, Since Access SQL doesn't save comments, line breaks, and indents, I've always preferred the Designer mode over direct SQL coding. Of course, it has it's limitations, but that's a tradeoff that works for me.
PowerUser
@PowerUser I understand, I was suggesting it as more of a fix for his issue, not to suggest that using SQL view is the way he should go all the time. Use whatever you're comfortable with, but know all your tools.
Joseph
+1  A: 

I think this is what you are looking for. You can just paste this into your SQL View

SELECT contacts.names_1, contacts.names_2, contacts.addresses, contacts.cities, contacts.us_states_and_canada, contacts.zip_codes
FROM contacts, temp_query
WHERE 
(
contacts.names_1  Like "*" & temp_query.first_name    & "*" 
and 
contacts.names_1  Like "*" & temp_query.last_name    & "*" 

)
OR
(
contacts.names_2  Like "*" & temp_query.first_name    & "*" 
and 
contacts.names_2  Like "*" & temp_query.last_name    & "*" 
)
;

Your original where clause was looking for when contacts.name_1 was in first_name and in last_name or if contacts.name_2 was in first_name and in the last_name.

Conrad Frix
That seemed to do it.
JohnMerlino
The key point here is that you had your search reversed -- you were looking for the combined name in each of the name fields, instead of searching for the firstname/lastname fields in the combined field. I post this clarifying comment because it took me a while to figure it out!
David-W-Fenton