Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through ONLY SOME controls on a form

Status
Not open for further replies.

aliendan

Programmer
Jun 28, 2000
79
US
I hope I can explain clearly enough. I want to loop through only some controls on a form. Using tab stop numbers for explanation purposes only, say I’m at stop 3 and it’s a combo box, if I change the value in that control I want to loop through all controls that come AFTER that (4-15) and clear them of any data while leaving stops 1 and 2 alone. Can this be done?

Dan Rogotzke
Daniel_Rogotzke@oxy.com
 
Put a tag in the controls. Maybe a "?"

dim ctrl as access.control
For each ctrl in me.controls
if ctrl.tag = "?" then do something
next ctrl
 
Yes, MajP, that works great but I failed to elaborate more as I should have. Here is my dilemma, this form has many combo boxes so say if I also wanted to do the same for stop 7 to clear stops 8-15 and leave 1-6 alone; also maybe do the same with stop 13 to change stops 14-15 and leave stops 1-12 alone. Does this make any sense? Now, if I could assign numbers to the control tag properties I could imbed it in a "For i = 8 to 15", for instance and it would work in my case but I find that numbers do not work with these properties, only string values. Do you have any other suggestions?

Dan Rogotzke
Daniel_Rogotzke@oxy.com
 
I'd use the TabIndex property of the controls.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, using the TabIndex property I get a Run-time error '438' "Object doesn't support this property or method". Any other ideas? Thanks.

Dan Rogotzke
Daniel_Rogotzke@oxy.com
 
How about using a numerical value in the tag?
Then, while using code such as that suggested by MajP, you can look for the value to be greater than the one you just updated.


Randy
 
If you still have the default names for your combo boxes, like Combo1, Combo2, Combo3, etc (not recomended, but I see a lot of people programming like that) you can do:

Code:
For i = 8 to 15
  Me.Controls("Combo" & i).Visible = False
Next i

Have fun.

---- Andy
 
For each ctrl in me.controls
select case ctrl.tag
case "Group1
'code to do stuff for the first group of controls
case "Group2"
'code to do stuff for the second group
case "GroupN"
'etc
end select
next ctrl
 
Andrzejek, I always rename my controls to represent what they contain. Such as cboFailLoc (Failure Location). I combined your suggestion with MajP’s using the Tag property with combo1, combo2, etc. I tested it using just a couple of controls and it looks like this:

For i = 1 To 3
For Each ctrl In Me.Controls
If ctrl.Tag = "combo" & i Then
ctrl = ""
End If
Next ctrl
Next i

IT WORKS GREAT! Thank you everyone for all your suggestions. It there is ever anything I can do for any of you, anytime, just let me know.

Dan


Dan Rogotzke
Daniel_Rogotzke@oxy.com
 
Two points to make:
1. Avoid using the default property of a control. Always [blue]specify[/blue] what you are changing. VBA allows you to get away with it, but good programming practice would be to be specific.
2. One you found your control, [red]get out[/red] of the loop * :)

Code:
For i = 1 To 3
  For Each ctrl In Me.Controls
    If ctrl.Tag = "combo" & i Then
      ctrl[blue].Text[/blue] = ""
      [red]Exit For[/red]
    End If
  Next ctrl
Next i

Just a suggestion...

* It is in the last place you look, so if you find it, don't look any more :)

Have fun.

---- Andy
 
Andrzejek, you are absolutely right. I will write that in immediately. Thank you. Looking back at my post my indentations didn't show (guess I didn't preview well enough, in a hurry). It should have looked like yours...heh-heh.

Dan

Dan Rogotzke
Daniel_Rogotzke@oxy.com
 
how about this

Function GetControlIndex()
Dim ctlindex As Integer
For ctlindex = 0 To Me.Controls.Count - 1
If Me.Controls(ctlindex).Name = Me.ActiveControl.Name Then
Exit For
End If
Next
GetControlIndex = ctlindex

End Function
Function PrintctlName(ctlindx)
For ctlindx = ctlindx To Me.Controls.Count - 1
Debug.Print Me.Controls(ctlindx).Name
Next ctlindx
Debug.Print "end"
End Function

Private Sub RunProgram_Enter()
GetControlIndex
PrintctlName (GetControlIndex)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top