VB6 Question

Please would explain the following code to me, especially NewIndex

     'Get all AccommodationType detail for combo
    cboTypeId.AddItem "<Not Selected>"
    cboTypeId.ItemData(cboTypeId.NewIndex) = 0
    Dim rsTmp As ADODB.Recordset
    m_oNonTransaction.GetRecordset rsTmp, "Select * from AccommodationTypes"
    If rsTmp.RecordCount Then
        rsTmp.MoveFirst
        While Not rsTmp.EOF
            cboTypeId.AddItem rsTmp!AccommodationType
            cboTypeId.ItemData(cboTypeId.NewIndex) = rsTmp!AccommodationTypeID
           
            rsTmp.MoveNext
        Wend
    End If
    cboTypeId.Text = "<Not Selected>"
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
PaulHewsCommented:
The combo box allows you to add string items that show up in the dropdown list using .AddItem.  However, it's a frequent need to have some other data associated with the selection, such as a numeric key.  For that you can use the .ItemData property.  Each item added to a combo box has it's own ItemData element.


.NewIndex allows you to retrieve the index of the last item added to the combo box.  So this code:

cboTypeId.AddItem "<Not Selected>"
cboTypeId.ItemData(cboTypeId.NewIndex) = 0

Adds a new item, and sets itemdata (for the newly created item) to zero

This code:
While Not rsTmp.EOF
    cboTypeId.AddItem rsTmp!AccommodationType
    cboTypeId.ItemData(cboTypeId.NewIndex) = rsTmp!AccommodationTypeID
   
    rsTmp.MoveNext
Wend

Loops through a recordset, adding new string items based on the AccommodationType field, then adding ItemData for the new element set to the corresponding AccommodationTypeID.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great answer. Thanks very much
0
 
PaulHewsCommented:
You're welcome.  And thanks for the points and the good grade.  I just crossed the 1M total point mark.  :)
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Glad I was the person to have posed your 1M question. That's pretty impressive stuff
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.