Tools Links Login

Select List Box or Combo Box Value by Index

This function will select the value of a List Box or Combo Box based upon the Index ID. This is helpful when you are trying to edit a record and want to select a saved value in a combo box or list box.

Original Author: Cierra Computers & Consulting

API Declarations

Public Enum CtlType
ListBox
ComboBox
End Enum

Code

Public Sub SelectInList(varID As Variant, ctlList As Control, Optional ctl As CtlType, _
   Optional blnRefresh As Boolean = True)
'Selects the Item in List or Combo Box that matches passed varID
Dim x
If Not IsNull(varID) Then
   varID = CLng(varID)
    
   If blnRefresh = True Then
     ctlList.Refresh
   End If
  
   For x = 0 To ctlList.ListCount - 1
     If ctlList.ItemData(x) = varID Then
        If ctl = ListBox Then
          ctlList.Selected(x) = True
        Else
          ctlList = ctlList.List(x)
        End If
        Exit Sub
     End If
   Next
Else
   'Reset the ComboBox
   ctlList.ListIndex = -1
End If
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 108 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.