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
| Public Function BM_Gestion_application_Import_Gesper()
'Import des données issues de Gesper (Matricule, nom, prenom, etc.)
'On Error GoTo Gestion_erreur
'PROCEDURE D'IMPORT DES DONNEES
Dim sSQL As String 'Commande SQL
Dim sFichierImport As String 'Nom complet du fichier Excel d'import
Dim lEnreg As Long 'Nombre d'enregistrements
Dim sMsg As String 'Message
Dim sTitre As String 'Titre
Dim lStyle As Long 'Paramètres de la boite de message
'Vérification du niveau de l'utilisateur
If VarAdmin = False Then
sMsg = "L'application est en mode utilisateur. " & vbNewLine & _
"Vous n'êtes pas habilité(e) à effectuer cette opération."
sTitre = "Opération non autorisée"
lStyle = vbCritical + vbOKOnly
MsgBox sMsg, lStyle, sTitre
Exit Function
End If
'DoCmd.SetWarnings False 'Supprimer les messages
'Localisation du fichier d'importation
sFichierImport = OuvrirUnFichier(0, "Localisation du fichier d'import (*.XLS)", 1, "Fichier Excel", "xls")
DoEvents
'Suppression de la table DATA si elle existe
If ExistenceTable("DATA") = True Then DoCmd.DeleteObject acTable, "DATA"
DoEvents
'Importer les données du fichier Excel dans la table temporaire DATA
DoCmd.TransferSpreadsheet acImport, 8, "DATA", sFichierImport, True, ""
DoEvents
'Suppression de la table temporaire UNITES si elle existe
If ExisteTable("UNITES") = True Then DoCmd.DeleteObject acTable, "UNITES"
DoEvents
'Création de la table temporaire UNITES
sSQL = "SELECT DATA.CAffectation AS CUnite, DATA.Affectation AS Unite, Date() AS MaJ INTO UNITES " & _
"FROM DATA " & _
"GROUP BY DATA.CAffectation, DATA.Affectation, Date() " & _
"HAVING (((DATA.CAffectation) Like '*00'));"
CurrentDb.Execute sSQL
DoEvents
'Mise à jour de la table tbl_UNITES avec la table temporaire UNITES
sSQL = "UPDATE tbl_UNITES RIGHT JOIN UNITES ON tbl_UNITES.CUnite_ID = UNITES.CUnite " & _
"SET tbl_UNITES.CUnite_ID = [UNITES]![CUnite], tbl_UNITES.Unite = [UNITES]![Unite], " & _
"tbl_UNITES.MaJ = [UNITES]![MaJ];"
CurrentDb.Execute sSQL
DoEvents
'Suppression de la table temporaire UNITES
DoCmd.DeleteObject acTable, "UNITES"
DoEvents
'Suppression de la table temporaire AFFECTATIONS si elle existe
If ExisteTable("AFFECTATIONS") = True Then DoCmd.DeleteObject acTable, "AFFECTATIONS"
DoEvents
'Création de la table temporaire AFFECTATIONS
sSQL = "SELECT DATA.CAffectation, DATA.Affectation, Left([CAffectation],6) & '00' AS CUnite, Date() AS MaJ INTO AFFECTATIONS " & _
"FROM DATA " & _
"GROUP BY DATA.CAffectation, DATA.Affectation, Left([CAffectation],6) & '00', Date() " & _
"ORDER BY DATA.CAffectation;"
CurrentDb.Execute sSQL
DoEvents
'Mise à jour de la table tbl_AFFECTATIONS avec la table temporaire AFFECTATIONS
sSQL = "UPDATE tbl_AFFECTATIONS RIGHT JOIN AFFECTATIONS ON tbl_AFFECTATIONS.CAffectation_ID = AFFECTATIONS.CAffectation " & _
"SET tbl_AFFECTATIONS.CAffectation_ID = [AFFECTATIONS]![CAffectation], " & _
"tbl_AFFECTATIONS.Affectation = [AFFECTATIONS]![Affectation], " & _
"tbl_AFFECTATIONS.CUnite = [AFFECTATIONS]![CUnite], tbl_AFFECTATIONS.MaJ = [AFFECTATIONS]![MaJ];"
CurrentDb.Execute sSQL
DoEvents
'Suppression de la table temporaire AFFECTATIONS
DoCmd.DeleteObject acTable, "AFFECTATIONS"
DoEvents
'Mise à jour de la table tbl_AGENTS avec la table temporaire DATA
sSQL = "UPDATE tbl_AGENTS RIGHT JOIN DATA ON tbl_AGENTS.Matricule_ID = DATA.Matricule " & _
"SET tbl_AGENTS.Matricule_ID = [DATA]![Matricule], tbl_AGENTS.Qualite = [DATA]![Qualite], " & _
"tbl_AGENTS.Nom = [DATA]![Nom], tbl_AGENTS.Prenom = [DATA]![Prenom], " & _
"tbl_AGENTS.Naissance = [DATA]![Naissance], tbl_AGENTS.CAffectation = [DATA]![CAffectation], " & _
"tbl_AGENTS.CGrade = [DATA]![CGrade], " & _
"tbl_AGENTS.Sortie = [DATA]![Sortie], tbl_AGENTS.MaJ = Date();"
CurrentDb.Execute sSQL
DoEvents
'Suppression de la table temporaire GRADES si elle existe
If ExisteTable("GRADES") = True Then DoCmd.DeleteObject acTable, "GRADES"
DoEvents
'Création de la table temporaire GRADES
sSQL = "SELECT DATA.CGrade, DATA.Grade, Date() AS MaJ INTO GRADES " & _
"FROM DATA " & _
"GROUP BY DATA.CGrade, DATA.Grade, Date();"
CurrentDb.Execute sSQL
DoEvents
'Mise à jour de la table tbl_GRADES avec la table temporaire GRADES
sSQL = "UPDATE tbl_GRADES RIGHT JOIN GRADES ON tbl_GRADES.CGrade_ID = GRADES.CGrade SET " & _
"tbl_GRADES.CGrade_ID = [GRADES]![CGrade], tbl_GRADES.Grade = [GRADES]![Grade], tbl_GRADES.MAJ = [GRADES]![MaJ];"
CurrentDb.Execute sSQL
DoEvents
'Suppression de la table temporaire GRADES
DoCmd.DeleteObject acTable, "GRADES"
DoEvents
'Décompte des enregistrements
lEnreg = DCount("*", "DATA")
DoEvents
'Information de la procédure
sMsg = "Mise à jour de la table des agents :" & vbNewLine _
& lEnreg & " enregistrements mis à jour." & vbNewLine & vbNewLine _
& "L'importation des données GESPER est terminée !"
MsgBox sMsg, vbInformation + vbOKOnly, "Import des données GESPER"
'Supprimer la table temporaire DATA et de la table des erreurs
DoCmd.DeleteObject acTable, "DATA"
If ExisteTable("Feuil1$_ImportErrors") = True Then DoCmd.DeleteObject acTable, "Feuil1$_ImportErrors"
DoCmd.SetWarnings True 'Afficher les messages
End Function |
Partager