做成函数,每次调用, 尽供参考。 ^_^Private Function rsSQL(ByVal strSQL As String, Optional ByVal bReadOnly As Boolean = True) As ADODB.Recordset Dim rsData As ADODB.Recordset On Error Resume NextErr.Clear Set rsData = New ADODB.Recordset With rsData .ActiveConnection = connDB 'ADO.Connection .CursorLocation = adUseClient .CursorType = adOpenKeyset .LockType = IIf(bReadOnly, adLockReadOnly, adLockPessimistic) .Source = strSQL .Open If (Err.Number <> 0) Then Set rsSQL = Nothing Set rsData = Nothing ' Debug.Print Err.Number & vbCrLf & Err.Description & vbCrLf + Err.Source Else If bReadOnly Then .ActiveConnection = Nothing ElseIf Not (.BOF And .EOF) Then .Fields(0) = .Fields(0) & "" End If Set rsSQL = rsData Set rsData = Nothing End If End WithEnd Function
尽供参考。
^_^Private Function rsSQL(ByVal strSQL As String, Optional ByVal bReadOnly As Boolean = True) As ADODB.Recordset
Dim rsData As ADODB.Recordset
On Error Resume NextErr.Clear
Set rsData = New ADODB.Recordset
With rsData
.ActiveConnection = connDB 'ADO.Connection
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.LockType = IIf(bReadOnly, adLockReadOnly, adLockPessimistic)
.Source = strSQL
.Open
If (Err.Number <> 0) Then
Set rsSQL = Nothing
Set rsData = Nothing
' Debug.Print Err.Number & vbCrLf & Err.Description & vbCrLf + Err.Source
Else
If bReadOnly Then
.ActiveConnection = Nothing
ElseIf Not (.BOF And .EOF) Then
.Fields(0) = .Fields(0) & ""
End If
Set rsSQL = rsData
Set rsData = Nothing
End If
End WithEnd Function