Link to home
Start Free TrialLog in
Avatar of pander
panderFlag for Mexico

asked on

Easy CombBox Question.

Hi guys:

I have a ComboBox control and the style property is set to 2, so the user cannot write on it, this combo has all the States of and I filled up like this:

Do while not rsStates.Eof
     cbmStates.AddItem rsStates!StateName & space(50) & rsStates!StateId
     rsStates.MoveNext
Loop

So, in the combo has "California                                     CA"  and one of this for each state.

but I need to display the data from a table, at this point I cannot put any data on the combo because style 2 is read only.

If my table has Born in Texas, I need to put "Texas" on the combo.

I guess, I need to use the listindex property but how to assing it.

Thank you guys.

Avatar of lunchbyte
lunchbyte

This is a routine that I use

Add the below routine to your module or class

on your form load or where ever you populate your combo box do this.

Combox_Listindex_Selector cbmStates, rsStates!StateName



Public Sub Combox_Listindex_Selector(ctrl As ComboBox, str As String, Optional no_right_trim As Boolean = False)
    Dim i As Long
   
    ctrl.ListIndex = -1
   
    If Trim(str) <> "" Then
        For i = 0 To ctrl.ListCount
       
            If no_right_trim Then
                If UCase(ctrl.List(i)) = UCase(Trim(str)) Then
                    ctrl.ListIndex = i
                    Exit For
                End If
            Else
                If UCase(Trim(Right(ctrl.List(i), 10))) = UCase(Trim(str)) Then
                    ctrl.ListIndex = i
                    Exit For
                End If
            End If
        Next i
    End If



End Sub
I do the same thing as you do which is to load the name and hide the code deep in the string. the routine that I gave you will ignore the hidden code so that is why I have the no_right_trim parameter. This routine will work with any combo box with style set to 2.
Avatar of pander

ASKER

Thank you Lunch,

I put this code on the General - Declaration Section of my form:

Combox_ListIndex_Selector cmbStates, rsCatEstadosUsa!Estado

But when I try to run it,  appears an compile error: "Invalid outside procedure"  in the cmbStates.
>>General - Declaration Section of my form

No, put it in module or class. If you want it on your form then put it on the bottom. I think you have it on the very top where you declare global variables.
ASKER CERTIFIED SOLUTION
Avatar of lunchbyte
lunchbyte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pander

ASKER

I allready put the code in the module where I have global variables, I don't know if you mean that module,  I must doing something wrong because I'm getting the same error.

Regards,
OK, Let me find out where you are but first are you new to VB? Just now learning VB6? I do not want to give you instructions only to insult you if you much more advance.
Avatar of pander

ASKER

oh no, I allready made some apps. I like to use listboxes because the user doesn't have to use the mouse, but I think I'm going to put the following and it solves the question:

On keyPress Event

Keycode = 0

end sub.

Thank you lunch.

regards,