473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 13574
TheSmileyCoder
2,322 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,322 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,556 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

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

Similar topics

1
by: deko | last post by:
I have a popup "Tools" form with a tab control that has a different subform on each tab. The problem is that new tabs/subforms continue to be added as users request new features - so the code on...
1
by: marc-andr? | last post by:
Hi, I have to loop on each control on a webforms... I use this function : public void LoopOnAllControls(ControlCollection coll) { foreach (System.Web.UI.Control ctrl in coll) {
1
by: RC | last post by:
Hi, I am newbie on ASP.NET and VS.NET I want to build a site with multiple dll of code-behind code for each web form. As I know each project build in VS.NET....all code-behind codes will be...
3
by: VJ | last post by:
I have a MDI application with Child windows... On one specfic child window if I click a Toolbar (part of the child window), I launch a Window that will be child of the MDI and it comes up...
1
by: Shurik | last post by:
Hey everyone, Just run into an issue with ASP.NET 2.0 Image control... I assign CSS style to it in HTML (CssClass="blah"), but when I hit the page with a browser my style isn't used due to...
2
by: david.boone | last post by:
Hello, I am trying to enable controls based on the value of a checkbox, i.e. if value = true then enable. I have a tab control form with controls on 4 tabs. I have some code (below) on the...
0
by: Filippo Bettinaglio | last post by:
hi, I have developed a window control form in C sharp 2005, I can use the component in other .exe applications (just keeping the two project in the same solution group file) but I cannot use it...
4
by: TonyMast | last post by:
win forms - xp pro - vb 2005 Shouldn't this code look at all textboxes on my form and make it lightblue and say "OK"? It seems to be evaluating a button I have on the form and giving me this...
2
by: TerryStone | last post by:
I have created a control that displays a list of items. During design mode I fill it with junk data using calls from the constructor. So when I look at a form with the control on, instead of...
0
by: GeoffT | last post by:
I have encountered a problem with the data that is displayed in the list boxes that are located on a tab control access form (2003 version). This form uses several combo boxes as filters (After...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.