View Full Version : Run Query Based on Value From Another Table


crhodus
09-10-2001, 08:01 AM
I'm trying to create a query that will dump data from table COMPANY_T, into another table NAMED tblCompany1, if any of the companies from COMPANY_T have their company number recorded in table tblCompNum1.

Can someone tell me what I'm doing wrong? Thanks!

SELECT [Company_T].[COMPANY_NUMBER], [Company_T].[INDIVIDUAL], [Company_T].[LAST_UPDATED] INTO tblCompany1
FROM Company_T
WHERE ((([Company_T].[COMPANY_NUMBER]) In ([tblCompNum1].[COMPANY_NUMBER])));

Dave Sherer
09-10-2001, 08:34 PM
Your are Using the SELECT INTO and that creates a new table. You probaly want to use the UPDATE statement. This way you can us your criteria on the table because it won't erase any records

HTH

crhodus
09-11-2001, 05:03 AM
Actually, I want it to replace these records every time it runs. The records in the table are only temporary records.

Regardless, thanks for your comments!

[This message has been edited by crhodus (edited 09-11-2001).]

Pat Hartman
09-11-2001, 11:53 AM
To create the update query add the target table to the QBE form and then add the source table. Select all the columns from the target table. Change the query type to update. Then type the matching column name from the source table as the "Update To" cell. Use the format
[sourcetablename].[sourcecolumnname]

surround the table name and column name with square brackets and use the period as the separator.

crhodus
09-12-2001, 06:14 AM
Thanks. I tried doing what you recomended, but I ended up adding the following line of code:

WHERE COMPANY_NUMBER IN
(SELECT [tblCompNum1].[COMPANY_NUMBER] FROM tblCompNum1);

This seems to have worked. Thanks again!

Pat Hartman
09-12-2001, 12:00 PM
All you needed to do was to join the two tables on COMPANY_NUMBER. Sorry I didn't mention that specifically.