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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lop through controls

Status
Not open for further replies.

neemi

Programmer
May 14, 2002
519
GB
I am trying to loop through controls on a form to set labels to invisible where the Tag of the label is set to "

But the code does not pic up the labels:

what am i doing wrong?

also how do i tell it to just loop through the labels?

Code:
Dim cCont As Control
    For Each cCont In Me.Controls
        If cCont.Tag = "OrderPic" Then
            cCont.Visible = False
        End If
    Next cCont
 
I have checked the labels and the tag property has definately got "OrderPic" in it but it does not pic it up
 
mayhap?

Code:
For Each cCont In Me.Controls
    If (InStr(cCont.Tag, "OrderPic") <> 0 Then
        cCont.Visible = False
    End If
Next

MichaelRed


 
neemi,

I think MichaelRed's example looks doable, and if you also add-in the "Type of" filter as well, as SoCalAccessPro suggested - before the current filter, that should also speed the process up.

Something like this:
Code:
Private Sub Form_Load()
  Dim cCont As Control
  For Each cCont In Form.Controls
    If cCont.ControlType = acLabel Then
      If InStr(cCont.Tag, "OrderPic") Then
      Else
          cCont.Visible = False
      End If
    End If
  Next cCont
  Set cCont = Nothing
End Sub

Of course, I did vary a little from the other posts on how I put it together. Take a look, and let us know.

Also, I wasn't sure where you were putting the code, so I just guessed under Form_Load, but of course you'd put it wherever you actually are using it.

--

"If to err is human, then I must be some kind of human!" -Me
 
I've had this problem, 'For Each' doesn't pick up labels. Use 'While... Wend' instead:

Dim i As Integer
i = me.Controls.Count - 1
While i > 0
If me.Controls(i).Tag = "OrderPic" Then
me.Controls(i).Visible = False
End If
i = i - 1
Wend

Use control property ControlType = 100 to select just labels, smth like:
if Controls(i).ControlType = 100 Then 'This is label
 
Sorry, mistake in my previous post: instead of
While i > 0 should be While i >= 0
 
Thank you all for your help. It is very much apprecieated.
I ended up using the code as suggested by KJV1611 but everyones advice was taken into account and helped with the learning process.
thanks again.
Neemi
 
yurko,

The For Each Construct will work for ANY type of control, it is just another form of a loop, basically; same as While-Wend, Do-Loop, etc. If it didn't work for you, there was some other reason.

--

"If to err is human, then I must be some kind of human!" -Me
 
I used the for ... each loop and it worked fine.
Not sure what was wrong with my orginal code as in the 1st post but all is working perfectly now. Cheers for ur help.
 
kjv1611,
You are right, just checked and it does pick up labels. Probably it was something else.
 
How are ya neemi . . .
neemi said:
[blue]I have checked the labels and the tag property has definately got "OrderPic" in it but it does not pic it up . . .[/blue]
Be aware: when you prescribe a string such as ("OrderPic") for the [blue]Tag[/blue] property of a control, the delimiting double quotes ("...") are accepted as characters instead! Thats is to say if you look at the tag thru VBA you'll see [red]""OrderPic""[/red] not [blue]"OrderPic"[/blue]!

The key is to not enter quotations in the tag property. You should simply have [blue]OrderPic[/blue] (no quotations), which VBA will returns as [blue]"OrderPic"[/blue]. So just remove the quotations in the tag!

Example: I have a number of posts where I set the tag property to a question mark [purple]?[/purple], and in parenthese I always say ([blue]no quotations please[/blue]) . . .

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top