View Full Version : Solved: Removing Controls on the Fly
Djblois
01-08-2007, 02:47 PM
I am using this code to add a control on the fly:
 
RNM = "ReportName"
    LBL = "ReportLabel"
    Email.Controls.Add bstrProgId:="Forms.Label.1", Name:=RNM, Visible:=True
    Email.Controls(RNM).Top = 70
    Email.Controls(RNM).Left = 6
    Email.Controls(RNM).Height = 30
    Email.Controls(RNM).Width = 150
    Email.Controls(RNM).Caption = "What do you want to name the file?"
    Email.Controls(RNM).Font.Bold = True
 
However, I can't figure out how to remove the same control on the fly.
 
Daniel
XLGibbs
01-08-2007, 03:09 PM
Email.Controls(RNM).Delete ?
Djblois
01-09-2007, 10:18 AM
I tried that and 
email.controls(RNM).Remove
 
neither one works.  Also adding the form is in one control_Click.  and removing the control is in a different control_click.
Djblois
01-09-2007, 11:19 AM
I would also like to test if the control exists before I add it?
CBrine
01-09-2007, 12:15 PM
Djblois,
  Are you adding the controls to a userform or a worksheet?
Djblois
01-09-2007, 12:53 PM
I am adding them to a userform.  That part works correctly.  However, I can't remove them and I would like to test if they are there already before I add them.
CBrine
01-09-2007, 01:46 PM
Djblois,
  Here's the code revisions you need.
 
To check to see if the control exists before adding.
 
 
Private Sub CommandButton1_Click()
Dim c As Control, F As Boolean
rnm = "ReportName"
LBL = "ReportLabel"
F = False
For Each c In Me.Controls
    If c.Name = rmn Then
        F = True
    End If
Next c
If F = False Then
    Email.Controls.Add bstrProgId:="Forms.Label.1", Name:=rnm, Visible:=True
    Email.Controls(rnm).Top = 70
    Email.Controls(rnm).Left = 6
    Email.Controls(rnm).Height = 30
    Email.Controls(rnm).Width = 150
    Email.Controls(rnm).Caption = "What do you want to name the file?"
    Email.Controls(rnm).Font.Bold = True
Else
    'Not sure what you want to do here
End If
End Sub
 
To remove an existing control
 
 
Private Sub CommandButton2_Click()
        Email.Controls.Remove "ReportName"
End Sub
 
HTH
Cal
Djblois
01-09-2007, 02:46 PM
Thank you,
Cbrine
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.