Solved

Remove controls created at run-time

Posted on 2002-07-17
9
163 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 49

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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now