+ Reply to Thread
Results 1 to 8 of 8

Insert a Textbox in a Frame during runtime

Hybrid View

  1. #1
    Registered User
    Join Date
    01-13-2007
    Posts
    9

    Post Insert a Textbox in a Frame during runtime

    I´m adding textboxs in runtime mode. I need to insert those controls in a specific Frame.
    I search the web and only found a solution for VB6, but it doesn´t apply to VBA.
    I´m using Excel 2003.

    Code:

    Sub AddTextbox()

    Dim Txt As Control
    set Txt = Me.Controls.Add("Forms.TextBox.1")
    'The new Textbox is added in the Userform
    'I need that the new textbox to be inserted into the Frame1.

    End Sub

    The "Container" method doesn´t apply here (it does in VB6)
    Example: set me.txt.container=me.Frame1

  2. #2
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    Well ... ya know, VBA is only a subset of VB, and a different subset for each version.

    You do not say, and the code does not inform, if these are Controls added to a Form or to a Worksheet. (i.e., who is "Me"?)

    If a Worksheet, there is no "group" object among the ActiveX controls; in which case you would need to use the older (Excel5) controls in order to have a "group" object. If you are going that route, see the Forms toolbar. There is not a lot of documentation of these older Controls, but for certain purposes they work very well.

  3. #3
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203

    Sorry ... ignore previous post

    I take it back. The comments in your code make it clear you are referring to a UserForm. Sorry about that. Try this:

    Sub AddTextbox()
    Dim fr As Frame
    Dim Txt As Control
    
        Set fr = Me.Frame1
        Set Txt = fr.Controls.Add("Forms.TextBox.1")
    
    End Sub
    If you do not already have a frame, then:
    Sub AddTextbox()
    Dim fr As Frame
    Dim Txt As Control
    
        Set fr = Me.Controls.Add("Forms.Frame.1")
        Set Txt = fr.Controls.Add("Forms.TextBox.1")
        
    End Sub

  4. #4
    Registered User
    Join Date
    01-13-2007
    Posts
    9

    Thanks!

    It works thanks!

    Before I posted the thread I tryed to as similar solution.

    I´m making a custom "autofilter" utiliy. I´m adding Texbox that allow the user to insert many criterias.

    Look at this and tell me what is wrong:

    sub AddTexbox()

    dim Txt as control
    dim Fr as Frame
    dim ctrl as control

    set Fr =me.Frame1

    for each ctrl in me.controls
    if typename(ctrl)="Frame" then

    'Here goes another piece of code, because I have many Frames and not all are used to add new textboxs

    set Txt=me.ctrl.controls.add("Forms.Textbox.1")
    'This code fails, I guess that "ctrl" is a frame at this point

    'This one works
    'set Txt=me.Fr.controls.add("Forms.Textbox.1")
    'I need the first line because if not, I repeat the code for every single frame. Can you find the error?

    end if

    next ctrl

    end sub

  5. #5
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    What you are attempting sounds ingenious. I would love to see it when it is finished.

    About this problem line of code:
    Set Txt = Me.ctrl.Controls.Add("Forms.Textbox.1")
    I think the problem is "Me". Since ctrl is already defined as an object, "Me" is not necessary, and possibly is the source of the problem. Try it without me.

  6. #6
    Registered User
    Join Date
    01-13-2007
    Posts
    9
    Quote Originally Posted by MSP77079
    What you are attempting sounds ingenious. I would love to see it when it is finished.

    About this problem line of code:
    Set Txt = Me.ctrl.Controls.Add("Forms.Textbox.1")
    I think the problem is "Me". Since ctrl is already defined as an object, "Me" is not necessary, and possibly is the source of the problem. Try it without me.
    I will send it as soon as I finished. It´s almost finished by now, I'm working on details.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1