当前位置:网站首页>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.
边栏推荐
- 固定资产可视化智能管理系统
- Node encapsulates a console progress bar plugin
- Object和Map的区别
- Anaconda Navigator卡在loading applications
- 【剑指 Offe】剑指 Offer 18. 删除链表的节点
- 开心的聚餐
- 7.30模拟赛总结
- VS Code 连接SQL Server
- Swiper rotates pictures and plays background music
- 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
猜你喜欢
Critical Reviews | A review of the global distribution of antibiotics and resistance genes in farmland soil by Nannong Zou Jianwen's group
CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配
WeChat Mini Program Cloud Development | Urban Information Management
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
【总结】1396- 60+个 VSCode 插件,打造好用的编辑器
Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
一文读懂“语言模型”
golang日志库zerolog使用记录
nlohmann json 使用指南【visual studio 2022】
Does the satellite phone communicate directly with the satellite or through a ground station?
随机推荐
Go 系统收集
WEBSOCKETPP使用简介+demo
Anaconda Navigator卡在loading applications
C# wpf borderless window add shadow effect
VBA 运行时错误‘-2147217900(80040e14):自动化(Automation)错误
高精度加法
Range.CopyFromRecordset 方法 (Excel)
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
阿里云武林头条活动分享
终端分屏工具Terminalx的使用
又一家公司面试的内容
Mysql执行原理剖析
微信小程序云开发 | 城市信息管理
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
[Use of Qt Designer tool]
The Meta metaverse division lost 2.8 billion in the second quarter!Still want to keep betting?Metaverse development has yet to see a way out!
高并发秒杀项目总结
natural language processing nltk
crontab中写go run不执行的问题
2种手绘风格效果比较,你更喜欢哪一种呢?