Option Explicit

Private Sub btnfermer_Click()
    Unload Me
End Sub



Private Sub btnsauvegarde_Click()

    Dim tnumligne As Long
    Dim reponse As Integer
    
    reponse = MsgBox("Désirez-vous sauvegarder les modifications?", _
                vbQuestion + vbYesNo + vbDefaultButton1, "CONFIRMATION MODIFICATION")
                
    'tnumligne = Range("tnumligne")
    
    If reponse = vbYes Then
    
        ActiveSheet.Range("B6").Select
        Do Until ActiveCell = CStr(Me.txtnom) Or ActiveCell = ""
            ActiveCell.Offset(1, 0).Select 'On descend d'une ligne vers le bas
        Loop
    
        'Cells(tnumligne, 1) = Me.cbocivilite
        'Cells(tnumligne, 2) = Me.txtnom
        'Cells(tnumligne, 3) = Me.txtprenom
        ActiveCell.Offset(0, 2) = Me.txtdate
        ActiveCell.Offset(0, 3) = Me.cbostatus
        ActiveCell.Offset(0, 4) = CInt(Me.txtn_carte)
        ActiveCell.Offset(0, 5) = Me.txtadresse
        ActiveCell.Offset(0, 6) = Me.txtcp
        ActiveCell.Offset(0, 7) = Me.txtville
       
    ElseIf reponse = vbNo Then
        Exit Sub
        Unload Me
    End If
    
End Sub

Private Sub txtn_carte_Keypress(ByVal KeyAscii As MSForms.ReturnInteger)
    If InStr("1234567890", Chr(KeyAscii)) = 0 Then KeyAscii = 0: MsgBox "Saisie non valide", vbCritical
End Sub

Private Sub txtn_carte_Exit(ByVal Cancel As MSForms.ReturnBoolean)

        'On se positionne sur la feuille active et en cellule F6
    ActiveSheet.Range("F6").Select
    'On parcours toutes les cellules de la colonne F jusqu'à
    'soit la valeur trouvée ou la premère cellule vide
    Do Until ActiveCell = CInt(Me.txtn_carte) Or ActiveCell = ""
        ActiveCell.Offset(1, 0).Select 'On descend d'une ligne vers le bas
    Loop
    'On teste si la valeur a été trouvée.
    If ActiveCell = CInt(Me.txtn_carte.Value) Then
        Me.lblmessage_carte = "Le Numéro de l'Adhérent " & ActiveCell & " existe déjà dans la base."
        Me.txtn_carte = ""
        Me.txtn_carte.SetFocus
    Else
        Me.lblmessage_carte = ""
        Exit Sub
    End If
    
End Sub

Private Sub UserForm_Initialize()

    Dim tnumligne As Long
    
    tnumligne = Range("tnumligne")
    Me.lblmessage = "Vous pouvez désormais modifier les information de l'Adhérent."
    'MsgBox tnumligne
    
    Me.cbocivilite = Cells(tnumligne, 1)
    Me.txtnom = Cells(tnumligne, 2)
    Me.txtprenom = Cells(tnumligne, 3)
    Me.txtdate = Cells(tnumligne, 4)
    Me.cbostatus = Cells(tnumligne, 5)
    Me.txtn_carte = Cells(tnumligne, 6)
    Me.txtadresse = Cells(tnumligne, 7)
    Me.txtcp = Cells(tnumligne, 8)
    Me.txtville = Cells(tnumligne, 9)
    
    
End Sub

'Procédure permettent de mettre en MAJUSCULE
Private Sub Majuscule(ByRef moncontrol As Object)
    moncontrol.Value = UCase(moncontrol.Value)
    
        
End Sub

'Procédure permettent de mettre un contrôle tout en MAJUSCULE
    Private Sub txtnom_Change()
        Majuscule Me.txtnom
        
    End Sub

Private Sub txtcp_afterupdate()

txtcp.Value = Format(txtcp, "00000")

End Sub

Private Sub txtcp_Keypress(ByVal KeyAscii As MSForms.ReturnInteger)

If InStr("1234567890", Chr(KeyAscii)) = 0 Then KeyAscii = 0: MsgBox "Saisie non valide", vbCritical

End Sub



Private Sub txtprenom_Change()
    Dim st As String
    st = Me.txtprenom.Text
    Me.txtprenom.Text = UCase(Mid(st, 1, 1)) & Mid$(st, 2, Len(st))
    Me.txtprenom.SelStart = Len(st)

End Sub

Private Sub txtadresse_Change()
    Dim st As String
    st = Me.txtadresse.Text
    Me.txtadresse.Text = UCase(Mid(st, 1, 1)) & Mid$(st, 2, Len(st))
    Me.txtadresse.SelStart = Len(st)

End Sub
Private Sub txtville_Change()
    Dim st As String
    st = Me.txtville.Text
    Me.txtville.Text = UCase(Mid(st, 1, 1)) & Mid$(st, 2, Len(st))
    Me.txtville.SelStart = Len(st)

End Sub