?
Solved

VB6 Question

Posted on 2007-07-25
4
Medium Priority
?
273 Views
Last Modified: 2010-04-30
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>"
0
Comment
Question by:Murray Brown
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 2000 total points
ID: 19565760
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
 

Author Comment

by:Murray Brown
ID: 19565868
Great answer. Thanks very much
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 19565915
You're welcome.  And thanks for the points and the good grade.  I just crossed the 1M total point mark.  :)
0
 

Author Comment

by:Murray Brown
ID: 19572499
Glad I was the person to have posed your 1M question. That's pretty impressive stuff
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question