【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选项卡示例】Ac...(09.09)
- 【Access源码示例】按输入...(09.02)
- 【Access日期区间段查询】...(08.29)
- 【Access日期区间段查询】...(08.27)
- Access怎样才能实现日期时...(08.21)
- 【Access定时打开查询】A...(08.19)
- Access生成固定数量的记录...(08.13)
- Access怎样才能实现日期时...(08.12)
- Access利用导航窗体控件对...(08.03)
学习心得
最新文章
- Access表中的字段名、字段标题...(09.19)
- Access快速开发平台--更改“...(09.18)
- 【中秋及国庆优惠】Access培训...(09.15)
- Access如何将日期型的数值转换...(09.14)
- 英文输入法输入数据中存在单引号引起...(09.11)
- 【Access选项卡示例】Acce...(09.09)
- 让Access光标停留在指定的控件...(09.07)
- 关于Access查询条件里使用通配...(09.06)
- Access报表偷懒制作法--Ac...(09.05)
- Access快速开发平台--窗体数...(09.04)