|
-
Oct 7th, 2003, 02:37 AM
#1
Thread Starter
Junior Member
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
-
Oct 7th, 2003, 03:53 AM
#2
Try this instead:
VB Code:
'In General Declarations
Dim myCombo as ComboBox
'in form load (or where you want!)
Set myCombo = AddCombo
'your functions
Private Function AddCombo() as ComboBox
'Use STATIC rather than DIM, so that next time the number wont reset
Static line as Integer
Set AddCombo = 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
'
myCombo.Visible = False
:
:
End Sub
-
Oct 7th, 2003, 04:14 AM
#3
Thread Starter
Junior Member
Originally posted by si_the_geek
Try this instead:
VB Code:
'In General Declarations
Dim myCombo as ComboBox
'in form load (or where you want!)
Set myCombo = AddCombo
'your functions
Private Function AddCombo() as ComboBox
'Use STATIC rather than DIM, so that next time the number wont reset
Static line as Integer
Set AddCombo = 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
'
myCombo.Visible = False
:
:
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.
-
Oct 7th, 2003, 05:00 AM
#4
In that case you need to use a collection or an array.
Here's an example using a collection:
VB Code:
'In General Declarations
Dim myCombo as Collection
'in form load (or where you want!)
Set myCombo = New Collection
Dim combo_number as Integer
For combo_number = 1 to 5
myCombo.add AddCombo(frame1,combo_number)
Next combo_number
'your functions
Private Function AddCombo(Parent_Control as object, line as Integer) as ComboBox
Set AddCombo = Parent_Control.controls.add("Forms.ComboBox.1", "Combo" & line, true)
End Function
Private Sub CommandButton1_Click()
' This is where i am having problems
Dim combo_number as Integer
For combo_number = 1 to 5
myCombo(combo_number).Visible = False
Next combo_number
'OR
Dim tmpCombo
For Each tmpCombo in myCombo
tmpCombo.Visible = False
Next tmpCombo
:
:
End Sub
-
Oct 7th, 2003, 09:43 AM
#5
Thread Starter
Junior Member
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!!
-
Oct 7th, 2003, 09:52 AM
#6
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:
Load cboControlArray(1)
cboControlArray(1).Left = ...
cboControlArray(1).Top = ...
cboControlArray(1).Visible = True
Dim i as Integer
For i = 2 to 10
Load cboControlArray(i)
With cboControlArray(i)
.Left = 300
.Top = .Height * i
.Visible = True
End With
Next i
-
Oct 7th, 2003, 10:11 AM
#7
Thread Starter
Junior Member
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
//
-
Oct 7th, 2003, 10:40 AM
#8
vba can be annoying.. no more ideas there I'm afraid
-
Oct 7th, 2003, 08:54 PM
#9
Thread Starter
Junior Member
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!!
-
Oct 8th, 2003, 03:44 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|