Delete Controls

Kango

Registered User.
Local time
Today, 08:04
Joined
Mar 11, 2004
Messages
29
looping through a Form and deleting all controls!

Hi People,

I have a manu form which has a command button. When the button is press it should open a form called FrmBank2 and delete all controls on the form by looping through them. The problem is only 6 controls (textboxes) get deleted and the rest of the controls (Textboxes) remain on the form. I need to delet all the textboxes on the form?. I am using the following code -

Dim ctrlCntrl As Control
Dim frmParent1 As Form


DoCmd.OpenForm "FrmBank2", acDesign, , , , acHidden
Set frmParent1 = Forms!FrmBank2

For Each ctrlCntrl In frmParent1.Controls
DeleteControl frmParent1.Name, ctrlCntrl.Name
Next ctrlCntrl

End Sub



Any ideas as to what might be the problem.

Thanks!!!
 
Kango said:
When the button is press it should open a form called FrmBank2 and delete all controls on the form by looping through them.

Er, why not just have a blank form to begin with? :confused:
 
Mile-O-Phile said:
Er, why not just have a blank form to begin with? :confused:

Sorry i should have been a bit more clearer on what i am trying to do.

I have a specific form which uses different tables as a recordsource. Each of these tables has a different number of fields so at the moment i have a procedure that will change the recordsource of the form to any table i require, it will then create the new controls (textboxes) for each field in the table. This all works fine, but everytime i run the procedure it keep's on adding new control's, what i need to do is delete the existing controls , and then run my add procedure. The form is formatted in a specific way so i do not want to start from a blank form if i can help it.

Thanks
 
So, why not just make six forms - one each to fit the purpose of one RecordSource. It sounds like you are making this overcomplicated.
 
Mile-O-Phile said:
So, why not just make six forms - one each to fit the purpose of one RecordSource. It sounds like you are making this overcomplicated.

Noop!!,

Can't do that. The Tables are created by the user on the fly from a user input form. This user input form is bound to a table which will store the list a specific tables the user has created (this again is decided by the user). The names could be anything they like within reason!, and the number of fields could be any number (but they would all be numeric) . The system is fairly complex, at this point if i could clear all the controls of a specific form i am on the way!!!, all else seems to be working fine!!!. Sorry for not being more specific. But i am trying to create a system whereby the user can control certain objects easily through user input forms.


Thanks
 
Kango,

I have a db that I use, that has the same functionality...this is how I implement it:

Have a main form which has all your design, specifics etc...

For the 'changing' data, create an unbound subform. Change the Recordsource and requery every time you have the need to 'switch Tables'...

It will look the same, and be far more efficient to design
 
I am not going to comment on the requirements or the solution. I have very strong opinions on this topic and I am not getting involved. I am only going to explain why your code is not working.

When the code starts, there are n items in the controls collection. The code loops through the collection by incrementing a counter until the counter > the total number of controls in the array. However, as you delete a control, you are changing the number of controls in the array which is being dynamically resized. So, say you have 4 controls:
1 cntlA
2 cntlB
3 cntlC
4 cntlD

You delete #1 which is cntlA. As you delete a control, it slips up the list because the control before it was deleted. There are now 3 items in the array rather than 4. The counter is incremented to 2.

1 cntlB
2 cntlC
3 cntlD

You delete #2 which is cntlC. The counter is incremented to 3 but now there are only 2 items in the list and so the loop ends. You are left with 2 undeleted controls.

1 cntlB
2 cntlD

To solve the problem, you need to find the total number of controls in the array and then write a loop where you control the counter by looping backwards. That way, you'll be deleting controls from the end of the array so there is no problem with changing the size of the array.
 
Can you just keep deleting the first item...

Do
...DeleteControl frm.Name, frm.Controls(0).Name
Loop Until Controls.Count = 0


...to empty a collection?
 
Thanks

Hi All,

Thanks for the advice and more importantly the solution. I can see where i was going wrong!.

Thanks Again For Saving My Bacon!!!!

Kango
 
Kango.

You may still have a problem…

Deleting controls on a form may not fully delete them. Any new controls that are created may simply add to the total number of controls allowed during the life of the form. (Around 750? whatever, doesn’t matter.)

That is to say…the form may crash today, tomorrow or in six months time.

Regards,
Chris.
 

Users who are viewing this thread

Back
Top Bottom