472,780 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 13426
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,534 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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.