前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VBA与数据库——写个类操作ADO_写入

VBA与数据库——写个类操作ADO_写入

作者头像
xyj
发布2022-04-26 21:02:30
9920
发布2022-04-26 21:02:30
举报
文章被收录于专栏:VBA 学习
添加数据:尝试过使用Recordset的Update功能,并不是所有数据库都支持,所以还是启用事物,逐条添加:
代码语言:javascript
复制
'插入数据
Function InsertDB() As RetCode
    '选择数据源,检查标题
    Dim rngsrc As Range
    If SelectDataAndCheckField(rngsrc) = ErrRT Then
        InsertDB = ErrRT
        Exit Function
    End If
    Dim srcArr() As Variant
    srcArr = rngsrc.Value

    Dim rng As Range
    '需要插入的列对应的Fields的下表
    Dim colInsert() As Long, colInsertName() As String
    ReDim colInsert(UBound(srcArr, 2) - 1) As Long
    ReDim colInsertName(UBound(srcArr, 2) - 1) As String
    
    Dim i As Long, j As Long
    For i = 1 To UBound(srcArr, 2)
        For j = 0 To DB_Info.ActiveTable.FieldsCount - 1
            If srcArr(1, i) = DB_Info.ActiveTable.Fields(j).SName Then
                colInsert(i - 1) = j
                colInsertName(i - 1) = DB_Info.ActiveTable.Fields(j).SName
                Exit For
            End If
        Next j
        
        If j = DB_Info.ActiveTable.FieldsCount Then
            MsgBox "不能存在的列:" & srcArr(1, i)
            InsertDB = ErrRT
            Exit Function
        End If
    Next i
    
    Dim strsql As String
    strsql = "insert into " + DB_Info.ActiveTable.SName + "(" + VBA.Join(colInsertName, ",") + ") values ("
    
    If DB_Info.db.Begin Then
        MsgBox DB_Info.db.GetErr
        InsertDB = ErrRT
        Exit Function
    End If
            
    Dim sqlvalues() As String
    ReDim sqlvalues(UBound(srcArr, 2) - 1) As String
    For i = 2 To UBound(srcArr)
        'x , y
        For j = 0 To UBound(colInsert)
            sqlvalues(j) = MPublic.GetFieldValueInSql(srcArr(i, j + 1), DB_Info.ActiveTable.Fields(colInsert(j)).sType)
        Next
        
        If DB_Info.db.ExecuteNonQuery(strsql + VBA.Join(sqlvalues, ",") + ")") Then
            MsgBox DB_Info.db.GetErr
            DB_Info.db.Rollback
            InsertDB = ErrRT
            Exit Function
        End If
    Next
    DB_Info.db.Commit

    MsgBox "OK"
    InsertDB = SuccRT
End Function

按主键更新和按ID更新:2个功能是一样的原则,按照某些字段作为条件去更新数据:

代码语言:javascript
复制
'更新数据
'colsWhere   条件所在列(ID、或者主键等),对应的是单元格
Function UpdateDB(colswhere() As Long) As RetCode
    '选择数据源,检查标题
    Dim rngsrc As Range
    If SelectDataAndCheckField(rngsrc) = ErrRT Then
        UpdateDB = ErrRT
        Exit Function
    End If

    '输入需要更新数据的列
    Dim rngs As Range
    On Error Resume Next
    Set rngs = Application.InputBox("选择需要更新数据所在的列,按Ctrl多选,只能选择第一行所在单元格。", Default:=ActiveCell.Address, Type:=8)
    On Error GoTo 0
    If rngs Is Nothing Then Exit Function

    Dim rng As Range
    '需要更新的列
    Dim colUpdate() As Long
    Dim kColUpdate As Long
    For Each rng In rngs
        If rng.Row = 1 Then
            ReDim Preserve colUpdate(kColUpdate) As Long
            colUpdate(kColUpdate) = rng.Column
            kColUpdate = kColUpdate + 1
        End If
    Next
    If kColUpdate = 0 Then
        MsgBox "没有选择满足要求的更新列。"
        UpdateDB = ErrRT
        Exit Function
    End If
    
    
    Dim srcArr() As Variant
    srcArr = rngsrc.Value
    
    Dim i As Long, j As Long
    Dim sqlcmd As String
    ReDim sqlwhere(UBound(colswhere)) As String

    Dim updatefield() As String
    ReDim updatefield(kColUpdate - 1) As String
    
    If DB_Info.db.Begin Then
        MsgBox DB_Info.db.GetErr
        UpdateDB = ErrRT
        Exit Function
    End If
            
    For i = 2 To UBound(srcArr)
        'set F1=x and F2 = x
        For j = 0 To kColUpdate - 1
            updatefield(j) = DB_Info.ActiveTable.Fields(colUpdate(j) - 1).SName & "=" & MPublic.GetFieldValueInSql(srcArr(i, colUpdate(j)), DB_Info.ActiveTable.Fields(colUpdate(j) - 1).sType)
        Next
        
        For j = 0 To UBound(colswhere)
            sqlwhere(j) = DB_Info.ActiveTable.Fields(colswhere(j) - 1).SName & "=" & MPublic.GetFieldValueInSql(srcArr(i, colswhere(j)), DB_Info.ActiveTable.Fields(colswhere(j) - 1).sType)
        Next
        
        sqlcmd = "update " & DB_Info.ActiveTable.SName & " set " & VBA.Join(updatefield, ",") & " where " & VBA.Join(sqlwhere, " and ")
        If DB_Info.db.ExecuteNonQuery(sqlcmd) Then
            MsgBox DB_Info.db.GetErr
            DB_Info.db.Rollback
            UpdateDB = ErrRT
            Exit Function
        End If
    Next
    DB_Info.db.Commit
    
    MsgBox "OK"
End Function
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-04-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 VBA 学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档