OVH Cloud OVH Cloud

Code pour TextBox

41 réponses
Avatar
David
Bonjour/soir

Comment activer ou désactiver la touche Majuscule en VB ?

Je m'explique

A l'arrivée d'un TextBox dans un UserForm, j'aimerais qu'une fonction active
la touche majuscule et qu'à la sortie de ce textBox la touche majuscule soit
désactiver.

Deux autres questions

Comment obliger un utilisateur à rentrer dans un TextBox un format numérique
?

Comment obliger un utilisateur à rentrer dans un TextBox un montant en euros
(format 122,54 ?) en tapant juste 122.54 (point) ou 122,54 (virgule) ?

Un grand merci pour vos réponses et votre aides

Bonne soirée


David

10 réponses

1 2 3 4 5
Avatar
David
Re

Je comprends bien mais pour la mise en forme de mon projet j'aimerais que
cela s'inscrive dans le TextBox

Alors malgré
Private Sub TextBox10_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
TextBox10.Value = Format(TextBox10.Value, "#,##0.00 ?")
End Sub

cela ne me mets pas ce signe ?

merci de ton aide
Avatar
David
Test car je vois toujours pas mais zoli euros ¤
Avatar
Ilan
Bonjour,
Je n'autorise pas le caractere ;.

essaie si ca te convient

Dim Nombre As String

Private Sub TextBox1_AfterUpdate()
If IsNumeric(Nombre) Then
TextBox1.Value = Format(CDbl(Nombre), "#,##00.00 ?")
Else
TextBox1.Value = Format("0.00", "#,##00.00 ?")
End If
Nombre = ""
End Sub

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1.Value = Format(Nombre, "#,##00.00 ?")
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii <> 8 And KeyAscii <> 10 And KeyAscii <> 13 And KeyAscii <> 44
And _
KeyAscii <> 46 And KeyAscii <> 63 And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
Exit Sub
End If
If KeyAscii = 63 Then
If Right(TextBox1.Text, 1) <> Chr(63) And Len(TextBox1.Text) <> 0 Then
TextBox1.Text = TextBox1.Text + " ?"
KeyAscii = 0
End If
If KeyAscii = 46 Then KeyAscii = 44
If KeyAscii = 44 Then If InStr(1, TextBox1.Text, Chr(44)) > 0 Then KeyAscii
= 0
If KeyAscii <> 0 Then Nombre = Nombre & Chr(KeyAscii)
End Sub
Avatar
David
Re

Il manque un End If à la fin de
If KeyAscii = 46 Then KeyAscii = 44
If KeyAscii = 44 Then If InStr(1, TextBox8.Text, Chr(44)) > 0 Then KeyAscii
= 0
If KeyAscii <> 0 Then Nombre = Nombre & Chr(KeyAscii)
End If

Enfin bref cela me donne quand je tape 23,45 donne 00,00
Avatar
Ilan
Heu... Non il ne manque pas de End if
Essaie ca, la seule chose qui a ete modifiee c'est la premiere ligne
j'ai retire le caractere _

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii <> 8 And KeyAscii <> 10 And KeyAscii <> 13 And KeyAscii <> 44
And KeyAscii <> 46 And KeyAscii <> 63 And (KeyAscii < 48 Or KeyAscii > 57)
Then
KeyAscii = 0
Exit Sub
End If
If KeyAscii = 63 Then
If Right(TextBox1.Text, 1) <> Chr(63) And Len(TextBox1.Text) <> 0 Then
TextBox1.Text = TextBox1.Text + " ?"
KeyAscii = 0
End If
If KeyAscii = 46 Then KeyAscii = 44
If KeyAscii = 44 Then If InStr(1, TextBox1.Text, Chr(44)) > 0 Then KeyAscii
= 0
If KeyAscii <> 0 Then Nombre = Nombre & Chr(KeyAscii)
End Sub



Re

Il manque un End If à la fin de
If KeyAscii = 46 Then KeyAscii = 44
If KeyAscii = 44 Then If InStr(1, TextBox8.Text, Chr(44)) > 0 Then KeyAscii
= 0
If KeyAscii <> 0 Then Nombre = Nombre & Chr(KeyAscii)
End If

Enfin bref cela me donne quand je tape 23,45 donne 00,00





Avatar
Ilan
Salut pour info,
les tests IF THEN qui n'ont pas de ELSE sont ecrits sur une seule ligne.
Les lignes suivantes correspondent a une seule ligne dans le code.

If Right(TextBox1.Text, 1) <> Chr(63) And Len(TextBox1.Text) <> 0 Then
TextBox1.Text = TextBox1.Text + " ?"
Avatar
michdenis
Bonjour David,

La fonction a été modifié pour limiter la saisie à 2 décimales après le séparateur décimal. Ce dernier peut être la
virgule ou le point c'est selon la valeur définie dans le panneau de configuration de windows

'--------------------------
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1 = Format(Me.TextBox1, "# ##0.00 ?")
End Sub
'--------------------------
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

KeyAscii = CheckLaSaisie(TextBox1, KeyAscii)

End Sub
'--------------------------
Function CheckLaSaisie(Textbox As MSForms.Textbox, ByVal Char As Integer)

Dim SepDec As String, A As Integer
SepDec = Application.International(xlDecimalSeparator)

A = InStr(1, Textbox, SepDec, vbTextCompare)

If Char = 44 Or Char = 46 Then
If A > 0 Then
CheckLaSaisie = 0
Else
CheckLaSaisie = Asc(SepDec)
End If
Else
If Char < 48 Or Char > 58 Then
CheckLaSaisie = 0
Else
If Len(Textbox) - A = 2 And A <> 0 Then
CheckLaSaisie = 0
Else
CheckLaSaisie = Char
End If
End If
End If

End Function
'--------------------------

Salutations!





"David" <(Pas de Spam) a écrit dans le message de
news:%23$klm$
Bonsoir
Désolé pour le retard dans ma réponse du à des contrainte pro.

Question 1, pas de problème

Question 2 Un grand merci, cela fonctionne presque parfaitement !!!
Quand j'appui sur la touche "point" cela me mets une virgule (impec) et
quand j'appui sur la touche "virgule" cela n'inscrit rien. J'aimerais que
cela inscrive également une "virgule".
En clair, que l'on actionne "point" ou "virgule" cette action doit mettre
une "virgule".

Encore merci
Avatar
David
Re

Bon je m'en sort plus. Accepte tu un petit fichier avec le TextBox Date et
le TextBox du Montant ?

Merci de ton aide
Avatar
Frédéric Sigonneau
Bonjour,

Une autre possibilité pour afficher le sigle euro :

Private Sub TextBox10_BeforeUpdate _
(ByVal Cancel As MSForms.ReturnBoolean)
TextBox10.Value = Format(TextBox10.Value, "#,##0.00") & " " & Chr(128)
End Sub

FS
---
Frédéric Sigonneau [MVP Excel - né un sans-culottide]
Gestions de temps, VBA pour Excel :
http://frederic.sigonneau.free.fr
Si votre question sur Excel est urgente, évitez ma bal !

Re

Je comprends bien mais pour la mise en forme de mon projet j'aimerais que
cela s'inscrive dans le TextBox

Alors malgré
Private Sub TextBox10_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
TextBox10.Value = Format(TextBox10.Value, "#,##0.00 ?")
End Sub

cela ne me mets pas ce signe ?

merci de ton aide




Avatar
David
Bonjour

Également pour éviter la saisie de plus de 2 chiffres après la virgule

Merci de ton aide

David
1 2 3 4 5