Solved

Delete all controls on a form

Posted on 2003-12-02
16
620 Views
Last Modified: 2008-03-03
Hi All,

Has anyone ever tried to delete all the controls on a form from vba? I'm using the following code behind a command button on a different form to "MyForm":

____________________________________________________________

  Dim ctl As Control

  DoCmd.OpenForm "MyForm", acDesign, WindowMode:=acWindowNormal
 
  For Each ctl In Forms!MyForm.Controls
    'MsgBox ctl.Name
    DeleteControl frm, ctl.Name
  Next
____________________________________________________________


For some reason a random selection of the controls are deleted and a few remain! If I add another for loop like:

For i = 0 To 3
  For Each ctl In Forms!MyForm.Controls
    'MsgBox ctl.Name
    DeleteControl frm, ctl.Name
  Next
Next

it works!!

Haydn.
0
Comment
Question by:HaydnH
  • 6
  • 6
  • 4
16 Comments
 
LVL 32

Expert Comment

by:jadedata
ID: 9859783
Hey HaydnH!

  "DELETE CONTROLS" - slow down cowboy!!  Is there a really good reason to delete vs. say maybe just making the visible property False??

regards
Jack
0
 
LVL 39

Accepted Solution

by:
stevbe earned 250 total points
ID: 9860083
yeeee  haw ... you need to delete items from a collection in reverse order

For i = Forms!MyForm.Controls.Count To 0 Step -1
    DeleteControl Forms!MyForm, Forms!MyForm.Controls(i).Name
Next


Steve
0
 
LVL 8

Author Comment

by:HaydnH
ID: 9860230
Thanks steve - that works fine... (well it works as below)... any idea why you have to do this? why can't you just delete them all by name as I have tried above??

                                                         \/
For i = Forms!MyForm.Controls.Count - 1 To 0 Step -1
    DeleteControl Forms!MyForm, Forms!MyForm.Controls(i).Name
Next


Hi Jack,

The reason I want to delete the controls is because I'm playing around trying to learn a little more =P

Haydn.
0
 
LVL 32

Expert Comment

by:jadedata
ID: 9860251
ahhhhh,... ok then!!  (that's exactly how I learned it...)
When do you expect you'll be seeing an opportunity to use this new found tidbit??  
You must have had some scenario in mind???
0
 
LVL 39

Expert Comment

by:stevbe
ID: 9860270
What happens is that a collection automatically resets itself after you delete objects so if you delete one then the one that was in the next position has moverd into the first position and the code says to move to thwe next item in the control. You haqd to add the .Count -1 because the collection is 0 based so you have 10 items but their positions in the collection are 0-9.

Steve
0
 
LVL 39

Expert Comment

by:stevbe
ID: 9860318
the lesson could be generically applicaed to deleting items from a collection ???
0
 
LVL 8

Author Comment

by:HaydnH
ID: 9860321
"When do you expect you'll be seeing an opportunity to use this new found tidbit??"

Not sure - it would probably be easier than creating a new form from code as you could save all those little formatting rules, cangrow, backcolor etc etc without having to change them again after creating the new form...

I was thinking it might be useful if you have the following conditions:

 - you might need N controls of the same type - never knowing the max number
 - the controls might be referenced from other forms so that the name must remain the same


I have a project in mind, I'll explain a little more tomorrow (going to the company x-mas dinner now) =)

Haydn.
0
 
LVL 32

Expert Comment

by:jadedata
ID: 9860354
:).... ahhhh, the learning curve...!!
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 39

Expert Comment

by:stevbe
ID: 9860371
you could save all those little formatting rules

you can save this by crerating a generic form with all of  the various control types, set background, whatever then Format | AutoFormat | Customize, Create a new Autoformat based on form xxx, now set the default form style for your app in Tools | Options | Forms/Reports type in the name of the autoformat you just created.

Steve
0
 
LVL 32

Expert Comment

by:jadedata
ID: 9860394
Maybe I'm the only one left that builds the recordset and then uses that to set the form controls....  :)
0
 
LVL 39

Expert Comment

by:stevbe
ID: 9860532
Hey Jack,

Do you mean that you create controls at runtime or that you set the format properties according to your table?

Steve
0
 
LVL 32

Expert Comment

by:jadedata
ID: 9860562
I will usually NOT set things like Format/InputMask/Required/AllowZeroLen on the table.  I will set these properties only on the form.  Like this I can change the INTERFACE to effect changes, not a bunch of table changes against production tables.
0
 
LVL 39

Expert Comment

by:stevbe
ID: 9860684
Interesting, I put Format and Input mask onto the form because they are interface properties and Required and Allow ZeroLen on the table because they are data integrity properties. Do you read relationships from your table to be enforced at the form or capture the Form_Error that gets thrown when someone breaks the rules? I started on a UI table based that controls who can edit which fileds, which forms are updateable according to psuedo groups, because I have this down to the control level perhaps I could expand to include formatting / input mask ... hmmm have to think about this ...

Steve
0
 
LVL 32

Expert Comment

by:jadedata
ID: 9860969
If you have a relational integrity enforcement in place the Required property is not needed, the relationship error will pick up the slack.

I try to NEVER use AllowZeroLen,. I find nulls much easier to find and deal with then zero len field values.
0
 
LVL 8

Author Comment

by:HaydnH
ID: 9875141
hmmmm better remember to close this question.

Haydn.
0
 
LVL 8

Author Comment

by:HaydnH
ID: 9875170
p.s: Jack... if you want to see the sample db where I used this "tidbit" let me know your e-mail address =P

Haydn.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access query with left expression 9 32
Create Form using Wizard 14 39
linked subforms are yielding error:  ... (800110108) 3 16
Search Form not Querying 2 12
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now