G
gxdata
I have a mental block about this, and can't find a ready solution. I'm sure
it's very simple.
I have an unbound listbox into which the user adds items selected from two
other controls (combos - cbPoint1, cbPoint2). The listbox has 4 columns,
essentially Point1_ID, Point1_Name, Point2_ID, and Point2_Name.
What I need to do is make sure that the user doesn't choose the same
combination (Point1_ID, Point2_ID) more than once and try to add it to the
listbox - so I need to loop through the existing items in the listbox and
read the contents of the 4 columns. The logic to exclude a candidate for the
listbox is simple enough.
I've tried a bit of code to set the .ListIndex proprty when nothing has been
chosen in the listbox, and then return the .Column(i) values. I get error
"You've used the ListIndex property incorrectly."
' here, n1, n2 are the Point1_ID, Point2_ID values.
Function InList(n1 As Integer, n2 As Integer) As Boolean
On Error GoTo ErrFN
Dim lng As Long , frm As Form
Set frm = Forms!frmPoints2
For lng = 0 To frm!listChosen.ListCount - 1
Debug.Print "ListIndex = "; frm!listChosen.ListIndex
Forms("frmPoints2").Controls("ListChosen").ListIndex = lng ' ERROR
here
' frm!listChosen.ListIndex = i
If frm!listChosen.ItemData(lng).Column(0) = n1 _
And frm!listChosen.ItemData(lng).Column(2) = n2 Then
InList = True
Debug.Print "Gotcha"
Exit Function
End If
Next
Exit_Here:
Exit Function
ErrFN:
Debug.Print Err.Description
Resume Exit_Here
End Function
TIA - Ian Thomas
it's very simple.
I have an unbound listbox into which the user adds items selected from two
other controls (combos - cbPoint1, cbPoint2). The listbox has 4 columns,
essentially Point1_ID, Point1_Name, Point2_ID, and Point2_Name.
What I need to do is make sure that the user doesn't choose the same
combination (Point1_ID, Point2_ID) more than once and try to add it to the
listbox - so I need to loop through the existing items in the listbox and
read the contents of the 4 columns. The logic to exclude a candidate for the
listbox is simple enough.
I've tried a bit of code to set the .ListIndex proprty when nothing has been
chosen in the listbox, and then return the .Column(i) values. I get error
"You've used the ListIndex property incorrectly."
' here, n1, n2 are the Point1_ID, Point2_ID values.
Function InList(n1 As Integer, n2 As Integer) As Boolean
On Error GoTo ErrFN
Dim lng As Long , frm As Form
Set frm = Forms!frmPoints2
For lng = 0 To frm!listChosen.ListCount - 1
Debug.Print "ListIndex = "; frm!listChosen.ListIndex
Forms("frmPoints2").Controls("ListChosen").ListIndex = lng ' ERROR
here
' frm!listChosen.ListIndex = i
If frm!listChosen.ItemData(lng).Column(0) = n1 _
And frm!listChosen.ItemData(lng).Column(2) = n2 Then
InList = True
Debug.Print "Gotcha"
Exit Function
End If
Next
Exit_Here:
Exit Function
ErrFN:
Debug.Print Err.Description
Resume Exit_Here
End Function
TIA - Ian Thomas