Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual Basic Combobox Limit to List Question

Status
Not open for further replies.

michellecole

Programmer
Feb 12, 2004
42
0
0
US
Is there a property in VB for a combobox similar to MS Access where you can select LimitToList or does one need to write code to validate what was entered in the combobox on a particular event?

 
The default "Style" for a combo box is "0-Dropdown Combo".

By changing it to "2-DropDown List", you will get a combo box that you cannot type in. You will only be able to select items from the list.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
When I change the style, the user is no longer able to type the value manually. They are entering errors codes which are only 4 digits so I want to allow them to quickly type the code in if they know it but select from the list if they don't. Is this possible?
 
Add some items to the combo and also add code to the validate event, to validate the entry...
 
Please read thread222-757158. I personally prefer the solution that DrJavaJoe provided.
 
Even though autocomplete is an option, I would let the user type any code that they want, even error codes, and will validate them before I send it to the database.

You can use the Len() function to check the length of the string.

If you are sending this to a database, I wont mind what ever they type, because if the code is wrong no results are displayed anyway.

You can also use the Keypress or KeyDown events of the combobox to limit user input to just alphanumerals.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Thank you for your responses! I tried them out and here are the solutions I settled on to allow the user to type the code, auto expand and limit to list:

To allow for a Drop Down - this was provided by thread222-953023:

On Form:
Private Sub cboErrorCode_KeyDown(KeyCode As Integer, Shift As Integer)
DropDownCombo cboErrorCode
End Sub

In a Module:
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long

Public Sub DropDownCombo(cbo As ComboBox)
Const CB_GETDROPPEDSTATE = &H157
Const CB_SHOWDROPDOWN = &H14F
If Not CBool(SendMessage(cbo.hwnd, CB_GETDROPPEDSTATE, ByVal 0, ByVal 0&)) Then
SendMessage cbo.hwnd, CB_SHOWDROPDOWN, True, ByVal 0&
End If
End Sub

To allow for Limit to List Validation - this was provided by VLADK (sorry - I don't remember the thread maybe he can add it?:

Private Sub cboErrorCode_Validate(Cancel As Boolean)
Dim i As Integer
Dim blnListItemMatch As Boolean

For i = 0 To cboErrorCode.ListCount - 1

If cboErrorCode.Text = cboErrorCode.List(i) Then
blnListItemMatch = True
Exit For
End If

Next i

Cancel = Not blnListItemMatch

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top