Limit to list on a combo box

How can i make it impossible for a user to type in their own text in a combo box. I only want them to be able to click to enter a given choice. Access has a limit to list  option what do I do for VB 6.0??? Another thing, I would like to do is to allow the user to type in the first letter and the list would move to that word if possible.
techconceptsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
Set the Style property to "Dropdown list". It will change the combo behavior to respect both conditions.
0
CraigLazarCommented:
to do a search on the list like you want use the following code

Public Function cboSelString(pComboListBox As Object) As Long
'this function works like a quick fill in Quicken. This function is called
'in a change event of a combobox and finds the item in the list based on the
'first characters that the user types. If it finds a match, the user's input
'is left unhighlighted while the remaining characters are highlighted.

   Dim nIndex As Long
   Dim cSearch As String
   Dim cFound As String
   
   ' get the contents in input box
   cSearch = LTrim(pComboListBox.Text)    

   nIndex = SendMessage(pComboListBox.hwnd, CB_FINDSTRING, -1, _
                        cSearch)

   If (nIndex <> -1) Then           ' -1 means found nothing
      pComboListBox.ListIndex = nIndex 'display the found item
      cFound = pComboListBox.List(nIndex)
      If (Len(cFound) >= Len(cSearch)) Then 'highlight remainder
         pComboListBox.SelStart = Len(cSearch)
         pComboListBox.SelLength = Len(cFound) - Len(cSearch)
      End If
   End If
   cboSelString = nIndex     'returns -1 if not found, else returns index
End Function


this piece of code i prefer and found it works better
    If CSearchSurname.SelLength = 0 Then
        findstring = CSearchSurname.Text & Chr$(KeyAscii)
    Else
        findstring = Left$(CSearchSurname.Text, CSearchSurname.SelStart) & Chr$(KeyAscii)
    End If
    CB = SendMessage(CSearchSurname.hwnd, CB_FINDSTRING, -1, ByVal findstring)
    If CB <> CB_ERR Then CSearchSurname.ListIndex = CB
        CSearchSurname.SelStart = Len(findstring)
        CSearchSurname.SelLength = Len(CSearchSurname.Text) - CSearchSurname.SelStart
       

        srchstr = CSearchSurname
        Data1.Recordset.FindFirst "Surname=" & """" & srchstr & """"
       
    KeyAscii = 0

hope it helps

:)

Craig
0
techconceptsAuthor Commented:
I am populating the combobox with the following code...

    Set rs = db.OpenRecordset("select Country from Country")
    Country.Clear
 
    If rs.RecordCount <> 0 Then
        Do While Not rs.EOF
        Country.AddItem rs("Country")
           rs.MoveNext
        Loop
    End If
If I use the style 2 combo list I get an error that it is a read only field. I am not sure how to use the code you provided with this type of method of populating the combo box. Could you take another look at it please?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Éric MoreauSenior .Net ConsultantCommented:
Which combobox do you use exactly (there are at least 3 in VB6)?
0
TheAnswerManCommented:
Use Style 2-Dropdownlist for combo.

If you are getting an error saying combo1.text = "Jimmy Johnson" then it is not spelled the same as in the list box.
they must be EXACT for it to find it.

no leading ... no trailing spaces.
   

No "Jim Johnson"
No "JimmyJohnson"
No " Jimmy Johnson"
No "Jimmy Johnson "

Dim ls_MyVal as string
ls_MyVal = "Jimmy Johnson "
Combo1.text = Trim$(ls_MyVal)


As to the Finding the First...
Combo will automatically find the first item if the combo has the focus and you press a key....if you have it set to Style = 2 - Dropdownlist

0
techconceptsAuthor Commented:
The combobox that I am using is the standard one that appears when you start a standard exe project. TheAnswerman, I tried type 2 and it does work as far as searching the list but with the way I populate and move about in the record I get numerous this is a read only text field. I get it when I move forward and backward thru the recordset. There are times when I need to set the field.clear or field.text="" . I am at a lost for the best way to do this. The combo box's data must be of dynamic data ability (ie. list edited or added to via another form at the time of running the application) such as adding more countries. I am sure your answer is not totally wrong, but it doesnt seem to work with the method of my application. Is there a better combobox that I should be using? or a better way to populate the list?
0
techconceptsAuthor Commented:
emoreau,

how would you set the combobox up? Including populating the list? Would you do it as I have shown in the code above? Should I be using a data control to populate instead, and if so, how should I do that?
0
Éric MoreauSenior .Net ConsultantCommented:
>If I use the style 2 combo list I get an error that it is a read only field.

This is one of the property that you absolutely have to set at desing time.

I have just started a new project, added a combobox to the form, changed the Style property to 2 and add the following code:
Private Sub Form_Load()
    With Combo1
        .AddItem "1 item"
        .AddItem "2 item"
        .AddItem "3 item"
    End With
End Sub

Make this test and come back with any problems.
0
dishaCommented:
use style 2 combobox.
make sure u dont use combo1.text anywhere to set the item since this is not possible if u r using this style. use the additems to add the new item and use list and listindex preferably to retreive the items, avoid using .text property

Regds
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
techconceptsAuthor Commented:
could you provide a code snipet to achieve this?
0
dishaCommented:
for adding in items use the code as emoreauo....
for retrieving items...use
combo2.list(combo2.listindex) or similiar
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.