Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Remove controls created at run-time

Posted on 2002-07-17
9
Medium Priority
?
174 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 53

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
Industry Leaders: 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 400 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

598 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