Option Explicit
'=========================================================
' VARIABLE QUI CONTIENT LE CHEMIN DE LA PHOTO
'=========================================================
Private CheminPhoto As String
'=========================================================
' INITIALISATION DU USERFORM
'=========================================================
Private Sub UserForm_Initialize()
On Error GoTo GestionErreur
'---------------------------------------------
' Ouvrir la connexion Access
'---------------------------------------------
If Conn Is Nothing Then
OuvrirConnexionAccess
ElseIf [Link] = 0 Then
OuvrirConnexionAccess
End If
'---------------------------------------------
' Charger les facultés depuis Access
'---------------------------------------------
ChargerFacultes
'---------------------------------------------
' Charger les années
'---------------------------------------------
ComboAnné[Link]
ComboAnné[Link] "24"
ComboAnné[Link] "25"
ComboAnné[Link] "26"
ComboAnné[Link] "27"
ComboAnné[Link] "28"
'---------------------------------------------
' Charger les sexes
'---------------------------------------------
[Link]
[Link] "Masculin"
[Link] "Féminin"
'---------------------------------------------
' Date et heure d'inscription
'---------------------------------------------
[Link] = _
Format(Now, "dd/mm/yyyy hh:nn")
'---------------------------------------------
' Initialiser le chemin photo
'---------------------------------------------
CheminPhoto = ""
Exit Sub
GestionErreur:
MsgBox "Erreur lors de l'ouverture du formulaire :" & _
vbCrLf & [Link] & _
vbCrLf & [Link], _
vbCritical, "UPCC"
End Sub
'=========================================================
' CHARGER LES FACULTÉS DEPUIS ACCESS
'=========================================================
Private Sub ChargerFacultes()
On Error GoTo GestionErreur
Dim rs As [Link]
Dim SQL As String
ComboFaculté.Clear
'---------------------------------------------
' Vérifier la connexion
'---------------------------------------------
If Conn Is Nothing Then
OuvrirConnexionAccess
ElseIf [Link] = 0 Then
OuvrirConnexionAccess
End If
'---------------------------------------------
' Requête
'---------------------------------------------
SQL = "SELECT [Nom_Faculté] " & _
"FROM [Faculté] " & _
"ORDER BY [Nom_Faculté]"
Set rs = New [Link]
[Link] SQL, Conn, _
adOpenForwardOnly, _
adLockReadOnly
'---------------------------------------------
' Remplir ComboFaculté
'---------------------------------------------
Do While Not [Link]
ComboFaculté.AddItem rs![Nom_Faculté]
[Link]
Loop
[Link]
Set rs = Nothing
Exit Sub
GestionErreur:
MsgBox "Impossible de charger les facultés :" & _
vbCrLf & [Link] & _
vbCrLf & [Link], _
vbCritical, "UPCC"
End Sub
'=========================================================
' GÉNÉRER AUTOMATIQUEMENT LE MATRICULE
'=========================================================
Private Function GenererNumeroMatricule( _
ByVal NomFaculte As String, _
ByVal AnneeAcademique As String) As String
On Error GoTo GestionErreur
Dim rs As [Link]
Dim SQL As String
Dim CodeFac As String
Dim DernierMatricule As String
Dim Numero As Long
GenererNumeroMatricule = ""
'---------------------------------------------
' Vérifier les valeurs
'---------------------------------------------
If Trim(NomFaculte) = "" Then Exit Function
If Trim(AnneeAcademique) = "" Then Exit Function
'---------------------------------------------
' Ouvrir Access
'---------------------------------------------
If Conn Is Nothing Then
OuvrirConnexionAccess
ElseIf [Link] = 0 Then
OuvrirConnexionAccess
End If
'=================================================
' 1. RÉCUPÉRER LE CODE DE LA FACULTÉ
'=================================================
SQL = "SELECT [Code_Faculté] " & _
"FROM [Faculté] " & _
"WHERE [Nom_Faculté]='" & _
Replace(NomFaculte, "'", "''") & "'"
Set rs = New [Link]
[Link] SQL, Conn, _
adOpenForwardOnly, _
adLockReadOnly
If [Link] Then
MsgBox "La faculté sélectionnée n'existe pas dans Access.", _
vbExclamation, "UPCC"
[Link]
Set rs = Nothing
Exit Function
End If
CodeFac = Trim(rs![Code_Faculté])
[Link]
Set rs = Nothing
'=================================================
' 2. CHERCHER LE DERNIER MATRICULE
'=================================================
SQL = "SELECT TOP 1 [Matricule] " & _
"FROM [Etudiants] " & _
"WHERE [Matricule] LIKE '" & _
Replace(AnneeAcademique, "'", "''") & "-" & _
Replace(CodeFac, "'", "''") & "-%' " & _
"ORDER BY [ID_ETUDIANT] DESC"
Set rs = New [Link]
[Link] SQL, Conn, _
adOpenForwardOnly, _
adLockReadOnly
'=================================================
' 3. SI AUCUN ÉTUDIANT N'EXISTE
'=================================================
If [Link] Then
Numero = 1
Else
'---------------------------------------------
' Récupérer le dernier matricule
'---------------------------------------------
DernierMatricule = Trim(rs!Matricule)
'---------------------------------------------
' Extraire le numéro
' Exemple : 26-INF-005
'---------------------------------------------
If IsNumeric(Right(DernierMatricule, 3)) Then
Numero = _
CLng(Right(DernierMatricule, 3)) + 1
Else
Numero = 1
End If
End If
[Link]
Set rs = Nothing
'=================================================
' 4. CRÉER LE NOUVEAU MATRICULE
'=================================================
GenererNumeroMatricule = _
AnneeAcademique & "-" & _
CodeFac & "-" & _
Format(Numero, "000")
Exit Function
GestionErreur:
If Not rs Is Nothing Then
If [Link] = adStateOpen Then
[Link]
End If
End If
Set rs = Nothing
MsgBox "Erreur lors de la génération du matricule :" & _
vbCrLf & [Link] & _
vbCrLf & [Link], _
vbCritical, "UPCC"
GenererNumeroMatricule = ""
End Function
'=========================================================
' CHANGEMENT DE FACULTÉ
'=========================================================
Private Sub ComboFaculté_Change()
On Error GoTo GestionErreur
If Trim(ComboFaculté.Value) = "" Then
[Link] = ""
Exit Sub
End If
If Trim(ComboAnné[Link]) = "" Then
[Link] = ""
Exit Sub
End If
'---------------------------------------------
' Générer le matricule
'---------------------------------------------
[Link] = _
GenererNumeroMatricule( _
ComboFaculté.Value, _
ComboAnné[Link])
Exit Sub
GestionErreur:
MsgBox "Erreur lors du changement de faculté :" & _
vbCrLf & [Link], _
vbCritical, "UPCC"
End Sub
'=========================================================
' CHANGEMENT D'ANNÉE
'=========================================================
Private Sub ComboAnnée_Change()
On Error GoTo GestionErreur
If Trim(ComboFaculté.Value) = "" Then
[Link] = ""
Exit Sub
End If
If Trim(ComboAnné[Link]) = "" Then
[Link] = ""
Exit Sub
End If
'---------------------------------------------
' Régénérer le matricule
'---------------------------------------------
[Link] = _
GenererNumeroMatricule( _
ComboFaculté.Value, _
ComboAnné[Link])
Exit Sub
GestionErreur:
MsgBox "Erreur lors du changement d'année :" & _
vbCrLf & [Link], _
vbCritical, "UPCC"
End Sub
'=========================================================
' BOUTON CHOISIR LA PHOTO
'=========================================================
Private Sub CmdPhoto_Click()
On Error GoTo GestionErreur
Dim fd As FileDialog
Set fd = [Link]( _
msoFileDialogFilePicker)
With fd
.Title = "Sélectionner la photo de l'étudiant"
.AllowMultiSelect = False
.[Link]
.[Link] _
"Images", _
"*.jpg;*.jpeg;*.png;*.bmp"
If .Show = -1 Then
'-----------------------------------------
' Récupérer le chemin de la photo
'-----------------------------------------
CheminPhoto = .SelectedItems(1)
'-----------------------------------------
' Afficher la photo
'-----------------------------------------
[Link] = _
LoadPicture(CheminPhoto)
End If
End With
Set fd = Nothing
Exit Sub
GestionErreur:
MsgBox "Impossible de charger la photo :" & _
vbCrLf & [Link], _
vbCritical, "UPCC"
End Sub
'=========================================================
' VÉRIFIER SI LE MATRICULE EXISTE DÉJÀ
'=========================================================
Private Function MatriculeExiste( _
ByVal Matricule As String) As Boolean
On Error GoTo GestionErreur
Dim rs As [Link]
Dim SQL As String
MatriculeExiste = False
SQL = "SELECT [ID_ETUDIANT] " & _
"FROM [Etudiants] " & _
"WHERE [Matricule]='" & _
Replace(Matricule, "'", "''") & "'"
Set rs = New [Link]
[Link] SQL, Conn, _
adOpenForwardOnly, _
adLockReadOnly
If Not [Link] Then
MatriculeExiste = True
End If
[Link]
Set rs = Nothing
Exit Function
GestionErreur:
MatriculeExiste = False
End Function
'=========================================================
' BOUTON ENREGISTRER
'=========================================================
Private Sub CmdEnregistrer_Click()
On Error GoTo GestionErreur
Dim cmd As [Link]
'=================================================
' 1. VÉRIFIER LA CONNEXION
'=================================================
If Conn Is Nothing Then
OuvrirConnexionAccess
ElseIf [Link] = 0 Then
OuvrirConnexionAccess
End If
'=================================================
' 2. VÉRIFIER LES CHAMPS OBLIGATOIRES
'=================================================
If Trim([Link]) = "" Then
MsgBox "Le matricule est vide.", _
vbExclamation, "UPCC"
Exit Sub
End If
If Trim([Link]) = "" Then
MsgBox "Veuillez saisir le nom de l'étudiant.", _
vbExclamation, "UPCC"
[Link]
Exit Sub
End If
If Trim([Link]) = "" Then
MsgBox "Veuillez sélectionner le sexe.", _
vbExclamation, "UPCC"
[Link]
Exit Sub
End If
If Trim(ComboFaculté.Value) = "" Then
MsgBox "Veuillez sélectionner la faculté.", _
vbExclamation, "UPCC"
ComboFaculté.SetFocus
Exit Sub
End If
If Trim(ComboAnné[Link]) = "" Then
MsgBox "Veuillez sélectionner l'année académique.", _
vbExclamation, "UPCC"
ComboAnné[Link]
Exit Sub
End If
'=================================================
' 3. VÉRIFIER LE MATRICULE
'=================================================
If MatriculeExiste([Link]) Then
MsgBox "Ce matricule existe déjà.", _
vbExclamation, "UPCC"
Exit Sub
End If
'=================================================
' 4. PRÉPARER LA COMMANDE ACCESS
'=================================================
Set cmd = New [Link]
With cmd
.ActiveConnection = Conn
.CommandType = adCmdText
.CommandText = _
"INSERT INTO [Etudiants] " & _
"([Matricule],[Nom],[Sexe],[LD_Naissance]," & _
"[Promotion],[Faculté],[Année_Academique]," & _
"[Date inscription],[Telephone],[Adresse],[Photo]) " & _
"VALUES (?,?,?,?,?,?,?,?,?,?,?)"
'-----------------------------------------
' MATRICULE
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p1", _
adVarChar, _
adParamInput, _
50, _
[Link])
'-----------------------------------------
' NOM
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p2", _
adVarChar, _
adParamInput, _
100, _
[Link])
'-----------------------------------------
' SEXE
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p3", _
adVarChar, _
adParamInput, _
20, _
[Link])
'-----------------------------------------
' LIEU DE NAISSANCE
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p4", _
adVarChar, _
adParamInput, _
100, _
[Link])
'-----------------------------------------
' PROMOTION
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p5", _
adVarChar, _
adParamInput, _
50, _
[Link])
'-----------------------------------------
' FACULTÉ
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p6", _
adVarChar, _
adParamInput, _
150, _
ComboFaculté.Value)
'-----------------------------------------
' ANNÉE ACADÉMIQUE
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p7", _
adVarChar, _
adParamInput, _
50, _
ComboAnné[Link])
'-----------------------------------------
' DATE D'INSCRIPTION
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p8", _
adDate, _
adParamInput, , _
Now)
'-----------------------------------------
' TÉLÉPHONE
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p9", _
adVarChar, _
adParamInput, _
30, _
[Link])
'-----------------------------------------
' ADRESSE
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p10", _
adVarChar, _
adParamInput, _
255, _
[Link])
'-----------------------------------------
' PHOTO
'-----------------------------------------
.[Link] _
.CreateParameter( _
"p11", _
adVarChar, _
adParamInput, _
255, _
CheminPhoto)
'-----------------------------------------
' EXÉCUTER L'ENREGISTREMENT
'-----------------------------------------
.Execute
End With
Set cmd = Nothing
'=================================================
' 5. MESSAGE DE CONFIRMATION
'=================================================
MsgBox "Étudiant enregistré avec succès !" & _
vbCrLf & _
"Matricule : " & [Link], _
vbInformation, "UPCC"
'=================================================
' 6. VIDER LE FORMULAIRE
'=================================================
ViderFormulaire
Exit Sub
GestionErreur:
MsgBox "Erreur lors de l'enregistrement :" & _
vbCrLf & _
"Numéro : " & [Link] & _
vbCrLf & _
[Link], _
vbCritical, "UPCC"
Set cmd = Nothing
End Sub
'=========================================================
' VIDER LE FORMULAIRE
'=========================================================
Private Sub ViderFormulaire()
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
ComboFaculté.Value = ""
ComboAnné[Link] = ""
[Link] = ""
[Link] = ""
'---------------------------------------------
' Effacer la photo
'---------------------------------------------
CheminPhoto = ""
Set [Link] = Nothing
'---------------------------------------------
' Nouvelle date
'---------------------------------------------
[Link] = _
Format(Now, "dd/mm/yyyy hh:nn")
[Link]
End Sub
'=========================================================
' BOUTON NOUVEAU
'=========================================================
Private Sub CmdNouveau_Click()
ViderFormulaire
End Sub