当前位置:网站首页>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.
边栏推荐
猜你喜欢

【剑指 Offe】剑指 Offer 18. 删除链表的节点

【Pointing to Offer】Pointing to Offer 18. Delete the node of the linked list

OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法

2种手绘风格效果比较,你更喜欢哪一种呢?

国轩高科瑞交所上市:募资近7亿美元 为瑞士今年最大融资项目

NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...

kotlin by lazy

深入浅出边缘云 | 3. 资源配置

Does the satellite phone communicate directly with the satellite or through a ground station?

生物医学论文有何价值 论文中译英怎样翻译效果好
随机推荐
跨域问题的解决方法
C# wpf borderless window add shadow effect
【剑指 Offe】剑指 Offer 18. 删除链表的节点
node封装一个控制台进度条插件
MySQL数据类型
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显
MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
Scrapy framework is introduced
开心的聚餐
二分答案裸题(加一点鸽巢原理)
积性函数
还有三天忙完
Fixed asset visualization intelligent management system
设计消息队列存储消息数据的 MySQL 表格
Spark学习:用spark实现ETL
解决终极bug,项目最终能顺利部署上线。
线性筛求积性函数
微信小程序云开发 | 城市信息管理
经济新闻:错误# 15:初始化libiomp5md。dll,但发现libiomp5md。已经初始化dll。解决方法




