VB 把excel中内容添加到access中


除了access可以从excel直接导入(有点不符合本人观点)外,

可以用VB来操作。

两个注意点:

1、先查询记录在access是否存在,存在了就不用添加。

2、打开时,用键集游标adOpenKeyset,不然那个查询的记录集的个数是-1,为判断是否存在记录造成困惑。



Private Sub Command1_Click()
    Dim cn    As New ADODB.Connection
    Dim rs    As New ADODB.Recordset
    Dim i     As Integer, j As Integer
    Dim xl    As New Excel.Application
    Dim book  As Excel.Workbook
    Dim sheet As Excel.Worksheet

    Set book = xl.Workbooks.Open("D:\etoa\2.xls") '打开excel,
    Set sheet = book.Worksheets(1)
    
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\eToa\Station.mdb;Persist Security Info=False"

    For i = 1 To 282
        '查询
        rs.Open "select * from station where StationNumber='" & Trim(sheet.Cells(i, 1)) & "'", cn, adOpenKeyset, adLockOptimistic
        '是否有记录,有记录就不添加
        If rs.RecordCount = 0 Then '没有记录,添加
            rs.AddNew
            rs.Fields(1) = sheet.Cells(i, 1) '注意fields索引是以0开始,excel是以1开始
            rs.Fields(2) = sheet.Cells(i, 2)
            rs.Fields(3) = sheet.Cells(i, 3)
            rs.Fields(4) = sheet.Cells(i, 4)
            rs.Fields(5) = sheet.Cells(i, 5)
            rs.Fields(6) = sheet.Cells(i, 6)
            rs.Fields(7) = sheet.Cells(i, 7)
            rs.Update
        Else
            List1.AddItem sheet.Cells(i, 1) '这是没有被添加的记录
        End If
        rs.Close
    Next i
    MsgBox "导入完成"
    book.Close '退出
    xl.Quit
    cn.Close
End Sub


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dim extend_str As String Dim execl_cnn As New ADODB.Connection Dim execl_rst As New ADODB.Recordset Dim execl_count_rst As New ADODB.Recordset '纪录数量 Dim execl_field_rst As New ADODB.Recordset Dim execl_strconn As String Dim execl_strcmd As String Dim execl_count_str As String Dim insertStr As String 操作oracle数据库的记录集 Dim rst As ADODB.Recordset CommonDialog1.ShowOpen extend_str = Right(CommonDialog1.FileName, 3) If (extend_str = "xls") Then execl_cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & CommonDialog1.FileName & _ ";Extended Properties=Excel 8.0;" ElseIf (extend_str = "dbf") Then execl_cnn.ConnectionString = "Provider=MSDASQL.1;Driver=Microsoft Visual Foxpro Driver;SourceType=DBF;DBQ=" & getDir (CommonDialog1.FileName) execl_cnn.Open End If If (extend_str = "xls") Then execl_strcmd = "SELECT * from `Sheet1$` " execl_count_str = "SELECT count(*) from `Sheet1$` " execl_field_rst.Open execl_count_str, execl_cnn If (execl_field_rst.Fields(0).Value = 0) Then execl_strcmd = "SELECT * from [" & getFileNoExt(getfile(CommonDialog1.FileName)) & "$] " execl_count_str = "SELECT count(*) from [" & getFileNoExt(getfile(CommonDialog1.FileName)) & "$] " End If execl_field_rst.Close ElseIf (extend_str = "dbf") Then execl_strcmd = "SELECT * from " & CommonDialog1.FileName execl_count_str = "SELECT count(*) from " & CommonDialog1.FileName End If execl_rst.Open execl_strcmd, execl_cnn execl_count_rst.Open execl_count_str, execl_cnn If (CommonDialog1.FileName "") Then '进度条设置 ProgressBar1.Min = 0 ProgressBar1.max = execl_count_rst.Fields(0).Value If (execl_rst.RecordCount) Then '如果有记录 If (Option1.Value) Then '如果是仪器总库 If (execl_rst.Fields.Count = 8) Then Set rst = yg_gain_table_recordset("yg_device") execl_rst.MoveFirst Do While (Not execl_rst.EOF) rst.AddNew rst.Fields(0).Value = yg_getNextId("yg_device_id") rst.Fields(1).Value = execl_rst.Fields(0).Value & "" rst.Fields(2).Value = execl_r
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值