469,288 Members | 2,357 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

DeleteControl For..Each Control in Form, arbitrary behaviour

Knut Ole
im trying to delete all existing controls in a form before creating new ones... however, it wont delete all the controls. for every code run i create 6 controls, but for every run the "For Each" frmSub1.Controls will only delete 3. actually, if i start out with 0 controls on my form, the first run 0 will be deleted, and 6 made. second run, 3 will be deleted, 6 made (total = 9). third run, it will delete 5, add 6 (total = 10), same fourth run (tot=11), then stabilize on 6 deletions and 6 additions (tot=11) for every run after that... (when i want all to be deleted every time!)

why arent "For Each ctl In Forms!frmSub1.Controls" picking up all the controls?

thanks!


Expand|Select|Wrap|Line Numbers
  1. Public Function makeArrows()
  2.  
  3.     Dim lngColor As Long
  4.     Dim str1
  5.     Dim str2
  6.     Dim n As Integer
  7.     Dim RecSet As Recordset
  8.     Dim DatePoint As Recordset
  9.  
  10.     Set DatePoint = CurrentDb.OpenRecordset("DatePointer")
  11.     Set RecSet = CurrentDb.OpenRecordset("CalQarrows")
  12.     lngColor = RGB(0, 255, 0)
  13.  
  14.  
  15.     c = DatePoint.Fields("RStartDate").Value 'access day# for manual datepicker in calendar
  16.     d = 0
  17.  
  18.     '// some way of picking each room/subform
  19.     Forms!Calendar!ctrlSub1.SourceObject = ""
  20.     n = 1
  21.     str1 = "frmSub" & n
  22.     DoCmd.OpenForm str1, acDesign, , , acFormEdit, acHidden
  23.     '// delete all controls
  24.     Dim ctl As Control
  25.     o = 0
  26.     For Each ctl In Forms!frmSub1.Controls
  27.         o = o + 1
  28.         'MsgBox str1 & " / " & ctlDel.Name
  29.         'MsgBox str1
  30.         DeleteControl str1, ctl.Name
  31.     Next ctl
  32.     MsgBox o
  33.  
  34.     Do Until RecSet.EOF
  35.         If RecSet.Fields("RoomNumber").Value = 105 Then
  36.  
  37.         d = d + 1
  38.  
  39.         a = (RecSet.Fields("SlotBegin").Value - c) * 1000
  40.         b = (RecSet.Fields("SlotLength").Value * 1000)
  41.         bookID = RecSet.Fields("Bookings.ID").Value
  42.         bookNameS = RecSet.Fields("NameString").Value
  43.         bookFullName = RecSet.Fields("LastName").Value & ", " & RecSet.Fields("FirstName").Value
  44.         bookDate = RecSet.Fields("INdate").Value & " @ " & RecSet.Fields("INtime").Value & " to " & RecSet.Fields("OUTdate").Value & " @ " & RecSet.Fields("OUTtime")
  45.  
  46.  
  47.  
  48.         Set cImg1 = CreateControl(str1, acImage)
  49.         cImgn = cImg1.Name
  50.         Forms!frmSub1!(cImgn).BackStyle = 0
  51.         Forms!frmSub1!(cImgn).Width = 432
  52.         Forms!frmSub1!(cImgn).Height = 360
  53.         Forms!frmSub1!(cImgn).Left = a
  54.         Forms!frmSub1!(cImgn).Top = 0
  55.         Forms!frmSub1!(cImgn).ControlTipText = bookFullName
  56.         Forms!frmSub1!(cImgn).OnClick = ""
  57.         Forms!frmSub1!(cImgn).Picture = "C:\Users\Lailita\Documents\arrows\yellowA.png"
  58.  
  59.         Set cImg2 = CreateControl(str1, acImage)
  60.         cImg2n = cImg2.Name
  61.         Forms!frmSub1!(cImg2n).BackStyle = 0
  62.         Forms!frmSub1!(cImg2n).Width = 432
  63.         Forms!frmSub1!(cImg2n).Height = 360
  64.         Forms!frmSub1!(cImg2n).Left = a + b
  65.         Forms!frmSub1!(cImg2n).Top = 0
  66.         Forms!frmSub1!(cImg2n).Picture = "C:\Users\Lailita\Documents\arrows\yellowAh.png"
  67.  
  68.         Set cLbl1 = CreateControl(str1, acLabel)
  69.         cLbln = cLbl1.Name
  70.         Forms!frmSub1!(cLbln).Visible = True
  71.         Forms!frmSub1!(cLbln).BackColor = RGB(255, 194, 14)
  72.         Forms!frmSub1!(cLbln).ForeColor = RGB(255, 255, 255)
  73.         Forms!frmSub1!(cLbln).FontWeight = 900
  74.         Forms!frmSub1!(cLbln).TopMargin = 34
  75.         Forms!frmSub1!(cLbln).TextAlign = 2
  76.         Forms!frmSub1!(cLbln).BackStyle = 1
  77.         Forms!frmSub1!(cLbln).Top = 0
  78.         Forms!frmSub1!(cLbln).Height = 350
  79.         Forms!frmSub1!(cLbln).Left = a + 250
  80.         Forms!frmSub1!(cLbln).Width = b
  81.         Forms!frmSub1!(cLbln).Caption = bookNameS
  82.         Forms!frmSub1!(cLbln).ControlTipText = bookFullName & ": " & bookDate
  83.  
  84.         End If
  85.  
  86.     RecSet.MoveNext
  87.     Loop
  88.     MsgBox "no of recset loops is " & d
  89.     Dim ctlCheck As Control
  90.     p = 0
  91.     For Each ctlCheck In Forms!frmSub1.Controls
  92.         p = p + 1
  93.         'MsgBox str1 & " / " & ctlDel.Name
  94.         'MsgBox str1
  95.         'DeleteControl str1, ctl.Name
  96.     Next ctlCheck
  97.     MsgBox "total controls at build end is " & p
  98.     DoCmd.Close acForm, str1, acSaveYes
  99.     Forms!Calendar!ctrlSub1.SourceObject = str1
  100.  
