当前位置:网站首页>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 Sub
Method 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 Sub
Processing 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.
边栏推荐
- [Summary] 1396- 60+ VSCode plugins to create a useful editor
- Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
- 怎么样的框架对于开发者是友好的?
- Pytorch foundation -- tensorboard use (1)
- redis
- Delay queue optimization (2)
- JS提升:Promise中reject与then之间的关系
- 不同的路径依赖
- 浅聊对比学习(Contrastive Learning)第一弹
- LeetCode Exercise - Two Questions About Finding Sum of Array Elements
猜你喜欢
Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
Hello, my new name is "Bronze Lock/Tongsuo"
AI Basics: Graphical Transformer
SwiftUI iOS 精品开源项目之 完整烘焙食品菜谱App基于SQLite(教程含源码)
6 yuan per catty, why do Japanese companies come to China to collect cigarette butts?
Pytorch foundation -- tensorboard use (1)
防抖和节流有什么区别,分别用于什么场景?
MySQL数据类型
SimpleOSS第三方库libcurl与引擎libcurl错误解决方法
【PHPWord】PHPOffice 套件之PHPWord快速入门
随机推荐
又一家公司面试的内容
国轩高科瑞交所上市:募资近7亿美元 为瑞士今年最大融资项目
Object和Map的区别
scrapy基本使用
几个GTest、GMock的例子
防抖和节流有什么区别,分别用于什么场景?
猎豹移动终于递交年报:年营收7.85亿 腾讯持股16.6%
The use of @ symbol in MySql
架构师如何成长
Mysql execution principle analysis
VBA批量将Excel数据导入Access数据库
自然语言处理nltk
JsonUtil基于字符串操作josn
牛客刷题系列之进阶版(搜索旋转排序数组,链表内指定区间反转)
Meta元宇宙部门第二季度亏损28亿!仍要继续押注?元宇宙发展尚未看到出路!
电脑死机的时候,发生了什么?
7.30模拟赛总结
Scrapy framework is introduced
【PHPWord】PHPOffice 套件之PHPWord快速入门
SimpleOSS第三方库libcurl与引擎libcurl错误解决方法