这个删除、插入、修改都可以Public Function ExecuteSQL(ByVal SQL As String) As ADODB.Recordset Dim cnn As New ADODB.Connection Dim rst As ADODB.Recordset Dim sTokens() As String
On Error GoTo Error_Do cnn.Open PublicStr sTokens() = Split(SQL)
If InStr("INSERT,DELETE,UPDATE", UCase(sTokens(0))) Then cnn.Execute SQL Else Set rst = New ADODB.Recordset
With rst .ActiveConnection = cnn .CursorLocation = adUseClient .CursorType = adOpenStatic .LockType = adLockOptimistic .Open Trim(SQL) End With Set ExecuteSQL = rst End If
Set cnn = Nothing Set rst = Nothing Exit Function Error_Do: Dim Err_Str As String Err_Str = "服务器" & Err.Description If MsgBox(Err_Str, vbRetryCancel + vbCritical, "警告") = vbRetry Then Resume Else Set cnn = Nothing Set rst = Nothing End End If End Function
Dim cnn As New ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo Error_Do cnn.Open PublicStr
sTokens() = Split(SQL)
If InStr("INSERT,DELETE,UPDATE", UCase(sTokens(0))) Then
cnn.Execute SQL
Else
Set rst = New ADODB.Recordset
With rst
.ActiveConnection = cnn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.Open Trim(SQL)
End With
Set ExecuteSQL = rst
End If
Set cnn = Nothing
Set rst = Nothing
Exit Function
Error_Do:
Dim Err_Str As String
Err_Str = "服务器" & Err.Description
If MsgBox(Err_Str, vbRetryCancel + vbCritical, "警告") = vbRetry Then
Resume
Else
Set cnn = Nothing
Set rst = Nothing
End
End If
End Function