Append Records From Passthrough Query to Local Tab

2019-04-12 22:59发布

问题:

I have an Access Database and I'm using a pass through query to return records from an AS400 table. The connection string and pass through query work fine, but now I'm trying to populate the results of the p-t query into a local table within the db and my code is timing out. This is my first attempt at ADO so I'm disclaiming my code with "I'm not 100% sure what I'm doing!". Could you look at this and see if there is something obvious that I'm doing wrong? Any direction would be appreciated. Thank you in advance.

    Sub mod_ADODBConnect()
      Const NewTableName = "MyNewTable"
      Dim cn As ADODB.Connection
      Dim rs As ADODB.Recordset
      Dim db As DAO.Database
      Dim sSQL1 As String
      Dim sSQL2 As String

      sSQL1 = "SELECT ITMNUM, ITMDS, ITPKDS, MJCMCD, SBCMCD, STATUS, PRITIN, OGEXDT         from PDBLLIB007.BLPMST07"
      sSQL2 = "INSERT INTO ' & NewTableName & ' SELECT [" & sSQL1 & "].* from [" & sSQL1 & "]"

      Set cn = New ADODB.Connection
      cn.Open "Driver={Client Access ODBC Driver (32-bit)};" & _
        "System=DC007; Uid=XXXXX; Pwd=XXXXXX; MgDSN=0; ConnType=2;" & _
        "BlockSize=512; MaxFieldLen=2048; LazyClose=1; Prefetch=1; QueryTimeOut=0;     Translate=1"

     Set rs = New ADODB.Recordset
     rs.Open sSQL1, cn, adOpenDynamic, adLockOptimistic

     Do While Not rs.EOF
        rs.MoveNext
     Loop

   Set db = CurrentDb
   db.Execute ("sSQL2")

   rs.Close
   cn.Close

   Set rs = Nothing
   Set cn = Nothing
   Set db = Nothing
   End Sub

回答1:

You have a pass-through query which works fine and returns the rows you want. Now you want to store those rows in a new local (Jet/ACE) table. Seems to me a simpler approach would be to use the pass-through as the data source in a new "make table" query.

SELECT * INTO MyNewTable FROM YourPassThruQuery;

Oops, looks like you meant to append those rows to an existing table.

INSERT INTO MyNewTable
SELECT * FROM YourPassThruQuery;

If the table structures don't match, you can use field lists for both tables.

INSERT INTO MyNewTable (fld1, fld2)
SELECT first_field, second_field FROM YourPassThruQuery;