Mar 16 '11 #1

✓ answered by TheSmileyCoder

Sorry, I didn't have the correct syntax in front of me when I wrote the last post.

A small modification to your code if I may, to make it nicer:
Expand|Select|Wrap|Line Numbers
  1.     Do While Forms(strFormName).Controls.Count > 0
  2.         DeleteControl strFormName, Forms(strFormName).Controls(1).Name
  3.     Loop

4 12510
TheSmileyCoder
2,321 Expert Mod 2GB
Because imagine looping through an array of controls, where first you delete the first element, then proceed to delete the second element and so on. Problem is that as you delete the first element, the remaining controls "move down".
This behavior is something you need to be wary of, when using for each in combination with deletions and maybe also additions.

Instead you can use:
Expand|Select|Wrap|Line Numbers
  1. Do While Forms!frmSub1.Controls.Count>0
  2.  Forms.Controls(0).Delete
  3. Loop
Maybe the 0 should be a 1, not 100% sure.
Mar 16 '11 #2
thanks, looks clean - but i get a object not supported for the .Delete line..?
but this worked now:
Expand|Select|Wrap|Line Numbers
  1. Do While Forms!frmSub1.Controls.Count > 0
  2.         For Each ctl In Forms!frmSub1.Form.Controls
  3.         DeleteControl str1, ctl.Name
  4.         Next ctl
  5.     Loop
  6.  
so thank you very much!!
Mar 16 '11 #3
TheSmileyCoder
2,321 Expert Mod 2GB
Sorry, I didn't have the correct syntax in front of me when I wrote the last post.

A small modification to your code if I may, to make it nicer:
Expand|Select|Wrap|Line Numbers
  1.     Do While Forms(strFormName).Controls.Count > 0
  2.         DeleteControl strFormName, Forms(strFormName).Controls(1).Name
  3.     Loop
Mar 16 '11 #4
NeoPa
32,173 Expert Mod 16PB
I don't know if performance is an issue, but to save the object references to be worked out each time through the loop :

Expand|Select|Wrap|Line Numbers
  1. With Forms(strFormName).Controls
  2.     Do While .Count > 0
  3.         Call DeleteControl(strFormName, .Item(0).Name)
  4.     Loop
  5. End With
Mar 18 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by marc-andr? | last post: by
3 posts views Thread by VJ | last post: by
2 posts views Thread by david.boone | last post: by
reply views Thread by Filippo Bettinaglio | last post: by
4 posts views Thread by TonyMast | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.