VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Gets the ListIndex number from given Text in given ComboBox with starting and ending position speci

by Dipen Anovadia (19 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sun 21st August 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Gets the ListIndex number from given Text in given ComboBox with starting and ending position specified

Rate Gets the ListIndex number from given Text in given ComboBox with starting and ending position speci



'and ending to given values for any ComboBox
'IN: cb > ComboBox from which index is to be searched
' sTxt > Text to search in ComboBox
' lStart > Starting position (0 = From First)
' lEnd > Ending position (<0 is ComboBox.ListCount)
'OUT: lIDReturn > The ListIndex value which was found
'Return: True for success else False
'Tip: If Return is False and lIDReturn <> -1 Then lIDReturn
'     is the error number, so that you can display error
'     message later on if you wish.
Public Function IndexFromText( _
        cb As ComboBox, sTxt As String, _
        lIDReturn As Long, lStart As Long, _
        lEnd As Long) As Boolean

'Handle the error to avoid illegal termination of program
On Error GoTo errIFT

'Variable declaration
    Dim l As Long, lBegin As Long, lFinish As Long
    Dim sItem As String, lCurID As Long
    Dim lStep As Long, lCount As Long

'Initialization
    IndexFromText = False
    lIDReturn = -1

    lBegin = lStart: lFinish = lEnd
    lCount = cb.ListCount - 1

'Validating starting and ending position
    If lBegin < 0 Then lBegin = 0
    If lFinish > lCount Then lFinish = lCount
    If lFinish < 0 Then lFinish = lCount
'Priority-wise
    If lBegin > lFinish Then lStep = -1 Else lStep = 1

'Loop for verification
    l = 0
    For l = lBegin To lFinish Step lStep
        sItem = cb.List(l)
        If sItem = sTxt Then
            IndexFromText = True
            lIDReturn = l
            Exit Function
        End If
    Next l

    Exit Function
'Reply the error and failure both at a time
errIFT:
    lRetVal = Err.Number
    IndexFromText = False
    Err.Clear
    Exit Function
End Function

Download this snippet    Add to My Saved Code

Gets the ListIndex number from given Text in given ComboBox with starting and ending position speci Comments

No comments have been posted about Gets the ListIndex number from given Text in given ComboBox with starting and ending position speci. Why not be the first to post a comment about Gets the ListIndex number from given Text in given ComboBox with starting and ending position speci.

Post your comment

Subject:
Message:
0/1000 characters