[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL INSERT INTO with multiple tables (VB6/ Jet)

Posted on 2007-10-14
5
Medium Priority
?
1,063 Views
Last Modified: 2013-12-25
Dear Gurus
I have the following working SQL code (per EE response yesterday), working in a VB6/ Jet environment:
        SQLStr1 _
            = " INSERT INTO " & tbl & " IN '" & txt_Output_Db & "'" _
            & " SELECT * FROM " & tbl & " IN '" & txt_db1 & "'" _
            & " WHERE POSTCODE = '" & pc & "'"
        DbsConnection1.Execute SQLStr1

However, I now need to change the postcode criteria from a single postcode to a list of postcodes, that have been saved to a separate Access table.  My attempt at the following change:
        SQLStr1 _
            = " INSERT INTO " & tbl & " IN '" & txt_Output_Db & "'" _
            & " SELECT * FROM " & tbl & ", tblPostcode IN '" & txt_db1 & "'" _
            WHERE " & tbl & ".POSTCODE = tblPostcode.Pcode"

returned the error "Duplicate output destination ID".  I thought that I could get around that problem by changing the name of the ID field in the postcode table from 'ID" to 'ID1" - but that returned a separate error.

Your help with modifying the SQL code so that it works with a list of postcodes is most appreciated.
0
Comment
Question by:BrianBeck
  • 3
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20076061
what are you trying to achieve with this line?
            WHERE " & tbl & ".POSTCODE = tblPostcode.Pcode"

0
 

Author Comment

by:BrianBeck
ID: 20076076
Hi angelIII
Thanks, tblPostcode has for instance "4004", "4005" and "4006" as contents within its Pcode field.
What I want therefore is that the INSERT INTO exports to the output table (txt_Output_Db) any source table (txt_db1) records whose POSTCODE field contents match any of the three sample postcodes.
0
 

Author Comment

by:BrianBeck
ID: 20076165
Hi angelIII
Just to add to the above, if there are a total of 1,000 records in the source database table, and of those, 50 of them had a postcode that was either of 4004, 4005 or 4006, then I want the output database to contain the 50 respective source table records.
Much appreciated!
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20076172
would this work any better:

        SQLStr1 _
            = " INSERT INTO " & tbl & " IN '" & txt_Output_Db & "'" _
            & " SELECT t.* FROM " & tbl & " t , tblPostcode IN '" & txt_db1 & "' p " _
            WHERE t.POSTCODE = p.Pcode"
0
 

Author Comment

by:BrianBeck
ID: 20076279
Hi angelIII
Brilliant, thank you (I did remove the last 'p' in the second line).
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question