开发者

VBA Code Stops Working

开发者 https://www.devze.com 2022-12-13 03:12 出处:网络
The following code is called everytime the form is opened. It works great until the 5th opening and then mi开发者_如何学编程sses deleting one of the controls. Anyone know why this is?

The following code is called everytime the form is opened. It works great until the 5th opening and then mi开发者_如何学编程sses deleting one of the controls. Anyone know why this is?

 For Each cb In Forms(frmName).Controls
     If cb.ControlType = acCheckBox Then
         If Left(cb.Name, 3) = "clr" Then
            DeleteControl frmName, cb.Name
         End If
     ElseIf cb.ControlType = acLabel Then
         If Left(cb.Name, 3) = "clr" Then
            DeleteControl frmName, cb.Name
        End If
     End If
 Next


When you delete an item from a collection in Access the next item moves into that items spot. Thus when it comes to deleting items from a collection you must start at the end of the collection and go backward.

So replace the

For Each cb In Forms(frmName).Controls

line with

For counter = Forms(frmName).Controls.Count - 1 To 0 Step -1
set cb = Forms(frmName).Controls.Item(counter)

My next question though is what is your overall objective? It's unusual to be manipulating controls in design view programmatically.

0

精彩评论

暂无评论...
验证码 换一张
取 消