Solved

Remove controls created at run-time

Posted on 2002-07-17
9
169 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 52

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 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…
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…

726 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