VBA & Macro Problems

How to Add Recordset Records to Another Table using VBA

Anum MushtaqAnum Mushtaq Nov 3, 2026 871 views

Question details

The user needs to copy data records from an open VBA recordset into a separate destination database table.

How to Add Recordset Records to Another Table with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the Source Recordset

Establish a connection to your source database and open the ADO or DAO recordset containing the data you want to copy.

2
Loop Through the Recordset

Create a 'Do While Not rs.EOF' loop in your VBA script to process each row individually.

3
Construct the SQL INSERT Statement

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.

4
Execute and Move to Next

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.

Copy Records Using a Loop and INSERT Statements
Handling Special Values: Remember to escape text values containing apostrophes by doubling the single quotes (e.g., Replace(myString, "'", "''")) and explicitly write NULL in your SQL string if a source field IsNull().

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. 1. Enable Developer Tools: Open WPS Spreadsheets, navigate to the Options, and ensure the Developer tab is enabled to access the VBA environment.
  2. 2. Open the VBA Editor: Press ALT + F11 or click 'VBA Editor' on the Developer tab to open the macro scripting environment.
  3. 3. Add Database References: Go to Tools > References and enable 'Microsoft ActiveX Data Objects Library' to properly handle Recordsets.
  4. 4. Run Your Macro: Paste your SQL and Recordset VBA script into a module and press F5 to execute the data transfer.
Full compatibility with Microsoft Excel VBA macros and scriptsEasily write and execute ADO database connectionsLightweight application with high performance for handling large datasetsFree to use with a familiar spreadsheet interface
QA img-9

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.