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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| Public AppExcel As Object
Public Classeur As Object
Public Feuille As Object
Public ADOCnx As New ADODB.Connection
Public mRst As New ADODB.Recordset
Private Sub Button1_Click()
ret = InitConnection("mag", "xx", "xx")
ret = ExportDonneesExcel()
ret = CloseConnection()
End Sub
Private Function ExportDonneesExcel()
ret = OuvrirFichierExcel("TEST.xls", "Resultat")
ret = AjoutDansTableIngres("mb_commande_ad")
ret = FermerFichierExcel()
End Function
Private Function OuvrirFichierExcel(NomFichier As String, NomFeuille As String)
MyPath = ActiveWorkbook.Path
' Déclaration de référence d'objet à une variable
Application.StatusBar = "Ouverture du classeur Excel contenant les données à exporter ..."
Set AppExcel = CreateObject("Excel.Application")
Set Classeur = AppExcel.Workbooks.Open(MyPath & "/" & NomFichier)
Set Feuille = Classeur.Worksheets(NomFeuille) ' Adapter le nom de la feuille
Application.StatusBar = ""
End Function
Private Function AjoutDansTableIngres(NomTable As String)
Dim I As Integer
Dim rr As String
'Inscrit les valeurs dans la table ListeClients
If NomTable = "mb_commande_ad" Then
With mRst
.CursorType = 1
.LockType = 3
.Open "SELECT * FROM " & NomTable, ADOCnx
'Ajout de plusieurs enregistrements situés à partir de
' la ligne 2 afin d'éviter les entêtes de colonnes
rr = Feuille.Cells(2, 2 + 10)
For I = 2 To Feuille.Range("A65536").End(-4162).Row
Nbrecords = Feuille.Range("A65536").End(-4162).Row - 1
.AddNew
.Fields("mb_nomag") = Feuille.Cells(I, 2 + 10)
.Fields("mb_noart") = Feuille.Cells(I, 3 + 10)
.Fields("mb_qtecde") = Feuille.Cells(I, 4 + 10)
.Fields("mb_datesais") = Feuille.Cells(I, 6 + 10)
.Fields("mb_pahtcde") = 0
.Fields("mb_remise") = 0
.Fields("mb_noligne") = 0
.Fields("mb_heursais") = 0
.Fields("mb_topfour") = ""
.Fields("mb_topentr") = ""
.Fields("mb_coddev") = ""
.Update
Next I
End With
End If
'-------------------------------------------------------------------
If NomTable = "mb_message_ad" Then
With mRst
.CursorType = 1
.LockType = 3
.Open "SELECT * FROM " & NomTable, ADOCnx
'Ajout de plusieurs enregistrements situés à partir de
' la ligne 2 afin d'éviter les entêtes de colonnes
For I = 2 To Feuille.Range("A65536").End(-4162).Row
Nbrecords = Feuille.Range("A65536").End(-4162).Row - 1
.AddNew
.Fields("mb_mag") = Feuille.Cells(I, 2 + 10)
.Fields("mb_nofour") = Feuille.Cells(I, 5 + 10)
.Fields("mb_lig1four") = Feuille.Cells(I, 9 + 10)
.Fields("mb_lig2four") = ""
.Fields("mb_lig3four") = ""
.Fields("mb_lig4four") = ""
.Fields("mb_lig5four") = ""
.Fields("mb_lig1mag") = ""
.Fields("mb_lig2mag") = ""
.Fields("mb_lig3mag") = ""
.Fields("mb_lig4mag") = ""
.Fields("mb_lig5mag") = ""
.Fields("mb_datliv") = Feuille.Cells(I, 8 + 10)
.Fields("mb_mhtpub") = 0
.Update
Next I
End With
End If
End Function
Public Function InitConnection(DSN As String, UserName As String, PassWord As String) As Boolean
Dim query As String
Dim cnxString As String
Dim iAffected As Integer
'Connection à la BDD
InitConnection = False
'Initialisation de la chaine de connexion
cnxString = "DSN=" & DSN & ";"
'Vérifie que la connexion est bien fermée
If ADOCnx.State = adStateOpen Then
ADOCnx.Close
End If
On Error GoTo BadConnection
'Connexion à la base de données
ADOCnx.Open cnxString, UserName, PassWord, adAsyncConnect
'Attente que la connexion soit établie
While (ADOCnx.State = adStateConnecting)
DoEvents
Wend
'Vérification des erreurs dans le cas d'une mauvaise connexion
If ADOCnx.Errors.Count > 0 Then
'Affichage des erreurs
MsgBox ADOCnx.Errors.Item(0)
InitConnection = False
Exit Function
Else
InitConnection = True
End If
Exit Function
BadConnection:
If ADOCnx.Errors.Count > 0 Then
'Affichage des erreurs
MsgBox ADOCnx.Errors.Item(0)
InitConnection = False
Exit Function
Else
MsgBox Err.Description
End If
End Function
Private Function CloseConnection()
'Fermeture de la connection à la BDD
If ADOCnx.State = adStateOpen Then
ADOCnx.Close
End If
End Function
Private Function FermerFichierExcel()
Classeur.Close
AppExcel.Quit
' Met fin à l'association entre la variable et l'objet associé
Set AppExcel = Nothing
Set Classeur = Nothing
Set Feuille = Nothing
End Function |
Partager