Solved

clear all dynamically created controls

Posted on 2002-07-10
11
146 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Technology Partners: 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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

717 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