真的不的行,还是使用ado吧Execute、Requery 和 Clear 方法范例 该范例演示运行来自 Command 对象和 Connection 对象的 Execute 方法。同时使用 Requery 方法检索记录集中的当前数据,并用 Clear 方法清除 Errors 集合的内容。运行该过程需要 ExecuteCommand 和 PrintOutput 过程。Public Sub ExecuteX() Dim strSQLChange As String Dim strSQLRestore As String Dim strCnn As String Dim cnn1 As ADODB.Connection Dim cmdChange As ADODB.Command Dim rstTitles As ADODB.Recordset Dim errLoop As ADODB.Error ' 定义两个 SQL 语句作为命令文本执行。 strSQLChange = "UPDATE Titles SET Type = " & _ "'self_help' WHERE Type = 'psychology'" strSQLRestore = "UPDATE Titles SET Type = " & _ "'psychology' WHERE Type = 'self_help'" ' 打开连接。 strCnn = "Provider=sqloledb;" & _ "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; " Set cnn1 = New ADODB.Connection cnn1.Open strCnn ' 创建命令对象。 Set cmdChange = New ADODB.Command Set cmdChange.ActiveConnection = cnn1 cmdChange.CommandText = strSQLChange
' 打开标题表。 Set rstTitles = New ADODB.Recordset rstTitles.Open "titles", cnn1, , , adCmdTable ' 打印原始数据报告。 Debug.Print _ "Data in Titles table before executing the query" PrintOutput rstTitles ' 清除 Errors 集合的外部错误。 cnn1.Errors.Clear ' 调用 ExecuteCommand 子例程执行 cmdChange 命令。 ExecuteCommand cmdChange, rstTitles
' 打印新数据报告。 Debug.Print _ "Data in Titles table after executing the query" PrintOutput rstTitles ' 使用 Connection 对象的 execute 方法执行 SQL 语句以恢复数据。 ' 捕获错误,必要时检查 Errors 集合。 On Error GoTo Err_Execute cnn1.Execute strSQLRestore, , adExecuteNoRecords On Error GoTo 0 ' 通过再查询记录集检索当前数据。 rstTitles.Requery ' 打印已恢复数据的报告。 Debug.Print "Data after executing the query " & _ "to restore the original information" PrintOutput rstTitles rstTitles.Close cnn1.Close
Exit Sub
Err_Execute: ' 将任何由执行查询引起的错误通知用户。 If Errors.Count > 0 Then For Each errLoop In Errors MsgBox "Error number: " & errLoop.Number & vbCr & _ errLoop.Description Next errLoop End If
Resume NextEnd SubPublic Sub ExecuteCommand(cmdTemp As ADODB.Command, _ rstTemp As ADODB.Recordset) Dim errLoop As Error
' 运行指定的 Command 对象。捕获错误,必要时检查 Errors 集合。 On Error GoTo Err_Execute cmdTemp.Execute On Error GoTo 0 ' 通过再查询记录集检索当前数据。 rstTemp.Requery
Exit SubErr_Execute: ' 将任何由执行查询引起的错误通知用户。 If Errors.Count > 0 Then For Each errLoop In Errors MsgBox "Error number: " & errLoop.Number & vbCr & _ errLoop.Description Next errLoop End If
Resume NextEnd SubPublic Sub PrintOutput(rstTemp As ADODB.Recordset) ' 枚举 Recordset。 Do While Not rstTemp.EOF Debug.Print " " & rstTemp!Title & _ ", " & rstTemp!Type rstTemp.MoveNext LoopEnd Sub
该范例演示运行来自 Command 对象和 Connection 对象的 Execute 方法。同时使用 Requery 方法检索记录集中的当前数据,并用 Clear 方法清除 Errors 集合的内容。运行该过程需要 ExecuteCommand 和 PrintOutput 过程。Public Sub ExecuteX() Dim strSQLChange As String
Dim strSQLRestore As String
Dim strCnn As String
Dim cnn1 As ADODB.Connection
Dim cmdChange As ADODB.Command
Dim rstTitles As ADODB.Recordset
Dim errLoop As ADODB.Error ' 定义两个 SQL 语句作为命令文本执行。
strSQLChange = "UPDATE Titles SET Type = " & _
"'self_help' WHERE Type = 'psychology'"
strSQLRestore = "UPDATE Titles SET Type = " & _
"'psychology' WHERE Type = 'self_help'" ' 打开连接。
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
Set cnn1 = New ADODB.Connection
cnn1.Open strCnn ' 创建命令对象。
Set cmdChange = New ADODB.Command
Set cmdChange.ActiveConnection = cnn1
cmdChange.CommandText = strSQLChange
' 打开标题表。
Set rstTitles = New ADODB.Recordset
rstTitles.Open "titles", cnn1, , , adCmdTable ' 打印原始数据报告。
Debug.Print _
"Data in Titles table before executing the query"
PrintOutput rstTitles ' 清除 Errors 集合的外部错误。
cnn1.Errors.Clear ' 调用 ExecuteCommand 子例程执行 cmdChange 命令。
ExecuteCommand cmdChange, rstTitles
' 打印新数据报告。
Debug.Print _
"Data in Titles table after executing the query"
PrintOutput rstTitles ' 使用 Connection 对象的 execute 方法执行 SQL 语句以恢复数据。
' 捕获错误,必要时检查 Errors 集合。
On Error GoTo Err_Execute
cnn1.Execute strSQLRestore, , adExecuteNoRecords
On Error GoTo 0 ' 通过再查询记录集检索当前数据。
rstTitles.Requery ' 打印已恢复数据的报告。
Debug.Print "Data after executing the query " & _
"to restore the original information"
PrintOutput rstTitles rstTitles.Close
cnn1.Close
Exit Sub
Err_Execute: ' 将任何由执行查询引起的错误通知用户。
If Errors.Count > 0 Then
For Each errLoop In Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume NextEnd SubPublic Sub ExecuteCommand(cmdTemp As ADODB.Command, _
rstTemp As ADODB.Recordset) Dim errLoop As Error
' 运行指定的 Command 对象。捕获错误,必要时检查 Errors 集合。
On Error GoTo Err_Execute
cmdTemp.Execute
On Error GoTo 0 ' 通过再查询记录集检索当前数据。
rstTemp.Requery
Exit SubErr_Execute: ' 将任何由执行查询引起的错误通知用户。
If Errors.Count > 0 Then
For Each errLoop In Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume NextEnd SubPublic Sub PrintOutput(rstTemp As ADODB.Recordset) ' 枚举 Recordset。
Do While Not rstTemp.EOF
Debug.Print " " & rstTemp!Title & _
", " & rstTemp!Type
rstTemp.MoveNext
LoopEnd Sub