Deleting All Controls in a Section

  • Thread starter Thread starter chammock
  • Start date Start date
C

chammock

I am using VBA to create about 150 command buttons on a page with click
events. Each buttton is named cmd*, where * is a row and column number,
like cmdA1. I wanted a quick way to delete the buttons from the Detail
Section during testing. I am using the following code. I have stepped
through the process, which works, but it only deletes every other
control on the page. If I run it again it deletes 1/2 again. Why would
it only be getting 1/2 of the controls on each iteration? Also, can I
just select ALL controls in the Detail Section at once to delete? There
is other info in the header/footer that I don't want to delete.

DoCmd.OpenForm "frmPanelRightTemplate", acDesign, , , , acWindowNormal
Dim varControl As Variant

For Each varControl In Forms![frmPanelRightTemplate].Controls
MsgBox (varControl.Name) ' used for testing only
If Mid(varControl.Name, 1, 3) = "cmd" Then 'In design view set the tag
property of the control to "Visible
DeleteControl "frmPanelRightTemplate", varControl.Name
End If
Next

DoCmd.Save

DoCmd.OpenForm "frmPanelRightTemplate"
 
I would think you should just make them invisible rather than deleting them.
You may run into issues unless you compact frequently.

You should count the number of controls and then loop through backwards:

intCtrlCount = Me.Count -1
For intCtrlNum = intCtrlCount to 0 Step -1
If Mid....
....
Next
 
Access is getting confused because the controls are disappearing while the
loop is executing.

Loop backwards through the controls instead:

For i = Forms![frmPanelRightTemplate].Controls.Count -1 To 0 Step -1
Set varControl = Forms![frmPanelRightTemplate].Controls(i)
...
Next
 
Thanks. That worked well. I am also creating click events with the
command buttons that get created. For testing, I need to be able to hit
a button and delete the 150 click events created. Is there a
DeleteModule type of functionality in Access like the DeleteControl? I
tried something like the code below, but it did not recognize
DeleteModule.

DoCmd.OpenForm "frmPanelRightTemplate", acDesign, , , ,
acWindowNormal

Dim varModule As Variant

For i = Forms![frmPanelRightTemplate].Modules.Count - 1 To 0 Step
-1
Set varModule = Forms![frmPanelRightTemplate].Modules(i)
MsgBox (varModule.Name)
If Mid(varModule.Name, 1, 3) = "vbacmd" Then
DeleteModule "frmPanelRightTemplate", varModule.Name
End If
Next

DoCmd.Save

DoCmd.OpenForm "frmPanelRightTemplate"
 
Back
Top