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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
Private Function ExeSQL(Requête As String, CheminBDD As String, TblxRequête() As Variant, Optional Head As Byte = 1) As Long
'FUNCTION D'EXECUTION DE REQUETES
Dim Connexion, Resultats As Object
Dim Dbrute As Variant
Dim NbrColonne, Colonne, ligne As Long
If Requête = "" Then Err.Raise vbObjectError + 3, , "Requête absente!"
On Error GoTo GestionErreur
Erase TblxRequête
'CONNEXION -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Set Connexion = CreateObject("ADODB.Connection")
With Connexion
'ACCESS
.Provider = "MSDASQL"
.ConnectionTimeout = 30
.Open "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & CheminBDD & mSecure
.CursorLocation = 2
End With
'-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
'EXECUTION REQUETE -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
If Left(Requête, 6) = "SELECT" Then
Set Resultats = CreateObject("ADODB.Recordset")
Resultats.CursorLocation = 2
Resultats.Open Requête, Connexion, 3
NbrColonne = Resultats.Fields.Count - 1
If Head = 1 Then
ReDim TblxRequête(NbrColonne, 0)
For Colonne = 0 To NbrColonne
TblxRequête(Colonne, 0) = Resultats.Fields(Colonne).Name
Next Colonne
End If
ExeSQL = Resultats.RecordCount
If Not ExeSQL = 0 Then
If Head = 1 Then
ReDim Preserve TblxRequête(NbrColonne, ExeSQL)
Else
ReDim TblxRequête(NbrColonne, ExeSQL - 1)
End If
ReDim Dbrute(NbrColonne, ExeSQL - 1)
Resultats.MoveFirst
Dbrute = Resultats.GetRows
For Colonne = 0 To UBound(Dbrute, 1)
For ligne = 0 To UBound(Dbrute, 2)
TblxRequête(Colonne, ligne + Head) = IIf(IsNull(Dbrute(Colonne, ligne)), "", Dbrute(Colonne, ligne))
TblxRequête(Colonne, ligne + Head) = RTrim(TblxRequête(Colonne, ligne + Head))
If TblxRequête(Colonne, ligne + Head) = "" And mAbsenceD <> "" Then TblxRequête(Colonne, ligne + Head) = mAbsenceD
Next ligne
Next Colonne
End If
Else
Connexion.Execute Requête
ExeSQL = 0
End If
Connexion.Close
'-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Set Resultats = Nothing
Set Connexion = Nothing
mderoul = True
On Error GoTo 0
Exit Function
GestionErreur:
If Not Resultats Is Nothing Then If Resultats.State = 1 Then Resultats.Close
If Not Connexion Is Nothing Then If Connexion.State = 1 Then Connexion.Close
Set Resultats = Nothing
Set Connexion = Nothing
ExeSQL = -1
Err.Raise vbObjectError + 3, , "!!! Erreur lors de l'execution de la requête. !!!" & Chr(10) & Chr(10) & _
"Erreur N°" & Err.Number & Chr(10) & _
"Description " & Err.Description
End Function |
Partager