Bonsoir @Mimimathy, @kiss6,
@Mimimathy, voici le code et le fichier de là ou j’en suis.
Mais j’ai un problème pour la “Modification” d’une ligne choisie, en effet, quand je demande une modif, ça me modifie bien la Civilité et le Nom, mais aprés Prénom etc… ne veulent pas se modifier et je n’arrive pas a comprendre pourquoi ?
Peux-tu m’aider a comprendre pourquoi STP.
Il me restera ensuite a supprimer une ligne, et a mettre une photo.
Le code:
Option Explicit
'D?claration des variables pour tout le module
Dim FClients As Worksheet
Dim Dl%, Modif%
Dim Espace$
Dim Slash As Byte
Dim Civ As Range
Dim Ctrl As Control 'Variable pour effacer les ComboBox et TextBox
Private Sub Btn_Quitter_Click()
'Ferme l'USF
Unload Me
End Sub
Private Sub Cbox_Civilite_Change() 'S?lection civilit?
'?criture en nom propre
Cbox_Civilite = Application.Proper(Cbox_Civilite.Value)
End Sub
Private Sub CBox_NomComplet_Click()
'Remplir les TextBox avec Votre Choix
If (OptB_Modifier = True And Btn_Validation.Caption = "Modifier") _
Or (OptB_Supprimer = True And Btn_Validation.Caption = "Supprimer") Then
Modif = Range("A" & Rows.Count).End(xlUp).Row
Set FClients = Sheets("Clients")
Dl = Me.CBox_NomComplet.ListIndex + 3
Me.Cbox_Civilite = FClients.Cells(Dl, 1)
Me.TBox_Nom = FClients.Cells(Dl, 2)
Me.TBox_Prenom = FClients.Cells(Dl, 3)
Me.TBox_DNais = FClients.Cells(Dl, 4)
Me.TBox_Adresse = FClients.Cells(Dl, 5)
Me.TBox_CP = FClients.Cells(Dl, 6)
Me.TBox_Ville = FClients.Cells(Dl, 7)
Me.TBox_TelFixe = FClients.Cells(Dl, 8)
Me.TBox_TelPort = FClients.Cells(Dl, 9)
Me.TBox_Mail = FClients.Cells(Dl, 10)
'Me.CBox_NomComplet = ""
End If
End Sub
Private Sub OptB_Ajouter_Click()
'Clique Ajouter, alors vide les ComboBox et les TextBox
For Each Ctrl In Me.Controls
If TypeName(Ctrl) = "ComboBox" Then Ctrl.Text = ""
If TypeName(Ctrl) = "TextBox" Then Ctrl.Text = ""
Next Ctrl
CBox_NomComplet.Visible = False 'Masque ComboBox NomComplet
L_ChoixNomComplet.Visible = False 'Masque Label Votre Choix
'Clique Ajouter, alors change le nom du bouton
If Btn_Validation.Caption = "Modifier" Or Btn_Validation.Caption = "Supprimer" Then
Btn_Validation.Caption = "Ajouter" 'Valeur du bouton
Btn_Validation.BackColor = vbBlack 'Couleur du bouton
Btn_Validation.ForeColor = vbWhite 'Police du bouton
BackColor = vbBlack 'Couleur Cadre du formulaire
End If
End Sub
Private Sub OptB_Modifier_Click()
'Clique Modifier, alors vide les ComboBox et les TextBox
For Each Ctrl In Me.Controls
If TypeName(Ctrl) = "ComboBox" Then Ctrl.Text = ""
If TypeName(Ctrl) = "TextBox" Then Ctrl.Text = ""
Next Ctrl
CBox_NomComplet.Visible = True 'Affiche ComboBox NomComplet
L_ChoixNomComplet.Visible = True 'Affiche Label Votre Choix
'Clique sur Modifier alors RAZ Votre Choix
Me.CBox_NomComplet.Value = ""
'Clique Modifier, alors change le nom du bouton
If Btn_Validation.Caption = "Ajouter" Or Btn_Validation.Caption = "Supprimer" Then
Btn_Validation.Caption = "Modifier" 'Valeur du bouton
Btn_Validation.BackColor = vbBlue 'Couleur du bouton
Btn_Validation.ForeColor = vbWhite 'Police du bouton
BackColor = vbBlue 'Couleur Cadre du formulaire
End If
End Sub
Private Sub OptB_Supprimer_Click()
'Clique Supprimer, alors vide les ComboBox et les TextBox
For Each Ctrl In Me.Controls
If TypeName(Ctrl) = "ComboBox" Then Ctrl.Text = ""
If TypeName(Ctrl) = "TextBox" Then Ctrl.Text = ""
Next Ctrl
CBox_NomComplet.Visible = True 'Affiche ComboBox NomComplet
L_ChoixNomComplet.Visible = True 'Affiche Label Votre Choix
'Clique Modifier, alors change le nom du bouton
If Btn_Validation.Caption = "Ajouter" Or Btn_Validation.Caption = "Modifier" Then
Btn_Validation.Caption = "Supprimer" 'Valeur du bouton
Btn_Validation.BackColor = vbRed 'Couleur du bouton
Btn_Validation.ForeColor = vbWhite 'Police du bouton
BackColor = vbRed 'Couleur Cadre du formulaire
End If
End Sub
Private Sub TBox_Nom_Change() 'S?lection NOM
'?criture en majuscule
TBox_Nom.Value = UCase(TBox_Nom.Value)
End Sub
Private Sub TBox_Prenom_Change() 'S?lection Pr?nom
'?criture en nom propre
TBox_Prenom.Value = Application.Proper(TBox_Prenom.Value)
End Sub
Private Sub TBox_DNais_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 'S?lection N? le
'Uniquement des chiffres
If InStr("0123456789", Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub TBox_DNais_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Si la date n'existe pas ou si la date est sup a aujourd'hui
'alors on sort de la proc?dure
If Not IsDate(Me.TBox_DNais) Then
MsgBox "La date saisie n'est pas valide !", vbCritical, "Erreur de Saisie"
'Cancel = True
Me.TBox_DNais = ""
Else
If CDate(Me.TBox_DNais.Value) > Date Then
MsgBox "Date de naissance sup?rieur ? Aujourd'hui !", vbCritical, "Erreur de Saisie"
'Cancel = True
Me.TBox_DNais = ""
End If
End If
End Sub
Private Sub TBox_DNais_Change() 'S?lection N? le
'Mettre un Slash de s?paration style 01/01/2018
Slash = Len(TBox_DNais)
If Slash = 2 Or Slash = 5 Then TBox_DNais = TBox_DNais & "/"
End Sub
Private Sub TBox_Adresse_Change() 'S?lection adresse
'?criture en nom propre
TBox_Adresse.Value = Application.Proper(TBox_Adresse.Value)
End Sub
Private Sub TBox_CP_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 'S?lection code postale
'Uniquement des chiffres
If InStr("0123456789", Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub TBox_Ville_Change() 'S?lection Ville
'?criture en majuscule
TBox_Ville.Value = UCase(TBox_Ville.Value)
End Sub
Private Sub TBox_TelFixe_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 'S?lection t?l?phone fixe
'Uniquement des chiffres
If InStr("0123456789", Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub TBox_TelFixe_Change() 'Mettre un espace tout les 2 chiffres
Espace = TBox_TelFixe.Text
Select Case Len(Espace)
Case 2, 5, 8, 11
Espace = Espace & " "
End Select
TBox_TelFixe.Text = Espace
End Sub
Private Sub TBox_TelPort_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 'S?lection t?l?phone portable
'Uniquement des chiffres
If InStr("0123456789", Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub TBox_TelPort_Change() 'Mettre un espace tout les 2 chiffres
Espace = TBox_TelPort.Text
Select Case Len(Espace)
Case 2, 5, 8, 11
Espace = Espace & " "
End Select
TBox_TelPort.Text = Espace
End Sub
Private Sub TBox_Mail_Change() 'S?lection mail
'?criture en minuscule
TBox_Mail.Value = LCase(TBox_Mail.Value)
End Sub
Private Sub UserForm_Initialize()
OptB_Ajouter = True 'OptB_Ajouter = True ? l'ouverture de l'UserForm
Btn_Validation.BackColor = vbBlack 'Couleur du bouton
Btn_Validation.ForeColor = vbWhite 'Police du bouton
BackColor = vbBlack 'Couleur Cadre du formulaire
Me.CBox_NomComplet = ""
'Liste Nom Pr?nom
Me.CBox_NomComplet.ColumnCount = 3
Me.CBox_NomComplet.RowSource = "B3:C" & [C65000].End(xlUp).Row
'Liste sans doublons colonne A
For Each Civ In Worksheets("Clients") _
.Range("A3:A" & Range("A" & Rows.Count).End(xlUp).Row)
Me.Cbox_Civilite = Civ
If Me.Cbox_Civilite.ListIndex = -1 Then Me.Cbox_Civilite.AddItem Civ
Next Civ
Me.Cbox_Civilite.ListIndex = -1 'Pour mettre le Combo au d?but c?d vide
'Max 5 chiffres dans code postale
With TBox_CP
.MaxLength = 5
.Value = ""
End With
'Max 10 chiffres dans t?l?phone fixe + 4 espaces
With TBox_TelFixe
.MaxLength = 14
.Value = ""
End With
'Max 10 chiffres dans t?l?phone portable + 4 espaces
With TBox_TelPort
.MaxLength = 14
.Value = ""
End With
'Max 8 chiffres dans N? le + 2 Slashs
With TBox_DNais
.MaxLength = 10
.Value = ""
End With
End Sub
Private Sub Btn_Validation_Click() 'Appui sur le bouton Ajouter
'Message d'alerte Civilit?
If Me.Cbox_Civilite = "" Then
MsgBox "Vousn'avez pas renseign? la Civilit? !", vbInformation + vbOKOnly, "Erreur de saisie"
Me.Cbox_Civilite.SetFocus
Exit Sub
Else
'Message d'alerte Nom
If Me.TBox_Nom = "" Then
MsgBox "Vousn'avez pas renseign? le Nom !", vbInformation + vbOKOnly, "Erreur de saisie"
Me.TBox_Nom.SetFocus
Exit Sub
Else
'Message d'alerte Pr?nom
If Me.TBox_Prenom = "" Then
MsgBox "Vousn'avez pas renseign? le Pr?nom !", vbInformation + vbOKOnly, "Erreur de saisie"
Me.TBox_Prenom.SetFocus
Exit Sub
End If
End If
End If
With Feuil1 'Dans la feuille Clients
If Btn_Validation.Caption = "Ajouter" Then 'Si le bouton s'appel Ajouter alors
'Message avant validation d'un nouveau Client
If MsgBox("Confirmez-vous l'ajout de ce nouveau Client ?", vbYesNo, "Demande de confirmation d'ajout Client") = vbNo Then
Me.Cbox_Civilite.SetFocus
Exit Sub
Else
'?crire les donn?es dans la derni?re ligne non vide ? partir de la colonne A
Dl = Range("A" & Rows.Count).End(xlUp).Row + 1
.Cells(Dl, 1).Value = Me.Cbox_Civilite.Value
.Cells(Dl, 2).Value = Me.TBox_Nom.Value
.Cells(Dl, 3).Value = Me.TBox_Prenom.Value
.Cells(Dl, 4).Value = Me.TBox_DNais.Value
.Cells(Dl, 5).Value = Me.TBox_Adresse.Value
.Cells(Dl, 6).Value = Me.TBox_CP.Value
.Cells(Dl, 7).Value = Me.TBox_Ville.Value
.Cells(Dl, 8).Value = Me.TBox_TelFixe.Value
.Cells(Dl, 9).Value = Me.TBox_TelPort.Value
.Cells(Dl, 10).Value = Me.TBox_Mail.Value
End If
End If
If Btn_Validation.Caption = "Modifier" Then 'Si le bouton s'appel Modifier alors
Modif = Me.CBox_NomComplet.ListIndex + 3
.Cells(Modif, 1) = Me.Cbox_Civilite.Value
.Cells(Modif, 2) = Me.TBox_Nom.Value
.Cells(Modif, 3) = Me.TBox_Prenom.Value
.Cells(Modif, 4) = Me.TBox_DNais.Value
.Cells(Modif, 5) = Me.TBox_Adresse.Value
.Cells(Modif, 6) = Me.TBox_CP.Value
.Cells(Modif, 7) = Me.TBox_Ville.Value
.Cells(Modif, 8) = Me.TBox_TelFixe.Value
.Cells(Modif, 9) = Me.TBox_TelPort.Value
.Cells(Modif, 10) = Me.TBox_Mail.Value
End If
'Ferme l'USF
'Unload Me
'Ouvrir l'USF
'USFClients.Show 0
End With
End Sub
Le fichier: Exercice N° 3 Partie Ajouter Vy.xlsm (1,6 Mo)
D’avance merci de ton aide.