Private Sub Cbx_DateCommande_Change() ' Lors de la sélection change dans le combo
On Error GoTo ErrorHandler ' Gestion d'erreur
Dim trouve As Range
Dim ligne As Long
' Désactivation des événements pour éviter une boucle infinie
If Not EnableEvents Then Exit Sub
' Si aucune date n'est sélectionnée, effacer les données
If Me.Cbx_DateCommande = "" Then
Me.Range("B13:F34").ClearContents
Exit Sub
End If
' Vérification de la sélection valide de "Nom Prenom"
If Me.Cbx_NomPrenom.ListIndex = -1 Then
ViderBdC
Exit Sub
End If
' Recherche dans la feuille "Lst_Clients"
With Sheets("Lst_Clients").ListObjects("Lst_Tab_Clients")
' Cherche le "Nom Prénom" dans la colonne correspondante
Set trouve = .ListColumns("Nom Prénom").Range.Find(Me.Cbx_NomPrenom.Value, LookAt:=xlWhole, LookIn:=xlValues)
If Not trouve Is Nothing Then ' Si trouvé
ligne = trouve.Row - .Range.Row '+ 1 Calculer la ligne de la table
' Remplir les labels avec les valeurs de la table
Me.Lbl_NomPrenom = .ListColumns("Nom Prénom").DataBodyRange(ligne)
Me.Lbl_Téléphone = .ListColumns("Téléphone").DataBodyRange(ligne)
Me.Lbl_Mail = .ListColumns("Mail").DataBodyRange(ligne)
Me.Lbl_Adresse = .ListColumns("Adresse").DataBodyRange(ligne) & " " & .ListColumns("Code Postal").DataBodyRange(ligne) & " " & .ListColumns("Ville").DataBodyRange(ligne)
Me.Lbl_DateAnniverssaire = .ListColumns("Date Anniversaire").DataBodyRange(ligne)
End If
End With
' Recherche dans la feuille "Evt_Achat"
With Sheets("Evt_Achat").ListObjects("Lst_Tab_Achat")
' Cherche le "Date de Commande" dans la colonne correspondante
Set trouve = .ListColumns("Nom Prénom").Range.Find(Me.Cbx_NomPrenom.Value)
If Not trouve Is Nothing Then ' Si trouvé
ligne = trouve.Row - .Range.Row '+ 1 Calculer la ligne de la table
' Remplir les labels avec les valeurs de la table
Me.Lbl_DateCommande = Me.Cbx_DateCommande.Value
Me.Lbl_NumCommande = Format(.ListColumns("NumCommande").DataBodyRange(ligne), "0000")
Me.Chx_virement = .ListColumns("Virement").DataBodyRange(ligne)
Me.Chx_Chèque = .ListColumns("Chèque").DataBodyRange(ligne)
Me.Chx_Espèces = .ListColumns("Espèces").DataBodyRange(ligne)
Me.Chx_Paylib = .ListColumns("Paylib").DataBodyRange(ligne)
End If
End With
' Charger les informations du bon de commande
Call LoadBonDeCommande(Me.Cbx_NomPrenom, CDate(Me.Cbx_DateCommande))
Exit Sub ' Sortie normale du sub
ErrorHandler: ' Gestion des erreurs
MsgBox "Une erreur est survenue : " & Err.Description, vbCritical
End Sub