Click to See Complete Forum and Search --> : combo box


Pat S
September 17th, 2001, 03:17 PM
I have a combo box that displays an id number and a name. I need to just be able to read the id number in an if statement. Ex.
If cmbSite.text = 7 then etc.
Obviously this does not work, because it tries to read the whole box.
Anybody...Thanks

michi
September 17th, 2001, 03:26 PM
Hi,

To get only the number part of a string (cmbSite.text), you can use Mid function. But I prefer using the ItemData property to hold the IDs. For example:

===
With cmbSite
.Clear

.AddItem ("Site1")
.ItemData(.NewIndex) = 7

.AddItem ("Site2")
.ItemData(.NewIndex) = 8


' You can add more
End With

===

Then, to read the id, you can use the following statement:

If cmbSite.ItemData(cmbSite.ListIndex)= 7 then etc.

Hope this helps.



Regards,

Michi

Pat S
September 17th, 2001, 03:38 PM
Thanks for your help.
But, here's the deal--the combo box is populated by adding sites into the database from another form in the program. Will this work with that?

michi
September 17th, 2001, 03:49 PM
The site name and site ID in the codes can be replaced by database field values. You can create an ADO record set to loop through.

Regards,

Michi

MKSa
September 17th, 2001, 04:54 PM
The code below show how to extract the ID from the combo text property. You should add some error handling and tighten the contraints but the code below should get you started.

private Sub Command1_Click()
If GetID <> -1 then
MsgBox GetID
End If
End Sub
Function GetID() as Long
Dim ID as string, Islong as Boolean, Index as Long
Index = 0
Islong = IsNumeric(Left(LTrim(Combo1.Text), Index + 1))
While Islong
Index = Index + 1
Islong = IsNumeric(mid(LTrim(Combo1.Text), Index + 1, 1))
Wend
If Index > 0 then
GetID = CLng(LTrim(Left(Combo1.Text, Index)))
else
GetID = -1
End If
End Function