【Access源码示例】导入系列-Excel之循环读取单元格数据方法
时 间:2020-08-25 09:25:21
作 者:金宇 ID:43 城市:江阴
摘 要:通过循环读取Excel每行单元格内的数据导入到Access的表中。
正 文:
代码如下:
Private Sub btnImport_Click() On Error GoTo ErrorHandler: Dim strSQL As String Dim rst As Object Dim cnn As Object Dim rstXL As Object Dim objExcel As Object Dim objBook As Object Dim objSheet As Object Dim lngI As Long If IsNull(Me.strFilePath) Then MsgBox "请先选择文件!", vbInformation, "提示" Me.strFilePath.SetFocus Exit Sub End If If IsNull(Me.strSheetName) Then MsgBox "请先选择Excel工作表!", vbInformation, "提示" Me.strSheetName.SetFocus Exit Sub End If '打开Access记录集 Set rst = CreateObject("adodb.recordset") rst.Open "select * from tb_Parts where 1=2", CurrentProject.Connection, 1, 3 '打开Excel Set objExcel = CreateObject("Excel.Application") Set objBook = objExcel.Workbooks.Open(Me.strFilePath) Set objSheet = objBook.WorkSheets("" & Me.strSheetName & "") objSheet.Select With objSheet For lngI = 2 To 65536 '循环行记录 If .Cells(lngI, 1) = "" Then Exit For rst.AddNew rst![Part No] = .Cells(lngI, 1) rst![Part Name] = .Cells(lngI, 2) rst![Category] = .Cells(lngI, 3) rst![Part Type] = .Cells(lngI, 4) rst![Unit Cost] = .Cells(lngI, 5) rst![Cons Cost] = .Cells(lngI, 6) rst.Update Next End With rst.Close MsgBox "Import Success!" DoCmd.OpenTable "tb_Parts" Exithere: DoCmd.Hourglass False If Not objBook Is Nothing Then objBook.Saved = True If Not objExcel Is Nothing Then objExcel.Quit Set objSheet = Nothing Set objBook = Nothing Set objExcel = Nothing Set rst = Nothing Exit Sub ErrorHandler: MsgBox Err.Description, vbInformation, "提示" Resume Exithere End Sub
图 示:
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access判断多条明细的配方或订...(11.30)
- 如何让后台数据库在局域网共享时,且...(11.29)
- 【Access月初月末日期设置方法...(11.29)
- 【Access IIF函数嵌套示例...(11.26)
- Access快速开发平台--使用组...(11.25)
- Access快速开发平台--对上传...(11.22)
- Access快速开发平台企业版--...(11.18)
- 不会用多表联合查询,多表查询没结果...(11.16)
- 【案例分享】主键字段值含有不间断空...(11.16)
- Access快速开发平台--后台D...(11.14)