Consulting

Results 1 to 8 of 8

Thread: Solved: Removing Controls on the Fly

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,090
    Location

    Solved: Removing Controls on the Fly

    I am using this code to add a control on the fly:

    [VBA]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
    [/VBA]

    However, I can't figure out how to remove the same control on the fly.

    Daniel

  2. #2
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    Email.Controls(RNM).Delete ?
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,090
    Location
    I tried that and
    [VBA]email.controls(RNM).Remove[/VBA]

    neither one works. Also adding the form is in one control_Click. and removing the control is in a different control_click.

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,090
    Location
    I would also like to test if the control exists before I add it?

  5. #5
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Djblois,
    Are you adding the controls to a userform or a worksheet?
    The most difficult errors to resolve are the one's you know you didn't make.


  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,090
    Location
    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.

  7. #7
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    Djblois,
    Here's the code revisions you need.

    To check to see if the control exists before adding.

    [VBA]
    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
    [/VBA]

    To remove an existing control

    [VBA]
    Private Sub CommandButton2_Click()
    Email.Controls.Remove "ReportName"
    End Sub

    [/VBA]

    HTH
    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  8. #8
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,090
    Location
    Thank you,
    Cbrine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •