Microsoft 365 problème dans une textbox dans userform

dubarre

XLDnaute Occasionnel
Bonjour à tous,

Je viens vers vous car j'ai un problème de code VBA dans textbox1.

Dans le userform3 quand je clique sur "optionbutton1" << numéro>> dans la textbox1, si je rentre un numéro qui n'est pas dans la base de données, il me met un message, ce que je souhaite.

Sur "optionbutton2" <<nom>> dans la textbox1, si je mets plus de 3 lettres, il me met le même message, ce qui n'est pas correct.

Est-il possible, dans le code suivant, de lui notifier que si ce sont des lettres, il ne doit pas réagir ?

VB:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    Dim t$, Lig&
    With TextBox1
        If Len(.Value) = 4 Then KeyAscii = 0: Exit Sub
        t = Mid(.Value & Chr(KeyAscii), 1, 5)
        If Len(t) = 4 Then
            With Application: Lig = .IfError(.Match(Val(t), Feuil2.[B:B], 0), 0): End With
            If Lig = 0 Then
                MsgBox "CE NUMERO N'EXISTE PAS", vbExclamation, "A CHANGER": t = ""
            End If
        End If
        .Value = t
        KeyAscii = 0
    End With
End Sub
 

Pièces jointes

  • XLD_Dubarre_Gestion_des_Artistes_vBéta10092020.xlsm
    807.5 KB · Affichages: 10

patricktoulon

XLDnaute Barbatruc
Bonjour
tout simplement
VB:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

    Dim t$, Lig&
    With TextBox1
       if not optionbutton2 then
If Len(.Value) = 4 Then KeyAscii = 0: Exit Sub
        t = Mid(.Value & Chr(KeyAscii), 1, 5)
        If Len(t) = 4 Then
            With Application: Lig = .IfError(.Match(Val(t), Feuil2.[B:B], 0), 0): End With
            If Lig = 0 Then
                MsgBox "CE NUMERO N'EXISTE PAS", vbExclamation, "A CHANGER": t = ""
            End If
        End If
        .Value = t
        KeyAscii = 0
    end if
End With
End Sub
 
Dernière édition:

Discussions similaires