Home > Database > Mysql Tutorial > How Can I Efficiently Transfer Large Datasets from Excel to Access Using SQL?

How Can I Efficiently Transfer Large Datasets from Excel to Access Using SQL?

Mary-Kate Olsen
Release: 2025-01-05 07:22:44
Original
148 people have browsed it

How Can I Efficiently Transfer Large Datasets from Excel to Access Using SQL?

Using SQL Insert Statement to Efficiently Transfer Data from Excel to Access

In your Excel VBA code, you were manually looping through each row of data to insert values into an Access table. While this approach works for small datasets, it becomes inefficient for large datasets like 25,000 records.

Instead, you can utilize SQL INSERT statements to insert multiple rows at once, drastically reducing the processing time. The following revised code demonstrates how:

Public Sub DoTrans()
  Set cn = CreateObject("ADODB.Connection")
  dbPath = Application.ActiveWorkbook.Path & "\FDData.mdb"
  dbWb = Application.ActiveWorkbook.FullName
  dbWs = Application.ActiveSheet.Name
  scn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath
  dsh = "[" & Application.ActiveSheet.Name & "$]"
  cn.Open scn

  ssql = "INSERT INTO fdFolio ([fdName], [fdOne], [fdTwo]) "
  ssql = ssql & "SELECT * FROM [Excel 8.0;HDR=YES;DATABASE=" & dbWb & "]." & dsh

  cn.Execute ssql

End Sub
Copy after login

By using the SQL INSERT statement shown in line 11, all rows from the Excel range are inserted into the Access table at once. This eliminates the need for the loop and significantly improves performance.

Although you mentioned wanting to specify field names instead of using "SELECT *", the code provided does not demonstrate that. If you require assistance with setting specific field names, please provide the desired field names, and we can help you modify the code accordingly.

The above is the detailed content of How Can I Efficiently Transfer Large Datasets from Excel to Access Using SQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template