当前位置:网站首页>VBA batch import Excel data into Access database
VBA batch import Excel data into Access database
2022-07-30 19:02:00 【OOQ】
- Requirement goal: import the data existing in the Excel table into the created Access database
Implementation method
Method 1: Scan the worksheet row by row, when storing data in the array, get the value from the cell [rs.Fields(j - 1) = Cells(i, j).Value]
Sub addDate1()Dim i As Integer, j As Integer, n As IntegerDim sql As StringDim con As New ADODB.ConnectionWith con.Provider = "microsoft.ace.oledb.12.0".ConnectionString = ThisWorkbook.Path & "\test.accdb".OpenEnd WithSet rs = con.OpenSchema(adSchemaTables)n = Range("A1").End(xlDown).RowFor i = 1 To nSet rs = New ADODB.Recordsetsql = "select * from m_check"rs.Open sql, con, adOpenKeyset, adLockOptimisticrs.AddNewFor j = 1 To rs.Fields.Countrs.Fields(j - 1) = Cells(i, j).ValueNext jrs.UpdateNext iMsgBox "success" + Str(n)rs.Closecon.Close 'Close the connectionSet con = Nothing 'Release the variableSet rs = NothingEnd SubMethod 2: Read the data in the worksheet into the array, when storing data in the database, get the value from the array [rs.Fields(j - 1) = arr(i, j)]
Sub addDate()Dim arr, i As Integer, j As IntegerDim sql As Stringarr = Range("A2").CurrentRegionDim con As New ADODB.ConnectionDim rs As New ADODB.RecordsetWith con.Provider = "microsoft.ace.oledb.12.0".ConnectionString = ThisWorkbook.Path & "\test.accdb".OpenEnd Withsql = "select * from m_check"rs.Open sql, con, adOpenKeyset, adLockOptimisticFor i = 2 To UBound(arr)rs.AddNewFor j = 1 To rs.Fields.Countrs.Fields(j - 1) = arr(i, j)Next jrs.UpdateNext iMsgBox "success"' sql = "delete * from m_check"' con.Execute(sql)rs.Closecon.Close 'Close the connectionSet con = Nothing 'Release the variableSet rs = NothingEnd Sub
Comparison of different data volumes
Sub GetRunTime()Dim i As LongDim dteStart As DateDim strTime As String'Turn off screen refresh'Application.ScreenUpdating = FalsedteStart = Timer'--------- Main body of running process-------addDate1strTime = Format((Timer - dteStart), "0.00000")MsgBox "Running process: " & strTime & "seconds"'Turn on screen refresh'Application.ScreenUpdating = TrueEnd SubProcessing 11 pieces of data took 0.05469s
Processing 28339 pieces of data, the array method is fast
Processing 1048576 pieces of data, obviously the array will overflow.
Summary
Generally speaking, when the amount of data processed is small, no matter which method is adopted, the difference is not significant.But when the amount of data reaches tens of thousands, the array processing is obviously faster.When the amount of data reaches one million, the above two methods will cause "overflow". At this time, we should consider data segmentation, divide the million data into several parts, and process them in batches.
边栏推荐
- Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents
- 设计消息队列存储消息数据的 MySQL 表格
- Delay queue optimization (2)
- OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
- nlohmann json 使用指南【visual studio 2022】
- MongoDB打破了原则引入SQL?
- ROS 节点初始化步骤、topic/service创建及使用
- vxe-table实现复选框鼠标拖动选中
- 浅聊对比学习(Contrastive Learning)第一弹
- 第十七届“振兴杯”全国青年 职业技能大赛——计算机程序设计员(云计算平台与运维)参赛回顾与总结
猜你喜欢
随机推荐
【刷题篇】计算质数
What is the value of biomedical papers? How to translate the papers into Chinese and English?
Read the "Language Model" in one article
【Prometheus】Prometheus联邦的一次优化记录[续]
LocalDate时间生成
《痞子衡嵌入式半月刊》 第 59 期
浅聊对比学习(Contrastive Learning)第一弹
运营 23 年,昔日“国内第一大电商网站”黄了...
深化校企合作 搭建技术技能人才成长“立交桥”
MYSQL (Basic) - An article takes you into the wonderful world of MYSQL
Mysql执行原理剖析
不同的路径依赖
几个GTest、GMock的例子
部分分类网络性能对比
深入浅出边缘云 | 3. 资源配置
【每日一道LeetCode】——191. 位1的个数
The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
[Use of Qt Designer tool]
AI基础:图解Transformer
nlohmann json 使用指南【visual studio 2022】








![[TypeScript]编译配置](/img/ac/64ebd33de977e35620dbc18d2adfad.png)





