Results 1 to 10 of 10

Thread: Acessing runtime added controls

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26

    Acessing runtime added controls

    I wish to access some controls added during runtime.

    My codes is as follows.

    Private Function AddCombo()

    Dim myCombo as ComboBox
    Dim line as integar

    Set myCombo = frame1.controls.add("Forms.ComboBox.1", "Combo" & line, true)

    line = line + 1

    End Function

    Private Sub CommandButton1_Click()
    ' This is where i am having problems
    '
    Dim myCombo as ComboBox

    Set myCombo = Form1!("Combo" & line)
    myCombo.Visible = False
    :
    :
    End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Try this instead:

    VB Code:
    1. 'In General Declarations
    2. Dim myCombo as ComboBox
    3.  
    4.  
    5. 'in form load (or where you want!)
    6. Set myCombo = AddCombo
    7.  
    8.  
    9. 'your functions
    10. Private Function AddCombo() as ComboBox
    11.  
    12. 'Use STATIC rather than DIM, so that next time the number wont reset
    13. Static line as Integer
    14.  
    15. Set AddCombo = frame1.controls.add("Forms.ComboBox.1", "Combo" & line, true)
    16.  
    17. line = line + 1
    18.  
    19. End Function
    20.  
    21. Private Sub CommandButton1_Click()
    22. ' This is where i am having problems
    23. '
    24. myCombo.Visible = False
    25. :
    26. :
    27. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    Originally posted by si_the_geek
    Try this instead:

    VB Code:
    1. 'In General Declarations
    2. Dim myCombo as ComboBox
    3.  
    4.  
    5. 'in form load (or where you want!)
    6. Set myCombo = AddCombo
    7.  
    8.  
    9. 'your functions
    10. Private Function AddCombo() as ComboBox
    11.  
    12. 'Use STATIC rather than DIM, so that next time the number wont reset
    13. Static line as Integer
    14.  
    15. Set AddCombo = frame1.controls.add("Forms.ComboBox.1", "Combo" & line, true)
    16.  
    17. line = line + 1
    18.  
    19. End Function
    20.  
    21. Private Sub CommandButton1_Click()
    22. ' This is where i am having problems
    23. '
    24. myCombo.Visible = False
    25. :
    26. :
    27. End Sub
    Thanks,

    I need to access different ComboBox at different time, eg, combo1, combo0, combo4

    By using the above code, I am only able to reference the last comboBox created.

    I need a more dynamic manner to reference the objects created at runtime, such that I will be able to do any manipulation to them at any one time, just by knowing which line to control.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    In that case you need to use a collection or an array.

    Here's an example using a collection:
    VB Code:
    1. 'In General Declarations
    2. Dim myCombo as Collection
    3.  
    4.  
    5. 'in form load (or where you want!)
    6. Set myCombo = New Collection
    7. Dim combo_number as Integer
    8. For combo_number = 1 to 5
    9.   myCombo.add AddCombo(frame1,combo_number)
    10. Next combo_number
    11.  
    12.  
    13. 'your functions
    14. Private Function AddCombo(Parent_Control as object, line as Integer) as ComboBox
    15.  
    16. Set AddCombo = Parent_Control.controls.add("Forms.ComboBox.1", "Combo" & line, true)
    17.  
    18. End Function
    19.  
    20.  
    21. Private Sub CommandButton1_Click()
    22. ' This is where i am having problems
    23.  
    24. Dim combo_number as Integer
    25. For combo_number = 1 to 5
    26.   myCombo(combo_number).Visible = False
    27. Next combo_number
    28.  
    29. 'OR
    30. Dim tmpCombo
    31. For Each tmpCombo in myCombo
    32.   tmpCombo.Visible = False
    33. Next tmpCombo
    34. :
    35. :
    36. End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    I will give the collection a try.

    Is there any other means?
    I know that to reference a runtime added control I can use the following code, using "!" instead of "." Form1!Combo1.text.

    I wish to see if there is any means to type cast a string "Combo1
    " into an object so that I can reference it.

    If there isn't any, I will use your suggestion.

    Thank you very much!!

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    There is a Controls collection that is automatically created & updated that contains all the controls on a form (or other container controls such as frames).

    I'd be surprised if it doesn't work with names, eg:
    Form1.Controls("Combo1").text
    or:
    Frame1.Controls("Combo1").text


    You should also consider using normal control arrays, just create one combo with all the properties you want, set visible to false, and set the index to 0. Then you can add more by number, eg:

    (assumes first control is named cboControlArray, with an index of 0)
    VB Code:
    1. Load cboControlArray(1)
    2. cboControlArray(1).Left = ...
    3. cboControlArray(1).Top = ...
    4. cboControlArray(1).Visible = True
    5.  
    6. Dim i as Integer
    7. For i = 2 to 10
    8.   Load cboControlArray(i)
    9.   With cboControlArray(i)
    10.     .Left = 300
    11.     .Top = .Height * i
    12.     .Visible = True
    13.   End With
    14. Next i

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    How I wish I can use control array.. however VBA doesn't seems to support this. *SIGH*

    And controls added runtime can be reference only thru this method..

    // from the help on Add method
    If you add a control at run time, you must use the exclamation syntax to reference properties of that control. For example, to return the Text property of a control added at run time, use the following syntax:

    userform1!thebox.text
    //

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    vba can be annoying.. no more ideas there I'm afraid

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    Originally posted by si_the_geek
    vba can be annoying.. no more ideas there I'm afraid
    Don't applogize, I should thank you for being so helpful and giving me so many ideas. I've tried the collection method and it works fine.

    Thanks once again!!

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    No worries

    by the way, if you add the key to the collection you can probably get the items by name, eg:

    myCombo.add AddCombo(frame1,combo_number) , ,"Combo" & combo_number
    (it may need to be just 1 comma, I cant remember!)

    msgbox myCombo("Combo1").Text

Posting Permissions

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



Click Here to Expand Forum to Full Width