Full Version: Programmatically delete a control
UtterAccess Forums > Microsoft® Access > Access Forms
gavinclarke
Hi -
I have a subform in which i programmatically add labels to depending on the results of a recordset.
Before I add these labels, i want to delete all the labels that already exist on the form, i was wondering if there was a way to do this programmatically?
Odon't just want to hide the existing labels as it will quickly fill up with the maximum 748 controls allowed on a form, i need to actually remove them from the form.
I have the following code which loops through the controls on the subform and determines wether or not it should be deleted, but what do i need to actually delete the control?
'*************code start**************************
Function RemoveControls(ByVal frm As Form)
On Error GoTo Err:
Dim ctl As Control
For Each ctl In frm.Controls
If ctl.ControlType = acLabel Then
If Left(ctl.Caption, 3) = "lbl" Or Left(ctl.Caption, 3) = "box" Then
'do nothing
Else
'delete label

End If
End If
Next ctl
Exit Function
Err:
MsgBox Err.Description, , "Client Request Tracking"
Exit Function
End Function
'**************code end**************************
adamsherring
Yes there definatly is.
!--c1-->
CODE
    DoCmd.OpenForm "Legend", acDesign, , , acFormEdit, acHidden
    
    Set frmLegend = Forms!Legend
    For x = frmLegend.count - 1 To 0 Step -1
        DeleteControl "Legend", frmLegend(x).Name
    Next x

Odon't see the name of your form in your code, so you can adapt what I've written to suit your needs.
Hope that helps,
Adam
freakazeud
Hi,
I would not advise to do this...a form has a limit of how many controls you can add over it's lifetime...you will reach that limit very very quickly if you constantly programmatically add/delete them. I would instead leave the static labels and hide/show them if needed and adjust their caption property accordingly.
HTH
Good luck
adamsherring
Wait a minute,
There is this limit and what is it?
Crap.
Adam
freakazeud
The limit is a hidden counter and it is 754!
HTH
Good luck
adamsherring
Is there any way to reset this counter?
rap again.
Adam
freakazeud
I think a compact and repair will do that.
HTH
Good luck
adamsherring
Is there a similar counter for forms per database with regards to creating and deleting?
dam
gavinclarke
I take it that this limit includes controls that were deleted, and isn't just inclusive of controls that are currently on the form?
lso whilst the DeleteControl appears to work, my code seems to be skipping half the controls on the form?
anybody know why this is?
I've even changed it slightly so that i deletes all labels
Adam - to answer your question about the form, im actually passing the form into the RemoveControls function
Function RemoveControls(ByVal frm As Form)
On Error GoTo Err:
Dim ctl As Control
Dim i As Integer

For Each ctl In frm.Controls
If ctl.ControlType = acLabel Then
DeleteControl frm.Name, ctl.Name

End If
Next ctl
Exit Function
Err:
MsgBox Err.Description, , "Client Request Tracking"
Exit Function
End Function
freakazeud
The number of objects in a db are 32,768 in Acc03!
HTH
Good luck
adamsherring
Freakazeud,
Thanks.
Gavin,
The first time I tried to remove controls with a "each ctl" style loop, it skipped over half the controls for me as well. The only way I was able to get all of them was with the For x = frmLegend.count - 1 To 0 Step -1 loop.
Try switching it over to that loop and see if it works.
Adam
niesz
The other "fly in the ointment" of using this type of technique is that this DB could never be distributed as an MDE. Design mode is not available in an MDE. I would try to adapt your code/technique to use Freak's first suggestion of hiding unwanted controls and unhiding them as needed.
gavinclarke
thanks for all your input with this, sorry it took so long to get back to you all.
understand Freak's suggestion of hiding unwanted controls and i also understand some of the concerns & issues involved in building forms on the fly, but I am not sure that I can get around it by just hiding controls.
The database is going to be used in tracking projects (at client requests) that are undertaken by the company, rather than having a form in which they have to scroll through all the projects one by one, i want to present them with a form that contains all the projects (project ID, Name & Status) in a row/column format.
From this they can then click on the project ID which will open another form from which they can view the full project details.
Odon't think that hiding the controls will be particularly practical as i don't know how many projects will be added to the database over time etc.
I understand that due to the limitations on the number of controls on a form, i will have to ensure that either the data is purged (closed projects) every so often and the database is compacted & repaired.
Im not too familiar with the benefits of distributing the database as an MDE, but i don't think it will be necessary in any case as this database is just going to be used for internal tracking purposes.
Adam - I've used the for x-frmLegend.count-1 To Step -1 loop to remove all the unwanted controls and it works fine, thanks for that.
thanks for your help
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.