Back To Top


MS Access VBA Insert Into Select Function For versions 2000, 2002, 2003, 2007

Go here To Get all Fields in a Table.
Go here To Check if Field exists in VBA.
Go here to see the MS Access VBA Append All Tables to a Single Table
Go here to see the MS Access Append Query in the Query Builder

 

The VBA Append Sample is available for download here in a zipped MS Access Database.

The Actual VBA Code to Append to a Table, you should be able to copy/paste right into a module:


Function
AppendTable(toTableName As String , frmTableName As String , _
      FieldName
As String , FieldName2 As String ) As Boolean

  
' Append To a Table toTableName from another table frmTableName .
   ' Accepts
   ' toTableName: Name of table to Append to
   ' frmTableName: Name of table to Append from
   ' FieldName: Name of the field to Append
   ' FieldName2: Name of the second field to Append

   ' Returns True on success, false otherwise
   'USAGE: AppendTable "toTableName", "frmTableName", "FieldName", "FieldName2"
  
On Error GoTo errhandler
  
Dim strSql As String , Db As dao.Database


  
'Create the Append Insert Into Select SQL Code from our Four String Values

  
strSql = "INSERT INTO " & toTableName & "(" & FieldName & ", " & FieldName2 & ")" & _
        
" SELECT " & "[" & frmTableName & "]." & FieldName & ",[" & frmTableName & "]." & FieldName2 & _
        
" FROM " & frmTableName & ";"


  
'Print the SQL so we can paste into the query build if there are errors
  
Debug.Print strSql
  
'Use Current Database
  
Set Db = CurrentDb()


  
'Run the SQL Query
  
Db.Execute strSql

  
'If no errors return true
  
AppendTable = True
ExitHere:

  
Set Db = Nothing

  
'Notify the user the process is complete.
  
MsgBox "Insert Into Select Complete"
  
Exit Function
errhandler:
  
'There is an error return false
  
AppendTable = False
   With
Err
      MsgBox
"Error " & .Number & vbCrLf & .Description, _
            vbOKOnly
Or vbCritical, "AppendTable"
  
End With
   Resume
ExitHere
End Function



For the above VBA Append Function in an usable Form with Append Button, download the Sample database in ZIP format. The sample includes 2 Tables with 100 records each in which to test on.

For further help or suggestions go here: MS Access tutorials, Tables, Queries and later: Forms

You may post this tutorial on your website or in a forum. If you do please maintain a Link to Eraseve AP.



For further MS Access tutorials go here: MS Access tutorials

Go here To Update a field in the Query Builder.
Go here To Check if Table exists in VBA.
Go here To Check if Field exists in VBA.
Follow the Set Autonumber Field with VBA Link to Set a Field to Autonumber.

To Change a field’s data type in VBA go here.

 

Did you find what you were looking for?
What would you suggest?
mail@eraserve.com
Your Name:
Your Message:
Your Email:

    

 

Custom Search