Question : "Insert" query in VBA not working  with SQL Server

I'm using an "Insert" query to copy data from my main table to an archive table.  This code was working with an Access backend but doesn't work with SQL Server.  Below is the code.  The error is:
  Error Number 3146
  ODBC--call failed

strQuery decodes to:
INSERT INTO [Archive_Issued_Traveler_Steps] SELECT * FROM [Issued_Traveler_Steps] WHERE [Issued_Traveler_ID] =23972

What's wrong with this query?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
Dim rst1 As Recordset
  Dim tdfTable As TableDef
  Dim strQuery As String
...
strQuery = "INSERT INTO [" & strArchiveTable & "]" & _
           " SELECT * FROM [" & strProductionTable & "]" & _
            " WHERE " & "[" & tdfTable.Fields(1).Name & "] =" & lngID  'field(1) is always the Foreign Key field in this db
    On Error Resume Next
    CurrentDb.Execute strQuery, dbFailOnError + dbSeeChanges     '<-ODBC connection error from this statement

Answer : "Insert" query in VBA not working  with SQL Server

Error 3146 is somewhat generic ... are you using linked tables? If so, try using the ADO connection:

Currentproject.connection.execute strQuery

If that doesn't work, then you may need to examine the Errors collection further to find out what's wrong. The ADO Errors collection provides more details about what's going on with your connection and such.

Dim con As ADODB.Connection

On Error GoTo Err_Handler

Set con = New ADODB.Connection
Set con = CurrentProject.Connection


con.Execute strQuery

Exit Sub (or Exit Function)

Err_Handler:
  If con.Errors.Count > 0 Then
    For i = 0 to con.Errors.Count - 1
      Msgbox con.Errors(i). Description & vbCrLf & con.Errors(i).Source & vbCrLf & con.Errors(i).NativeError
    Next
  End If
Random Solutions  
 
programming4us programming4us