Script moins long

Résolu
avyrex1926 Messages postés 360 Date d'inscription dimanche 3 décembre 2006 Statut Membre Dernière intervention 3 janvier 2012 - 9 août 2007 à 12:08
avyrex1926 Messages postés 360 Date d'inscription dimanche 3 décembre 2006 Statut Membre Dernière intervention 3 janvier 2012 - 10 août 2007 à 01:06
Bonjour à tous.

Je voudrais alléger se script:

TextBox1 = vbNullString
TextBox2 = vbNullString
TextBox3 = vbNullString
TextBox4 = vbNullString
TextBox5 = vbNullString
TextBox6 = vbNullString
TextBox7 = vbNullString
TextBox8 = vbNullString
TextBox9 = vbNullString
TextBox10 = vbNullString
TextBox11 = vbNullString
TextBox12 = vbNullString
TextBox13 = vbNullString
TextBox14 = vbNullString
TextBox15 = vbNullString
TextBox16 = vbNullString
TextBox17 = vbNullString
TextBox18 = vbNullString
TextBox19 = vbNullString
TextBox20 = vbNullString
TextBox21 = vbNullString
TextBox22 = vbNullString
TextBox23 = vbNullString
TextBox24 = vbNullString
TextBox25 = vbNullString
TextBox26 = vbNullString
TextBox27 = vbNullString
TextBox28 = vbNullString
TextBox29 = vbNullString
TextBox30 = vbNullString

Y'a t-il un moyen de faire la même chose mais avec moins de script?

Merci 

19 réponses

mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
9 août 2007 à 23:44
salut,

dim i as long

for i = 1 to 30
   Me.Controls("TextBox" & i).Text = vbNullString
next i

voilà, pas compliqué

@++

<hr width="100%" size="2" />( Nouveau forum : Exclusivement Office & VBA/STRONG>
3
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
9 août 2007 à 12:15
Il me semble pas que VBA gère les collections.

Mais tu peux faire :

Dim controle As Control
For each controle In me.Controls
    if (InStr(1, controle.name, "TextBox") > 0) Then
       control.Text = vbNullString
    end if
next controle
0
avyrex1926 Messages postés 360 Date d'inscription dimanche 3 décembre 2006 Statut Membre Dernière intervention 3 janvier 2012 3
9 août 2007 à 12:23
Merci [auteurdetail.aspx?ID=13557 DARKSIDIOUS]

Mais il ne veux pas le prendre. Il me donne une erreur sur :
control.Text = vbNullString
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
9 août 2007 à 12:26
oui forcément faute de frappe :

controle.Text

Quoique je suis pas sûr que ca passe mieux : controle étant de type Contrôle, je pense que VBA va te faire un beau petit type incompatible...

A ce moment là :
Dim controle As Control
Dim texte As TextBox
For each controle In me.Controls
    if (InStr(1, controle.name, "TextBox") > 0) Then
       texte = controle
       texte.Text = vbNullString
    end if
next controle
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
9 août 2007 à 12:27
quelle erreur ?

Renfield
Admin CodeS-SourceS- MVP Visual Basic
0
avyrex1926 Messages postés 360 Date d'inscription dimanche 3 décembre 2006 Statut Membre Dernière intervention 3 janvier 2012 3
9 août 2007 à 12:39
Re,

Il me fais toujours une erreur:  texte = controle
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
9 août 2007 à 13:23
pas de réponse ?

Renfield
Admin CodeS-SourceS- MVP Visual Basic
0
jrivet Messages postés 7392 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 6 avril 2012 60
9 août 2007 à 14:06
Salut,
je ne sais pas si cela peu répondre au problème mais il y a ce snippet présent sur Coddyx

'Dans un Module

Public Sub EffaceTextBox(ByRef UForm As UserForm)
    Dim Ctrl As Control
    'Boucle pour tout les contrôle de UForm
    For Each Ctrl In UForm.Controls
       If TypeOf Ctrl Is MSForms.TextBox Then Ctrl.Value = VbNullString
    Next
    Set Ctrl = Nothing
End Sub
<hr style="width: 100%; height: 2px;" />
'N'importe ou dans un USerForm
Call EffaceTextBox(Me)

@+: Ju£i?n
Pensez: Réponse acceptée
0
cs_makakdef Messages postés 50 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 31 août 2007
9 août 2007 à 14:23
Salut,
Je suis débutant mais ce code me parait cohérent ;-)

Public Sub deltxtbox
Dim i as integer
i = 1
    For i = 1 to 30
    TextBox(i) = vbNullString
    i = i + 1
    Next
End Sub

A+
/!/makakdef/!/
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
9 août 2007 à 14:25
oui, avec un groupe de controles, c'est tout a fait possible

Renfield
Admin CodeS-SourceS- MVP Visual Basic
0
jrivet Messages postés 7392 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 6 avril 2012 60
9 août 2007 à 14:31
Re,
Sauf qu'en VBA il n'y a pas de groupe de controle. :)

@+: Ju£i?n
Pensez: Réponse acceptée
0
cs_makakdef Messages postés 50 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 31 août 2007
9 août 2007 à 14:36
Mon code est-il bien, mauvais, utile, inutile? ><
C'est mon premier code xD
Je pense pas que ce soit si simple, sinon quelqu'un d'autre l'aurait posté avant moi mais bon...

Dis moi quand meme s'il fonctionne ou pas stp!

merci, A+
/!/makakdef/!/
0
jrivet Messages postés 7392 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 6 avril 2012 60
9 août 2007 à 14:45
Ton code fonctionne très bien en VB6 mais pas en VBA puisqu'en VBA il n'est pas possible de faire facielement des groupe de controle comme en VB6?

@+: Ju£i?n
Pensez: Réponse acceptée
0
cs_makakdef Messages postés 50 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 31 août 2007
9 août 2007 à 14:51
D'accord à partir de quoi peut on dire que c'est un groupe de controle?
Car j'ai juste utilisé un For... :-D

/!/makakdef/!/
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
9 août 2007 à 14:54
les controles sont indexés (tableau)

TextBox1( i ).Text = VbNullString

Renfield
Admin CodeS-SourceS- MVP Visual Basic
0
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
9 août 2007 à 14:54
Tu n'as pas du comprendre la boucle for :

Public Sub deltxtbox
Dim i as integer
i = 1
    For i = 1 to 30
    TextBox(i) = vbNullString
    i = i + 1 'PAS LA PEINE : la boucle for fait l'incrémentation automatiquement !
    Next
End Sub
0
cs_makakdef Messages postés 50 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 31 août 2007
9 août 2007 à 15:00
Ah que je suis nul des fois xD
merci les gens! enfait je savais qu'il n'y avait pas besoin du i=i+1, mais je l'ai mis...

Allez a+
/!/makakdef/!/

Software is like sex, it's better when it's free
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
9 août 2007 à 23:45
Et je confirme, PAS de groupe de contrôle en VBA, faut faire une classe (voir une de mes sources)

@++

<hr width="100%" size="2" />( Nouveau forum : Exclusivement Office & VBA/STRONG>
0
avyrex1926 Messages postés 360 Date d'inscription dimanche 3 décembre 2006 Statut Membre Dernière intervention 3 janvier 2012 3
10 août 2007 à 01:06
ah bien oui, ça fonctionne. Merci 
0
Rejoignez-nous