Efficiënter maken van loop

Status
Niet open voor verdere reacties.

The Mad Man

Gebruiker
Lid geworden
15 aug 2007
Berichten
16
Ik heb hier de volgende code:

Code:
If chk_Check1.Value = True Then
    Selection.TypeText Text:=(chk_Check1.ControlTipText & vbNewLine)
    
    If chk1a.Value = True Then
        mroTemplate.AutoTextEntries("chk_Klant1").Insert Where _
        :=Selection.Range, RichText:=True
    End If
    If chk1b.Value = True Then
        mroTemplate.AutoTextEntries("chk_Klant2").Insert Where _
        :=Selection.Range, RichText:=True
    End If
    If chk1c.Value = True Then
        mroTemplate.AutoTextEntries("chk_Beide").Insert Where _
        :=Selection.Range, RichText:=True
    End If
End If
If chk_Check2.Value = True Then
    Selection.TypeText Text:=(chk_Check2.ControlTipText & vbNewLine)

    If chk2a.Value = True Then
        mroTemplate.AutoTextEntries("chk_Klant1").Insert Where _
        :=Selection.Range, RichText:=True
    End If
    If chk2b.Value = True Then
        mroTemplate.AutoTextEntries("chk_Klant2").Insert Where _
        :=Selection.Range, RichText:=True
    End If
    If chk2c.Value = True Then
        mroTemplate.AutoTextEntries("chk_Beide").Insert Where _
        :=Selection.Range, RichText:=True
    End If
End If

Op deze manier gaat het nog een aantal keer door.

Omdat ik de handel wat efficiënter wil maken, wil ik hier een loop van maken.
Kan iemand laten zien hoe dat in dit geval moet? Ik kan wel een while- of een for loop maken, maar hier lukt het niet omdat er ook nog tekst bij staat.
 
je moet in de loop de naam van de variabele opbouwen, zoiets als:

(deze code klopt niet helemaal, maar als je het principe snapt dan kom je er verder wel uit denk ik)

Code:
dim chkcontrolname1 as string
dim chkautotextname as string
dim i as integer
dim chkControl as checkbox

for i=1 to 3
   chkcontrolname1 = "chk" & i & "a"
   chkautotextname = chk_Klant" & i

   set chkControl = me.controls(chkcontrolname1)
   If chkControl.Value = True Then
        mroTemplate.AutoTextEntries(chkautotextname).Insert Where _
        :=Selection.Range, RichText:=True
    End If
next
 
je moet in de loop de naam van de variabele opbouwen, zoiets als:

(deze code klopt niet helemaal, maar als je het principe snapt dan kom je er verder wel uit denk ik)

Code:
dim chkcontrolname1 as string
dim chkautotextname as string
dim i as integer
dim chkControl as checkbox

for i=1 to 3
   chkcontrolname1 = "chk" & i & "a"
   chkautotextname = chk_Klant" & i

   set chkControl = me.controls(chkcontrolname1)
   If chkControl.Value = True Then
        mroTemplate.AutoTextEntries(chkautotextname).Insert Where _
        :=Selection.Range, RichText:=True
    End If
next

Bedankt, het is gelukt!
 
je moet in de loop de naam van de variabele opbouwen, zoiets als:

(deze code klopt niet helemaal, maar als je het principe snapt dan kom je er verder wel uit denk ik)

Code:
dim chkcontrolname1 as string
dim chkautotextname as string
dim i as integer
dim chkControl as checkbox

for i=1 to 3
   chkcontrolname1 = "chk" & i & "a"
   chkautotextname = chk_Klant" & i

   set chkControl = me.controls(chkcontrolname1)
   If chkControl.Value = True Then
        mroTemplate.AutoTextEntries(chkautotextname).Insert Where _
        :=Selection.Range, RichText:=True
    End If
next

Bedankt, het is gelukt! :thumb:
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan