7,785
社区成员




Dim cnn As ADODB.Connection
Dim errs As ADODB.Error
Dim rst As ADODB.Recordset
Dim pars As String
Dim flds As String
Dim strError As String
Dim p As Integer
Private Sub Test()
Set cnn = New ADODB.Connection
On Error GoTo cnnopenerr
cnn.Open "DSN=DSNname;uid=userid;pwd=password"
On Error GoTo 0
If cnn.State = adStateClosed Then
MsgBox "Connect DSN failure, please try again."
End 'Form
End If
Set rst = New ADODB.Recordset
If rst.State <> adStateClosed Then rst.Close
On Error GoTo TAerr
rst.CursorLocation = adUseClient
rst.Open "存储过程名 参数", cnn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Not (rst.EOF And rst.BOF) Then
Do
flds = ""
For p=1 to rst.Fields.Count
flds = flds + " " + Trim(CStr(rst.Fields.Item(p)))
Next
Debug.print "["+flds+"]"
rst.MoveNext
if rst.EOF Then Exit Do
Loop
End If
rst.Close
cnn.Close
Exit Sub
TAerr:
For Each errs In cnn.Errors
strError = " " & errs.Description & vbCrLf & _
" (Source: " & errs.Source & ")" & vbCrLf & _
" (SQL State: " & errs.SQLState & ")" & vbCrLf & _
" (NativeError: " & errs.NativeError & ")" & vbCrLf & vbCrLf
If errs.Number = 0 Then Exit For
Debug.Print "Error #" & Hex(errs.Number) + vbCrLf + strError
Exit For
Next errs
cnn.Errors.Clear
Exit Sub
cnnopenerr:
For Each errs In cnn.Errors
Debug.Print errs.Description
cnn.Errors.Clear
Exit For
Next errs
Resume Next
End Sub