Solved

VB Containers

Posted on 1998-11-25
3
462 Views
Last Modified: 2013-12-26
Is it possible to cycle through the controls in a container (frame or picture box) on a given form?  I know that the following code cycles through all controls on that form... but does not limit itself to the controls in that container.
Overview:  Place a picture box on the form and place a few text boxes in it.  As well as that, place a few text boxes just on the form.  Add a button and in the click event place the following code -

call clear_boxes(picture1)

Then add this code:

Public Sub clear_boxes(box As Object)
  Dim myobj As Object
  For Each myobj In box.Container
'..I know this gives the form, and not the container but???
    If TypeOf myobj Is TextBox Then
      myobj.Text = ""
    End If
  Next myobj
End Sub

0
Comment
Question by:tommy_boy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:vbWhiz
ID: 1488528
It's not exactly what you were looking for but you can check if the current control's (myobj) container is the box.

Your Modified Code:
Public Sub clear_boxes(box As Object)

  Dim myobj As Object
  For Each myobj In box.Parent
'now this actually gives the form, not the box's container
    If TypeOf myobj Is TextBox Then
        If Not TypeOf myobj.container Is Form Then
            If These_Are_Equal(myobj.container, box) Then
                myobj.Text = ""
            End If
        End If
    End If
  Next myobj
End Sub

Public Function These_Are_Equal(container As Object, box As Object) As Boolean

On Error GoTo No_Control_Array
These_Are_Equal = container.Name = box.Name And container.Index = box.Index

Exit Function

No_Control_Array:
These_Are_Equal = container.Name = box.Name

End Function


[-Note- the container property actually gives you a reference to whatever control contains whatever you are looking at. So box.container actually returns whatever contains the box(Usually a form but possibly another container control). If you were to take your original code and put one picturebox inside of another and try to run this routine on the inner-most picturebox, you would recieve an error (Object doesn't support this property or method). Another problem is control arrays (all the elements have the same name). You cannot check the index property of a control if it isn't inside an array, but if it is inside an array you MUST compare the index value to make sure the controls are the same. My answer to the array problem is to have a second function (These_Are_Equal) which first tries to compare indexes but if it encounters problems it just compares names. The .parent property returns the form so we can cycle through it's controls.]

I hope this helps you out.
0
 
LVL 1

Author Comment

by:tommy_boy
ID: 1488529
Sorry VBWhiz, just got on, and I have already found the problem. I was only refering to the name not the index of the control array (ie frame1(0), frame2(2) etc) The code is below if you need it at any time.  And yes I know it is a little messy, but I had to do it quickly.  Cheers!
This removes text from textboxes placed on a form inside frames.  The frames are a control array, and the textboxes can be anything they want to be.

Public Sub clear_boxes(f as form, inx As Integer, box as object)
  Dim myobj As Object
  For Each myobj In f
    If TypeOf myobj Is TextBox And myobj.Container.Name = box.Name Then '..now check to see if the index of the frame is the same as we require
      If myobj.Container.index = inx Then
        myobj.Text = ""
      End If
    End If
  Next myobj
End Sub



0
 
LVL 2

Accepted Solution

by:
JiaH earned 100 total points
ID: 1488530
How about this one:

Public Sub Clear_Boxes(box As Object)
    Dim ctrX As Control
    For Each ctrX In Controls
        If (TypeOf ctrX Is TextBox) And (ctrX.Container Is box) Then
            ctrX = ""
        End If
    Next
End Sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question