|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Query based on multiple table searches?
I have a MS Access database with two tables that Im trying to use with each other. The first table is tblContact(fields are: UserID, name, address, email) and the second is tblCategory(fields are: UserID, State, County). What Im trying to do is, based on what was filled out on a form I have(state and county) it will go through tblCategory and search for the state and county and when it finds them it will take the UserIDs then match that UserID or UserIDs(if more than one exist) to the UserID(s) in tblContact and then whatever UserIDs match it will take the email field from tblContact and email the information I want sent to those emails. I already know how to send the email and all that, and I even had the whole thing working with just one table(I just added an email field and threw some email addys in it.). I was just using this:
strSQL = "SELECT DISTINCT email FROM tblCategory WHERE State='" & state &"' AND County='" & county &"'" But how do I do this with two tables? And I dont know a lot about SQL but I know that the ONLY field that these two tables have the same name is UserID, so is it possible to search through tblCategory by state and county and then get the UserIDs then match those to the UserIDs in tblContact and then pull the emails from tblContact that match the UserIDs???? I hope this makes sense.. Thanks for any help. I really appreciate it. |
|
#2
|
||||
|
||||
|
Code:
SELECT A.Email FROM tblContact As A INNER JOIN tblCategory As B On (A.UserID = B.UserID) WHERE B.Country = '" & Country & "' AND B.State = '" & State & "' |
|
#3
|
|||
|
|||
|
Thats for an MSSQL database? Any idea on how to do this with an MSAccess database? Or would this sql statement work for ms access too?
Thanks |
|
#4
|
||||
|
||||
|
try it and see if it works.
|
|
#5
|
|||
|
|||
|
Doesnt work. Says:
Microsoft OLE DB Provider for ODBC Drivers error '80004005' [Microsoft][ODBC Microsoft Access Driver] Type mismatch in expression. ...... Ive gotten this same error using different sql statements as well. Hmm. Thanks for the help though. Maybe one day I will figure it out, lol, or just quit so I dont have to worry about it anymore. Thanks again.. |
|
#6
|
|||
|
|||
|
Well I figured it out for what I needed to do... Here is the sql query:
strSQL = "SELECT DISTINCT info.email, ContractorCat.UserID FROM info, ContractorCat WHERE ContractorCat.County = '" & county & "' AND ContractorCat.State = '" & state & "' AND ContractorCat.Category = '" & category & "'" This seems to be working for me. Thanks for all the help. |
|
#7
|
|||
|
|||
|
You really should join the tables instead of using Distinct. Bad habit to start
S- |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > Query based on multiple table searches? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|