With Unlaod "txtName" & i I get a type mismatch. If I try doing a Me.Controls.Remove("txtNam
Any other suggestions, or comments on what I might be doing wrong?
Main Topics
Browse All TopicsIs there a way to delete textboxes and/or labels programatically. I have a list of dynamically created text boxes and labels created similiarly to this:
For i = 0 To XXX
Set oNewTextBox = Me.Controls.Add("VB.Textbo
Next i
Now I need to go back and clean up after myself by deleting all of the textboxes. I tried using Me.Controls.Delete and .Remove, but neither seem to work. Is there another way to delete objects that I can't find in any of my manuals?
Regards,
Jaime
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
To unload controls you need to setup a control array. You can then unload all but the first one.
Using your example, add a TextBox to the form. Change its Name to txtName and give its Index property the value 0 to create a control array.
To create new controls, use:
For i = 0 To XXX
if i > 0 then
Load txtName(i)
end if
txtName(i).Move ?, ?, ?, ?
txtName(i).Visible = True
Next i
To remove controls, use:
For i = XXX to 0 step -1
if i = 0 then
txtName(0).Visible = False
else
Unload txtName(i)
end if
Next i
You should never unload txtName(0). Instead just make it invisible.
phildaley :
When I try using a control array, I still get the "Unable to Unload within this context." error while trying to unload that object:
For i = 1 To rstFPOrder.RecordCount
Load lblItemNum(iManualItems)
lblItemNum(iManualItems).M
lblItemNum(iManualItems).C
lblItemNum(iManualItems).V
Next i
.
.
.
For i = 1 To iManualItems
Unload lblItemNum(i)
Next i
Is the difference between th 'i' in
For i = 1 To rstFPOrder.RecordCount
and the 'iManualItems' in
Load lblItemNum(iManualItems)
a typo it should be
Load lblItemNum(i)
and try changing this
For i = 1 To iManualItems
Unload lblItemNum(i)
Next i
to
For i = lblItemNum.Ubound To 1 step -1
Unload lblItemNum(i)
Next i
In your first loop, you are not increasing iManualItems. Are you missing a iManualItems = iManualItems + 1 ?
Also, is the unload programmed to occur within a combo box click event? If so there is a VB bug that prevents this and causes the error you are seeing.
A workaround is to add a Timer control, then in the combo box click event add this code at the end:
Timer1.Interval = 10
Timer1.Enabled = True
Then program the Timer event as:
Private Sub Timer1_Timer()
Dim i as Integer
Timer1.Enabled = False
For i = 1 To iManualItems
Unload lblItemNum(i)
Next
End Sub
Well, Adding the timer seems to work, but it does bring up another problem.
Basically if I have a list of objects, I wanted to clear the old list before running the new query to create another list. With the timer, even with the Interval set to 1, the new list will populate before the timer activates, when it activates, it deletes everything, including the new items just added.
I'll play around and see what I can't figure out, but since that does solve my original problem, the answer goes phildaley.
If anyone knows of another way other than the timer to get around the VB bug, I would like to hear about it :)
Thanks to all,
Jaime
You could keep two module level variables. One to hold the old max list number and one for the new max list number, then only load/unload the difference. This will also run faster.
For example, your unload loop will then become:
if miNewMax < miOldMax then
For i = miNewMax + 1 To miOldMax
Unload lblItemNum(i)
Next i
end if
I'll let you figure how to program the load loop.
Business Accounts
Answer for Membership
by: emadatPosted on 2003-04-14 at 15:02:37ID: 8329714
Try using
ShowCode.A sp?ID=2815
Unload _Control_Name_
Also take a look at: http://www.freevbcode.com/