for each control iteration in form design mode

  • Thread starter Thread starter rmanchu
  • Start date Start date
R

rmanchu

the following code does NOT iterate over all the textbox controls on
the form.
is it because i'm modifying the control within the loop? what's the
solution?

public inttmp as integer
public strtmp as string
public ctltmp as control

Public Sub doFormDesignFormat()

Const frmName = "RecievedDocument"
Const frmPrefix = "RD_"
Dim ctl As Access.Control

DoCmd.OpenForm frmName, acDesign
For Each ctlTmp In Forms(frmName).Controls
If ctlTmp.ControlType = Access.acTextBox _
Or ctlTmp.ControlType = Access.acComboBox _
Or ctlTmp.ControlType = Access.acCheckBox Then
strTmp = ctlTmp.ControlSource
intTmp = InStr(1, strTmp, "_")
If intTmp > 0 Then strTmp = Mid(strTmp, intTmp + 1)
strTmp = frmPrefix & strTmp
ctlTmp.Name = strTmp
ctlTmp.ControlSource = strTmp
Debug.Print "CN:" & ctlTmp.Name & " ,CS:" &
ctlTmp.ControlSource
If ctlTmp.Controls.Count > 0 Then
ctlTmp.Controls(0).Name = strTmp & "_Label"
Debug.Print " CLN:" & ctlTmp.Controls(0).Name
End If
ElseIf ctlTmp.ControlType = Access.acOptionGroup Then
ctlTmp.TabStop = False
intTmp = 1
strTmp = ctlTmp.Name
ctlTmp.Controls(0).Name = strTmp & "_Label"
If ctlTmp.Name = "ogFilterType" Then
ctlTmp.Controls(0).Caption = "Record &Status:"
End If
If ctlTmp.Name = "ogFilterBy" Then
ctlTmp.Controls(0).Caption = "Filter &By:"
End If
Debug.Print "OG:" & ctlTmp.Name
For lngTmp = 1 To ctlTmp.Controls.Count - 1
With ctlTmp.Controls(lngTmp)
If .ControlType = Access.acOptionButton Then
.Name = strTmp & "_Option" & intTmp
.Controls(0).Name = .Name & "_Label"
intTmp = intTmp + 1
Debug.Print " OBN:" & .Name & ", OBLN:" &
..Controls(0).Name
End If
End With
Next lngTmp
End If
Next ctlTmp
DoCmd.Close acForm, frmName, acSaveYes


End Sub
 
I was having problems looping through controls with a for/each also. I don't
know if this is the best way, but to get around it I first set up an array
with the control names, then looped through the array. Maybe you can use a
similar approach.

Dim aName() As String
n = CurCtrl.Controls.Count
ReDim aName(n)
For i = 0 To n - 1
aName(i) = CurCtrl.Controls(i).Name
Next

For i = 0 To n - 1
L = CurCtrl.Controls(aName(i)).Left
T = CurCtrl.Controls(aName(i)).Top
iNewLeft = L + DL
iNewTop = T + DT
CurCtrl.Controls(aName(i)).Left = iNewLeft
CurCtrl.Controls(aName(i)).Top = iNewTop
Next
 
yes i tried debug (maybe not in depth). the problem is that it doesn't
crash and the For Each returns some controls double and some none.

in so far as i know, the For Each should NOT be affected by code that
does not DELETE or INSERT into the collection being iterated. so i gave
up on that.

my solution to it was to create a VBA.Collection of required controls
and then changed properties. somehow it seems to work! but i still
can't figure it out.
 
dchman said:
I was having problems looping through controls with a for/each also. I don't
know if this is the best way, but to get around it I first set up an array
with the control names, then looped through the array. Maybe you can use a
similar approach.

i solved it by creating a new collection.

the properties that i modified are .Name and .ControlSource

as far as i can tell, modifying (one of) these properties some how (i
imagine) re-orders the collection in the For Each iteration. if so,
then, this is a SERIOUS BUG.

a quick search, but i couldn't find any posts or info regarding this
issue.

riyaz
 
Back
Top