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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
Private Sub Commande21_Click()
'importer toutes les factures d'un repertoire
Dim bd As Database
Dim reponse As String
Dim repertoire As String
Dim fichier As String
Dim extension As String
Dim animal As String
Dim année As Integer
Dim semaine As String
Dim extraction As String
Dim i As Integer
Dim j As Integer
Dim Rst As Recordset
Dim rst1 As Recordset
Dim texte As String
Set bd = CurrentDb 'definition de la base de données de reference (celle en cours)
Set rst1 = bd.OpenRecordset("doublons", dbOpenDynaset)
DoCmd.RunSQL "ALTER TABLE factures DROP CONSTRAINT PrimaryKey" 'on enleve la clé primaire pour eviter le bloquage du prog sur les doublons
'C:\Documents and Settings\CUMA de l'Onglet\Mes documents\CUMAFACTURE
i = 0 'initialisation du compteur de factures extraites
j = 0 'initialisation du compteur de doublons
extension = "*.xls" 'selection de l'extension à rechercher dans le repertoire
animal = InputBox("type d'animal desiré?") 'selection du type de facture à importer
année = InputBox("quelle année?") 'choix de l'année des factures
reponse = MsgBox("lancer l'import", vbYesNo, "import des factures") 'demande de confirmation de lancement de la tache
Select Case reponse 'suivant la reponse
Case vbYes 'si oui
'MsgBox ("appuyer sur echap pour arreter la tache a tout moment")
'While GetAsyncKeyState(27) = 0 'tant que la touche echap n'as pas été pressé on poursui la tache
If animal = "agneaux" Then
excel.Workbooks.Open ("E:\agneaux\2008\Facture vierge AGNEAUXP") 'ouverture du fichier contenant la macro
repertoire = ("E:\agneaux\" & année & "\") 'definition du repertoire de recherche
ElseIf animal = "bovins" Then
excel.Workbooks.Open ("E:\GROSBOVIN\2008\Facture vierge bovinP.xls")
repertoire = ("E:\GROSBOVIN\" & année & "\")
ElseIf animal = "porcs" Then
excel.Workbooks.Open ("E:\Porcs\2008\Facture vierge porcsP.xls")
repertoire = ("E:\Porcs\" & année & "\")
ElseIf animal = "veaux" Then
excel.Workbooks.Open ("E:\Veaux\2008\Facture vierge veauxP.xls")
repertoire = ("E:\Veaux\" & année & "\")
End If
fichier = Dir(repertoire & extension) 'association du repertoire et l'extension pour obtenir uniquement le nom du fichier
MsgBox (fichier) 'affichage du premier fichier du repertoire
Do Until Left(fichier, 14) = "Facture vierge" Or fichier = "" 'parcours du repertoire
excel.Workbooks.Open (repertoire & fichier) 'ouverture du fichier excel
extraction = Cells(2, 9).Value
Select Case extraction
Case Is <> "OUI"
Cells(2, 9).Value = "non"
Select Case animal
Case "agneaux"
Set Rst = bd.OpenRecordset("factures", dbOpenDynaset) 'ouverture du recordset pour parcours de la table factures
With Rst
Do While Not .EOF And i >= 1 'tant qu'on est pas en fin de table ou qu'on est pas dans la cas d'un premier enregistrement
texte = Mid(Cells(12, 1), 12, 35) 'stockage du numero de facture dans une variable
Rst.FindFirst "[numero facture] like'" & texte & "'" 'comparaison du numero facture du fichier excel et de la table factures
If Rst.NoMatch Then 'si ils ne sont pas egaux on insere
excel.Application.Run ("'Facture vierge AGNEAUXP.xls'!extractagneaux") 'exceution de ma macro d'import des données
Rst.Close 'fermeture du recordset
Set Rst = Nothing 'liberation des ressources
Exit Do 'on sort de la boucle
Else 'sinon
With rst1
rst1.AddNew 'c'est un doublons, donc on l'insere dans la table doublons
![type facture] = Worksheets("Feuil1").Range("I1")
![date facture] = Worksheets("Feuil1").Range("b11")
![numero facture] = Mid(Worksheets("Feuil1").Range("a12"), 12)
rst1.Update
j = j + 1 'incrementation du compteur de doublons
End With
Exit Do
End If
Loop
If i < 1 Then ' cas de la premiere importation ou on ne peut pas avoir de doublons
excel.Application.Run ("'Facture vierge AGNEAUXP.xls'!extractagneaux")
Rst.Close
Set Rst = Nothing
End If
End With
Case "bovins"
Set Rst = bd.OpenRecordset("factures", dbOpenDynaset)
With Rst
Do While Not .EOF And i >= 1
texte = Mid(Cells(12, 1), 12, 35)
Rst.FindFirst "[numero facture] like'" & texte & "'"
If Rst.NoMatch Then
excel.Application.Run ("'Facture vierge bovinP.xls'!extractbovins")
Rst.Close
Set Rst = Nothing
Exit Do
Else
With rst1
rst1.AddNew
![type facture] = Worksheets("Feuil1").Range("I1")
![date facture] = Worksheets("Feuil1").Range("b11")
![numero facture] = Mid(Worksheets("Feuil1").Range("a12"), 12)
rst1.Update
j = j + 1
End With
Exit Do
End If
Loop
If i < 1 Then
excel.Application.Run ("'Facture vierge bovinP.xls'!extractbovins")
Rst.Close
Set Rst = Nothing
End If
End With
Case "porcs"
Set Rst = bd.OpenRecordset("factures", dbOpenDynaset)
With Rst
Do While Not .EOF And i >= 1
texte = Mid(Cells(12, 1), 12, 35)
Rst.FindFirst "[numero facture] like'" & texte & "'"
If Rst.NoMatch Then
excel.Application.Run ("'Facture vierge porcsP.xls'!extractporcs")
Rst.Close
Set Rst = Nothing
Exit Do
Else
With rst1
rst1.AddNew
![type facture] = Worksheets("Feuil1").Range("I1")
![date facture] = Worksheets("Feuil1").Range("b11")
![numero facture] = Mid(Worksheets("Feuil1").Range("a12"), 12)
rst1.Update
j = j + 1
End With
Exit Do
End If
Loop
If i < 1 Then
excel.Application.Run ("'Facture vierge porcsP.xls'!extractporcs")
Rst.Close
Set Rst = Nothing
End If
End With
Case "veaux"
Set Rst = bd.OpenRecordset("factures", dbOpenDynaset)
With Rst
Do While Not .EOF And i >= 1
texte = Mid(Cells(12, 1), 12, 35)
Rst.FindFirst "[numero facture] like'" & texte & "'"
If Rst.NoMatch Then
excel.Application.Run ("'Facture vierge veauxP.xls'!extractveaux")
Rst.Close
Set Rst = Nothing
Exit Do
Else
With rst1
rst1.AddNew
![type facture] = Worksheets("Feuil1").Range("I1")
![date facture] = Worksheets("Feuil1").Range("b11")
![numero facture] = Mid(Worksheets("Feuil1").Range("a12"), 12)
rst1.Update
j = j + 1
End With
Exit Do
End If
Loop
If i < 1 Then
excel.Application.Run ("'Facture vierge veauxP.xls'!extractveaux")
Rst.Close
Set Rst = Nothing
End If
End With
End Select
i = i + 1 'incrementation du compteur de factures extraites
excel.Application.DisplayAlerts = False 'permet de ne pas afficher de message d'alerte comme le verificateur de compatibilité
ActiveWorkbook.Close True 'fermeture de fichier precedent en sauvegardant les modifications
Set Workbook = Nothing
fichier = Dir
excel.Application.Wait (Now + TimeValue("0:00:03"))
Case non
ActiveWorkbook.Close False 'fermeture de fichiersans sauvegarder
Set Workbook = Nothing
fichier = Dir
End Select
Loop
rst1.Close 'fermeture du 2ieme recordset
Set rst1 = Nothing
MsgBox ("import des données terminées")
MsgBox ("le nombre de factures importées de: " & i - j)
If j <> 0 Then
MsgBox ("nombre de doublons touvées: " & j) 'affichage seulement en cas de doublons
End If
excel.Workbooks.Close
Set Workbook = Nothing
excel.Application.Quit
DoCmd.RunSQL "ALTER TABLE factures ADD CONSTRAINT PrimaryKey PRIMARY KEY ([numero facture])" 'on remet la clé primaire qui dira si on a oui ou non des doublons
Case vbNo 'si la reponse on est non alors on annule l'operation
DoCmd.CancelEvent
End Select
End Sub |
Partager