Solved

Remove controls created at run-time

Posted on 2002-07-17
9
167 Views
Last Modified: 2010-05-02
I need to remove some controls that I have added at run-time using the code shown below. The problem is that I need to remove these controls in order to create some of them again with the same name. Please, don't ask me why.

Dim EntryControls() As TextBox
Dim rc as ADODB.RecordSet

ReDim EntryControls(rc.RecordCount)

For i = 0 To rc.RecordCount - 1
   Set EntryControls(i) = Me.Controls.Add("VB.Textbox", "txtCampo_" + CStr(rc("CodCampo")), frameVariables)
Next i
0
Comment
Question by:svillalba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 7158987
Try Me.Control.Remove(ControlName) ?
0
 
LVL 3

Expert Comment

by:cmgarnett
ID: 7159082
Would you also need to

Set EntryControls(i) = Nothing

in order to reclaim the memory?
0
 

Author Comment

by:svillalba
ID: 7159198
Me.Control.Remove(ControlName) produces the following error:
"Cannot discharge in this context".
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
gencross earned 100 total points
ID: 7159230
You may not be able to use the remove method on controls in a control array.  You can try the Unload statement to unload these controls, but I'm not sure if it will work on controls added with the Add method.

If this does not work...
frmName.Controls.Remove "txtCampo_" + CStr(rc("CodCampo"))

Try...
Unload "txtCampo_" + CStr(rc("CodCampo"))

If that does not work would it be possible to create the controls using the Load statement?  You would need to create the first textbox at design time however (you could set it to invisible until you are ready to create the controls if you need to).  Then you would be able to unload the controls easily.

Let me know if you need more info.
0
 

Author Comment

by:svillalba
ID: 7159309
Firstly sorry for my english. The error message was:
"Cannot unload in this context".

I have tried
  Unload "txtCampo_" + CStr(rc("CodCampo"))
and it doesn't work, I got a compilation error. "Data types doesn't match".

I could do what gencross has told me, it's a good solution, but I'm a C++ programmer and I think is not too smart. I think if there is a function to add a control, should be another function to remove that control. That's the normal way, isn't it?.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7159349
What procedure / event are you trying to remove the control in? It is possible to get an error such as "Cannot unload in this context" if you are attempting to unload the control in an event procedure relating to that control.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7160125
If you slightly modify your project such that the controls have one design-time control (with index=0), then you can simply Load and Unload any additional controls (in that control array) as needed at run-time.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8013765
Hi svillalba,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept gencross's comment(s) as an answer.

svillalba, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8096093
Comment from expert accepted as answer

Computer101
E-E Admin
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 41
Copy a row 12 69
Visual Studio 2005 text editor 10 49
I need help embedding an image as HTML in my vb.net application 3 79
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question