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
|
Sub importJRN()
'Déclaration des variables
Dim chemins() As Variant
Dim chemin As Variant
Dim tableCible As String
Dim sqlCommand As String
'Gestion des erreurs
'On Error GoTo gestionErreurs
'Choix du fichier source
chemins() = selectionFichiers("Z:\xx2022")
'Arret des messages system
DoCmd.SetWarnings False
'Boucle d'importation
For Each chemin In chemins()
'Vérification présence
If DCount("*", "tblSuiviMAJ", "NomFichier = " & Chr(34) & Dir(chemin) & Chr(34)) < 1 Then
'Nettoyage de la table temp avant import
sqlCommand = "DELETE FROM tblTempJRN"
DoCmd.RunSQL sqlCommand
Call TestAppli
'Import du fichier
DoCmd.TransferSpreadsheet acImport, , "tblTempJRN", chemin, 1
'Transfert de la table temp vers table finale
sqlCommand = "INSERT INTO tblJRN ( NomFichier, Fournisseur, [Type Prev], [Date MSG], [Référence], Libelle, [Date début], [Date fin], Qte, [Fixation = A], [Num Message], [Date déb sélection] ) " & _
"SELECT """ & Dir(chemin) & """, tblTempJRN.Fournisseur, tblTempJRN.[Type Prev], tblTempJRN.[Date MSG], tblTempJRN.[Référence], tblTempJRN.Libelle, tblTempJRN.[Date début], tblTempJRN.[Date fin], tblTempJRN.Qte, tblTempJRN.[Fixation = A], tblTempJRN.[Num Message], tblTempJRN.[Date déb sélection] " & _
"FROM tblTempJRN"
DoCmd.RunSQL sqlCommand
'Suivi des MAJ
sqlCommand = "INSERT INTO tblSuiviMAJ (TableCible, NomFichier, DateAjout, NbLignes) " _
& "VALUES (""tblJRN"", """ & Dir(chemin) & """, NOW(), Dcount(""*"", ""tblTempJRN""))"
DoCmd.RunSQL sqlCommand
End If
Next
'Fin et nettoyage
DoCmd.SetWarnings True
MsgBox "Importation Terminée"
Exit Sub
'Gestion des erreurs
gestionErreurs:
Debug.Print Err.Number
Debug.Print Err.Description
DoCmd.SetWarnings True
MsgBox "Erreur lors de l'importation"
End Sub |
Partager