Reputation: 683
Is this the right way to fill the combobox?
On form combox are named as cmbType1
,cmbType2
,cmbType3
... and so on.
I am using following kind of logic to initialise the combo box:
Private Sub fillCombo(count As Integer)
Dim cmbControl As Object
For i = 1 To count
Set cmbControl = Me.Controls.item("cmbType" + CStr(i))
cmbConnectorTypeControl.AddItem ("ABC")
Next i
End Sub
So I just want cross verify this that is this the correct style of coding?
Any suggestions are most welcome...
Upvotes: 2
Views: 141
Reputation: 175876
You can skip needing to specify the initial count;
fillCombo "cmbType"
...
function fillCombo(name)
dim ctrl as Control
for each Control in Me.Controls
if typeof Control is ComboBox then
if left$(Control.name, len(name)) = name then
Control.additem "ABC"
end if
end if
next
end function
Upvotes: 0
Reputation: 9726
Alex is correct. If you have your combo boxes already on the form and your generic fillCombo method works then try changing the method to take a ComboBox as the parameter. It will save the hit for doing the look up and simplify the code. I assume you are not really adding "ABC" to all the comboboxs up to the count, but even if you are I would rather call this method in a loop.
Private Sub fillCombo(ByVal vCombobox As ComboBox)
vCombobox.AddItem ("ABC")
End Sub
Upvotes: 2