Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Cobobox Add Item giving error. Please help !

Status
Not open for further replies.

sergiog

Programmer
Feb 17, 2003
24
MT
hi I have to following code to fill in a combobox with the items from a recordset. I want to show the Description and use the iD as an idex for each item.

The folloeing code gives me an error "Invalid Procedure Call or Argument" on the bold line.

If Not rst.EOF And Not rst.BOF Then
rst.MoveFirst

Do Until rst.EOF
With cmbVacType
.AddItem rst("Description"), rst("ID")
End With
rst.MoveNext
Loop
End If

any ideas please ?
thanks
 
Hi

i think the index property must be within the number of items in combobox, so if you set the index to 5 and you only have 2 items, the error occurs. Index is only for setting ordinal position of an item in the combobox list.

Hope this helps
Mangro
 
yeah, good thinking. thanks

but how can i set the index to the ID from the table ? because i need to use that index while saving.

thanks
 
Check the DataCombo control and its BoundColumn and ListField properties. In your case BoundColumn would be set to "ID" and ListField to "Description".

Mangro
 
You can assign a number to the Combo's ItemData property.
Then you can loop through the combo elements until you find an element with an ItemData matching your search, or, when an item is selected, take the ItemData value for your update.
 
Ok CClint, it worked for filling up the combo box. thanks.

However, I need to add the combo box to a bindingcollection. when i try adding the Itemdata property it gives me an error "Need property array index".

should I bind the listindex instead, but I think it won't be right.

thanks again
 
ok more easy way is to make query in the db and sort it by index doing what u want and then make a datacontrol pointing to that qury and make the source of the combo pointing 2 the datacontrol
 
Yes but I am not using the datacontrol and I don't want to coz i prefer doing things myself in code.

any other suggestions please ?
 
Are you working with an Access table?, is ID an autonumber?
If thats the case then the best route is using SQL to query the table and establish a recordset for putting in your combo. Use the Itemdata property to put the ID value in. In your SQL query, establish the sort order you are wanting items to appear in the combo, ie ID order or "description".
Always consider the fact that records will get deleted later, and Autonumbers do not replace themselves, ie if ID 3 deleted, then ID 3 will not appear again.
If ID is an autonumber, then by virtue of adding a record in the table, the ID value will get entered automatically by the system.
Later, to select an item from the combo box, you do a loop through the records to match the ID value of the itemdata in the combo box to find the record. Again, not knowing if the ID is an autonumber, you should never use it as a visible/usable index value, as it can become meaningless by virtue of how the system derives an autonumber. You can get big increments for autonumber values at times, which related to the items you have in your table don't match. Hope I have not confused you by this, and its relevent to your querie. Regards

 
Hi ZOR, thanks for the reply.

Yes my field is an autonumber and I did use a recordset to populate the combobox and it worked. so far i knew how to do it.

My problem is that I am adding the combobox to a bindingcollection and where I have the code
.add cmbVacTypes, "ItemData", "VacTypeID" I get an error "Need Property Array index".

thanks
sergio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top