'AddComboItem
Public Sub AddComboItem( _ cboAdd As ComboBox, _ ByVal sText As String, _ ByVal lData As Long) cboAdd.AddItem sText cboAdd.ItemData(cboAdd.NewIndex) = lData End Sub 'CurrComboData Public Function CurrComboData( _ cbo As ComboBox) As Long If cbo.ListIndex <> -1 Then CurrComboData = cbo.ItemData(cbo.ListIndex) Else CurrComboData = -1 End If End Function I often need to add items to a ComboBox and store an index or ID value in the ItemData property. I've found that the code needed to add items to the ComboBox and to check the ItemData property of the currently selected item looks clumsy. So, I've written two simple helper routines to clean the code up a bit. Here they are: Now, instead of writing cboTest.AddItem "Hello" cboTest.ItemData(cboTest.NewIndex) = 5 you can just write AddComboItem cboTest, "Hello",5 Instead of writing ID = cboTest.ItemData(cboTest.ListIndex) you can write ID = CurrComboData( cboTest ) As an added bonus, CurrComboData protects you from the runtime error generated if ListIndex is -1. Just be sure to check for a return of -1 from CurrComboData. |