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
Printable View
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
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
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?
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
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