Solved

Remove controls created at run-time

Posted on 2002-07-17
9
165 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
9 Comments
 
LVL 50

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

831 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