执行cbInput的SQL:
'执行指定sql语句
Sub rbbtnExcsql(control As IRibbonControl)
Dim ret As Long
If VBA.Len(DB_Info.Path) Then
If VBA.Len(MPublic.scbInput) Then
ExecSQL MPublic.scbInput
Else
MsgBox "请在rbcbInput设置sql语句", vbInformation
End If
Else
MsgBox "请选择DB"
End If
End Sub
执行活动单元格的SQL:
Sub rbbtnExcActivecellSql(control As IRibbonControl)
If VBA.Len(DB_Info.Path) Then
If VBA.Len(ActiveCell.Value) Then
If ExecSQL(ActiveCell.Value) = SuccRT Then MsgBox "OK"
Else
MsgBox "请在活动单元格输入sql语句", vbInformation
End If
Else
MsgBox "请选择DB"
End If
End Sub
执行多个单元格SQL(事务):
Sub rbbtnExcCellsSql(control As IRibbonControl)
If VBA.Len(DB_Info.Path) Then
If VBA.TypeName(Selection) = "Range" Then
Dim arr() As Variant
Dim v As Variant
arr = Selection.Value
If DB_Info.db.Begin Then
MsgBox DB_Info.db.GetErr
Exit Sub
End If
For Each v In arr
If ExecSQL(VBA.CStr(v)) = ErrRT Then
DB_Info.db.Rollback
Exit Sub
End If
Next
DB_Info.db.Commit
MsgBox "OK"
Else
MsgBox "请选择单元格", vbInformation
End If
Else
MsgBox "请选择DB"
End If
End Sub
执行sql语句的功能,主要是判断一些是需要返回数据的还是不需要返回数据的:
Private Function ExecSQL(strsql As String) As RetCode
Dim ret As Long
Select Case VBA.LCase$(VBA.Left$(strsql, 6))
Case "select", "pragma"
ret = DB_Info.db.ExecuteToExcel(strsql, Range("A1"))
Case Else
ret = DB_Info.db.ExecuteNonQuery(strsql)
End Select
If ret Then
MsgBox DB_Info.db.GetErr()
ExecSQL = ErrRT
End If
End Function
AllData:这个其实应该放到读取中!就是把数据库的所有表的数据都读取出来:
Sub rbbtnAllTableAllData(control As IRibbonControl)
Dim i As Long
If VBA.Len(DB_Info.Path) Then
If DB_Info.TablesCount = 0 Then
MsgBox "请选点击[读取表名]。"
Exit Sub
End If
For i = 0 To DB_Info.TablesCount - 1
Worksheets.Add(After:=Worksheets(Worksheets.Count)).name = DB_Info.Tables(i).SName
If DB_Info.db.ExecuteToExcel("select * from " & DB_Info.Tables(i).SName, Range("A1")) Then
MsgBox DB_Info.db.GetErr()
Exit Sub
End If
Next
Else
MsgBox "请选择DB"
End If
End Sub