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
|
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 i As Integer
Dim j As Integer
Dim resultat As String
Dim Rst As Recordset
Dim rst1 As Recordset
'Dim cnt As Connection
'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"
'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 à editer
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
'MsgBox (fichier)
Excel.Workbooks.Open (repertoire & fichier) 'ouverture du fichier excel
Cells(2, 9).Value = "non"
Select Case animal
Case "bovins"
Set Rst = bd.OpenRecordset("factures", dbOpenDynaset)
With Rst
Do While Not .EOF
resultat = Rst.Fields("numero facture")
If resultat = Mid(Cells(12, 1), 12, 35) Then
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
Else
Rst.MoveNext
End If
Loop
If resultat <> Mid(Cells(12, 1), 12, 35) Then
Excel.Application.Run ("'Facture vierge bovinP.xls'!extractbovins")
Rst.Close
Set Rst = Nothing
Else
Rst.Close
Set Rst = Nothing
End If
End With
End Select
i = i + 1 'incrementation du compteur de factures extraites
ActiveWorkbook.Close False 'fermeture de fichier precedent en sauvegardant les modifications
fichier = Dir
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
Excel.Application.Quit
DoCmd.RunSQL "ALTER TABLE factures ADD CONSTRAINT PrimaryKey PRIMARY KEY ([numero facture])"
Case vbNo 'si la reponse on est non alors on annule l'operation
DoCmd.CancelEvent
End Select
End Sub |
Partager