Link to home
Start Free TrialLog in
Avatar of svillalba
svillalba

asked on

Remove controls created at run-time

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
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Try Me.Control.Remove(ControlName) ?
Would you also need to

Set EntryControls(i) = Nothing

in order to reclaim the memory?
Avatar of svillalba
svillalba

ASKER

Me.Control.Remove(ControlName) produces the following error:
"Cannot discharge in this context".
ASKER CERTIFIED SOLUTION
Avatar of gencross
gencross

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?.
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.
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.
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
Comment from expert accepted as answer

Computer101
E-E Admin