How to Add Recordset Records to Another Table using VBA
Question details
The user needs to copy data records from an open VBA recordset into a separate destination database table.

- Product
- Spreadsheets / Database VBA
- Device & OS
- not provided
- Scenario
- Transferring or appending data between tables using VBA scripts.
- Observed behavior
- Records must be mapped explicitly, appropriately formatted for SQL syntax, and inserted into the new table successfully.
Ensure you have an active database connection and that the destination table has the matching field structure to accept the incoming recordset data. Back up your target database before running bulk insert operations.
Copy Records Using a Loop and INSERT Statements
Best for processing records one at a time when data manipulation, field mapping, or validation is needed before insertion.
By iterating through the source recordset, you can dynamically construct an SQL INSERT statement for every row. This provides granular control over how text strings are escaped and how Null values are passed to the new table.
Establish a connection to your source database and open the ADO or DAO recordset containing the data you want to copy.
Create a 'Do While Not rs.EOF' loop in your VBA script to process each row individually.
Build an 'INSERT INTO DestinationTable' SQL string. Make sure to map fields explicitly, wrapping text values in single quotes and handling numeric or date values appropriately.
Use 'CurrentDb.Execute sqlString' (DAO) or 'Connection.Execute sqlString' (ADO) to run the query, then use 'rs.MoveNext' to proceed to the next record before looping.

Use an INSERT INTO ... SELECT Bulk Query
The most efficient method for transferring large datasets without processing records individually.
Manage VBA Macros and Data Transfers in WPS Spreadsheets
WPS Office provides robust support for VBA macros, allowing you to establish database connections and execute SQL queries to transfer data seamlessly.
- 1. Enable Developer Tools: Open WPS Spreadsheets, navigate to the Options, and ensure the Developer tab is enabled to access the VBA environment.
- 2. Open the VBA Editor: Press ALT + F11 or click 'VBA Editor' on the Developer tab to open the macro scripting environment.
- 3. Add Database References: Go to Tools > References and enable 'Microsoft ActiveX Data Objects Library' to properly handle Recordsets.
- 4. Run Your Macro: Paste your SQL and Recordset VBA script into a module and press F5 to execute the data transfer.

Frequently Asked Questions
Why do I get a syntax error when inserting text fields using VBA?
Syntax errors often occur because text values in SQL INSERT statements are not properly wrapped in single quotes, or because the text itself contains an apostrophe (like 'O'Connor'). Ensure you use explicit string formatting and escape characters.
How do I handle Null values from my source recordset?
When building your SQL string, you must check if a field is null using the 'IsNull()' function. If it is, explicitly write the word NULL in your SQL string without quotes instead of passing an empty string or causing a type mismatch.
Is looping through a recordset slower than an append query?
Yes, processing records one at a time via a loop creates significantly more overhead and database traffic. For large datasets without row-specific calculation logic, always use an 'INSERT INTO ... SELECT' bulk query.




