Microsoft KB Archive/266265

= How To Implement a MatchedWithList Validation Check in a ComboBox Control =

Article ID: 266265

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q266265



SUMMARY
A simple ComboBox control whose Style property has been set to 0-Dropdown Combo permits a user to manually make an entry into its text edit portion without selecting a value displayed in its drop-down list. The simple ComboBox control does not expose a Boolean property (identical to the MatchedWithList property of the OLEDB DataCombo control) that can be used to determine if the value that is keyed into the edit portion matches one of the elements in the list. This is an important validation check that can be very useful when using a simple drop-down ComboBox.

A generic, reusable routine can be written by using Visual Basic code to implement this validation check for a simple drop-down ComboBox control. This article documents the reusable code that can be used to address this requirement.



Code to Implement the MatchedWithList Validation Check
Copy-and-paste the following code into the Change event procedure of a simple drop-down ComboBox (whose Style has been set to 0-Dropdown Combo for which you wish to implement the MatchedWithList validation check. You can modify the message to be displayed to the user when the check fails. The validation check fails when a character that is keyed into the edit portion results in a string that does not match an initial substring of any of the elements in the list portion. The initial substring that is extracted from each of the list elements comprises of the first &quot;N&quot; characters, &quot;N&quot; being the length of the string in the edit portion of the ComboBox.

Dim listcount As Integer Dim textlen As Integer Dim matchexists As Boolean

textlen = Len(Combo1.Text) For listcount = 0 To Combo1.listcount - 1 If UCase(Mid(Combo1.List(listcount), 1, textlen)) = UCase(Combo1.Text) Then matchexists = True Exit For End If Next

If Not matchexists Then MsgBox &quot;Value not present in the list... Kindly enter a valid value..&quot; End If

Steps to Test the Routine
 Create a new standard EXE project in Visual Basic. Form1 is created by default. Add a ComboBox control to Form1 from the toolbox.  Cut-and-paste the following code into the Form module: Private Sub Combo1_Change

Dim listcount As Integer Dim textlen As Integer Dim matchexists As Boolean

textlen = Len(Combo1.Text) For listcount = 0 To Combo1.listcount - 1 If UCase(Mid(Combo1.List(listcount), 1, textlen)) = UCase(Combo1.Text) Then matchexists = True Exit For End If Next

If Not matchexists Then MsgBox &quot;Value not present in the list... Kindly enter a valid value..&quot; End If

End Sub

Private Sub Form_Load Combo1.AddItem &quot;Sam&quot; Combo1.AddItem &quot;Paul&quot; Combo1.AddItem &quot;Peter&quot; Combo1.Text = &quot;&quot; End Sub  Run the project, and then type in appropriate values in the edit portion of the ComboBox to test the routine.

For example, try typing the following to evaluate the outcome of the validation:

Same, Pater, Paul, Paul

Note that in the first three cases, the validation message pops up as soon as the first character (e,a, and d) that results in a NoMatch is keyed into the edit portion.

Keywords: kbhowto KB266265

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.