Microsoft 365 Decimale dans Userform

eric72

XLDnaute Accro
Bonjour à tous ,
J'ai un petit souci avec une formule qui fonctionne dur un userform et pas sur celui-ci "UsfProduit"
je ne comprends pas bien pourquoi, il y a un bug, voilà le code (code de Patrick Toulon qui m'a beaucoup aidé pour un autre fichier) :

'forcer les textbox en numerique seulement
Function KeyAsciiX(keyascii)
'TRANSFORMER LE POINT PAR UNE VIRGULE
If keyascii = 46 Then keyascii = 44
If InStr("1234567890,-", Chr(keyascii)) = 0 Then keyascii = 0
With ActiveControl
If InStr(.Value, ",") Then keyascii = 0
If Chr(keyascii) = "-" And .Value <> "" Then keyascii = 0
End With
End Function

Private Sub PrixAchatPlante_KeyPress(ByVal keyascii As MSForms.ReturnInteger): KeyAsciiX keyascii: End Sub

et le bug est sur:
If InStr(.Value, ",")

Merci d'avance pour l'explication pour laquelle ça ne fonctionne pas sur CE Userfrom
Eric
 
Solution
Bonsoir eric
le problème vient de ce que ActiveControl ne représente Rien !
j'ai donc modifié pour passer en argument le Control concerné ;
VB:
'forcer  les textbox en numerique seulement
Function KeyAsciiX(ByRef Ctrl As Object, keyascii)'Ici'
'TRANSFORMER LE POINT PAR UNE VIRGULE
With Ctrl 'Ici'
    If keyascii = 46 Or keyascii = 44 Then keyascii = IIf(InStr(.Value, ",") > 0 Or Len(.Value) = 0, 0, 44)
    If InStr(1, "0123456789,", Chr(keyascii)) = 0 Then keyascii = 0
  End With
End Function
'ci dessous'
Private Sub PrixAchatPlante_KeyPress(ByVal keyascii As MSForms.ReturnInteger): KeyAsciiX PrixAchatPlante, keyascii: End Sub
à voir donc
jean marie

ChTi160

XLDnaute Barbatruc
Bonsoir eric
le problème vient de ce que ActiveControl ne représente Rien !
j'ai donc modifié pour passer en argument le Control concerné ;
VB:
'forcer  les textbox en numerique seulement
Function KeyAsciiX(ByRef Ctrl As Object, keyascii)'Ici'
'TRANSFORMER LE POINT PAR UNE VIRGULE
With Ctrl 'Ici'
    If keyascii = 46 Or keyascii = 44 Then keyascii = IIf(InStr(.Value, ",") > 0 Or Len(.Value) = 0, 0, 44)
    If InStr(1, "0123456789,", Chr(keyascii)) = 0 Then keyascii = 0
  End With
End Function
'ci dessous'
Private Sub PrixAchatPlante_KeyPress(ByVal keyascii As MSForms.ReturnInteger): KeyAsciiX PrixAchatPlante, keyascii: End Sub
à voir donc
jean marie
 

eric72

XLDnaute Accro
Bonsoir Chti160,
Merci beaucoup, en effet cela fonctionne nickel, mais je ne comprends toujours pas pourquoi ça marche sur un autre userform du même fichier, c'est un grand mystère pour moi!!! a quoi doit être rattaché ce Active Control?
Mais le principal c'est que cela fonctionne.
Merci beaucoup et Bonne soirée.
Eric
 

ChTi160

XLDnaute Barbatruc
Re
voilà ce que j'ai mis pour le Premier Control et dans la Function
VB:
'forcer  les textbox en numerique seulement
Public Function KeyAsciiX(ByRef Ctrl As Object, keyascii)
'TRANSFORMER LE POINT PAR UNE VIRGULE
With Ctrl 'UsfProduit.FrPlante.ActiveControl
'MsgBox .Name 'on peut avoir le Nom du Control passé
    If keyascii = 46 Or keyascii = 44 Then keyascii = IIf(InStr(.Value, ",") > 0 Or Len(.Value) = 0, 0, 44)
    If InStr(1, "0123456789,", Chr(keyascii)) = 0 Then keyascii = 0
  End With
End Function

Private Sub PrixAchatPlante_KeyPress(ByVal keyascii As MSForms.ReturnInteger): KeyAsciiX UsfProduit.FrPlante.ActiveControl, keyascii: End Sub
jean marie
 

patricktoulon

XLDnaute Barbatruc
bonsoir
tiens c'est ma fonction ça la keyasciiX
oui sauf que celui qui la copié ne la pas fait entièrement il l'a raccourci inutilement
et oui!!! quand un textbox est dans un frame
le activecontrol renvoie le parent donc la frame


là vous avez la version complète
VB:
'*************************************************************************************************
'forcer  les textbox en numerique seulement
Sub KeyAsciiX(keyascii)
'le vrai code ET  au complet !!!!!! de patricktoulon
    Dim ctrl As Object
    Set ctrl = ActiveControl: If TypeName(ctrl) = "Frame" Then Set ctrl = ActiveControl.ActiveControl
    With ctrl
        If keyascii = 46 Then keyascii = 44' point to virgule 
        If Chr(keyascii) Like "[!0-9|,-]" Then keyascii = 0' rien que les chiffres la virgule le tiret 
        If (Len(.Value) = 0 Or .Value Like "*,*") And Chr(keyascii) = "," Then keyascii = 0'la virgule q'une fois!! et pas au debut
        If Chr(keyascii) = "-" And .Value <> "" Then keyascii = 0'pour les nombres negatifs 
    End With
End Sub

Private Sub PrixAchatPlante_KeyPress(ByVal keyascii As MSForms.ReturnInteger): KeyAsciiX keyascii: End Sub
 

patricktoulon

XLDnaute Barbatruc
re
et à supposer que le textbox soit dans un frame1 qui elle même est dans un frame2 qui elle meme est dans un frame3 etc...etc... (sans limite )
celle ci trouve toujours son caller
VB:
'qu'est ce que l'on s'amuse ici
'*************************************************************************************************
'forcer  les textbox en numerique seulement
Sub KeyAsciiX(KeyAscii)
'le vrai code ET  au complet !!!!!! de patricktoulon
    Dim ctrl As Object
    Set ctrl = ActiveControl: Do While TypeName(ctrl) <> "TextBox": Set ctrl = ctrl.ActiveControl: Loop
    With ctrl
        If KeyAscii = 46 Then KeyAscii = 44
        If Chr(KeyAscii) Like "[!0-9|,-]" Then KeyAscii = 0
        If (Len(.Value) = 0 Or .Value Like "*,*") And Chr(KeyAscii) = "," Then KeyAscii = 0
        If Chr(KeyAscii) = "-" And .Value <> "" Then KeyAscii = 0
    End With
End Sub


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger): KeyAsciiX KeyAscii: End Sub
démo
demo.gif
 

Discussions similaires

Réponses
2
Affichages
240
Réponses
2
Affichages
152

Statistiques des forums

Discussions
312 216
Messages
2 086 351
Membres
103 195
dernier inscrit
martel.jg