1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| Option Explicit
Private WithEvents conn As ADODB.Connection
Private Sub conn_ExecuteComplete(ByVal RecordsAffected As Long, _
ByVal pError As ADODB.Error, _
adStatus As ADODB.EventStatusEnum, _
ByVal pCommand As ADODB.Command, _
ByVal pRecordset As ADODB.Recordset, _
ByVal pConnection As ADODB.Connection)
Dim ws As Worksheet
Dim iCols As Integer
StopChrono
Set ws = Worksheets("Feuil2")
'MsgBox "Execution completed"
For iCols = 0 To pRecordset.Fields.Count - 1
ws.Cells(1, iCols + 1).Value = pRecordset.Fields(iCols).Name
Next
ws.Range(ws.Cells(1, 1), _
ws.Cells(1, pRecordset.Fields.Count)).Font.Bold = True
ws.Range("A2").CopyFromRecordset pRecordset
End Sub
Sub execSPAsync()
Dim strSQL As String, i As Integer, cm As Object
Const adOpenStatic = 3, adOpenKeyset = 1, adUseClient = 3
Const adLockReadOnly = 1, adAsyncExecute = 16, adExecuteNoRecords = 128
Const adCmdText = 1, adCmdStoredProc = 4
Set conn = Nothing: Set cm = Nothing
Set conn = CreateObject("ADODB.Connection")
Set cm = CreateObject("ADODB.Command")
Dim sconnect As String
sconnect = "Driver=PostgreSQL ODBC Driver(ANSI);User ID=test;Password=test;" & _
"Host=localhost;Port=5432;Database=Northwind;Pooling=true;" & _
"Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;"
' OPEN CONNECTION
conn.Open sconnect
With cm
.ActiveConnection = conn
.CommandText = "select mystoredproc(5);"
.CommandTimeout = 0
.CommandType = adCmdText
End With
' OPEN RECORDSET
'conn.Execute strSQL, adExecuteNoRecords, adAsyncExecute
cm.Execute , , adAsyncExecute
End Sub |
Partager