Solved

clear all dynamically created controls

Posted on 2002-07-10
11
143 Views
Last Modified: 2010-05-02
In VBA I was doing something like:

MyForm.Controls.Clear

to clear all the controls on this form.

In VB, if I do that I get an error saying the "Object is not supported". I figured out how to add them in VB but I want to get rid of all of them at once. Can I do this?

To make it more complicated I'm not actually trying to clear controls off a form but clear the controls off a tabstrip (SSTab). Is there another way to do this or am I doing something wrong? Thanks,

guidway
0
Comment
Question by:guidway
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7144072
Try this:
dim c
for each c in MyForm.COntrols
  c.Value = empty
next

CHeers
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7144075
sorry, disregard my comment. You try to remove the control as such, not clear the values.

To clear all the controls, I would load a new instance of the "empty" form, be aware that controls that you added at design time cannot be removed by VB code as such.

CHeers
0
 
LVL 12

Author Comment

by:guidway
ID: 7144098
The controls are all created at runtime. I'm not actually using a form, all the controls are on a tabstrip.

guidway
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7144108
>>I'm not actually using a form
you must be using a form to hold the tabstrip?!
so as i suggested, reload your form should work fastest.
CHeers
0
 
LVL 12

Author Comment

by:guidway
ID: 7144154
I see your point. I tried this but the problem is that some of the controls on the form and SSTab are created at design time. Only one tab on the SSTab has controls that are created at runtime. If I try reloading (unloading/loading) the form again it does not work.

Here's my code:

dim withevents lab as VB.label
dim withevents chk as VB.checkbox

Private Sub SSTab1_Click(PreviousTab as Integer)

  if SSTab1.Tab = 1 then

     Select Case (MyForm.EntryLabelType)
         Case "Type"
            set lab = Controls.Add("VB.Label", "labSwitch")
            set lab.Container = SSTab1
            SSTab1.Tab = 1
            lab.Caption = "Switches: "
            lab.Move 40,565, 1300
            lab.Visible = true
     end Select
  end if
end sub

Basically what I'm trying to do is when I click on this tab some controls are dynamically created. I want to make sure that the controls are not already there so the best way of doing this is just by clearing them. If the user wants they can pull up the form again but if they selected different options before they load the form then different controls will get loaded into the tab (that's the reason I have the switch statement). I'm not sure I'm following you on reloading the form. If you are talking about doing a Unload/Load on it I tried that and it locked up the project. Please be more specific. Thanks for the help,

guidway
0
 
LVL 12

Author Comment

by:guidway
ID: 7144201
Since I add them with a Controls.Add I really don't see why Controls.Clear won't work. It doesn't make sense to me.

guidway
0
 
LVL 12

Author Comment

by:guidway
ID: 7144345
I figured it out! All I needed to do was do a test to see if the control existed using this:

if lab is nothing then
   set lab = Controls.Add("VB.Label", "lab1")
end if

That way I don't even have to unload the control. If it exists it won't try to recreate it. I can't believe I didn't think of that earlier. Thanks anyway,

guidway
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 7144863
guidway:

You would use the REMOVE Method of the Controls Collection to unload your dynamically-created controls.

For example:

   Controls.Remove "lab1"

Please note that the Remove method can only be used with dynamically created controls.

The Controls collection, by design, does not support the CLEAR method.

0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 7144882
guidway:

One thing you could do to make managing it easier is to add a reference for each control you create dynamically to a collection.

To illustrate:

   Dim colCtrls       As Collection
   Dim WithEvents lab As VB.Label
   Dim WithEvents chk As VB.Checkbox


   Set colCtrls = New Collection
   Set lab = Controls.Add("VB.Label", "labSwitch")
   Set chk = Controls.Add("VB.Checkbox", "MyChkBox")

Then later, to unload those dynamically created controls:

   Dim Index As Long

   For Index = 1 To colCtrls.Count
      Controls.Remove colCtrls(Index).Name
   Next Index
   Set colCtrls = New Collection


0
 
LVL 12

Author Comment

by:guidway
ID: 7145938
Thanks guys for the suggestions however I'm on a time limit and don't have time to implement them. Unless anyone has a complaint I'm just going to have a mod delete the question. I ended up answering my own question. ;-) Thanks again though.

guidway
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 50 total points
ID: 7154505
Points refunded and moved to PAQ

** Mindphaser - Community Support Moderator **
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

838 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