Solved

Easy CombBox Question.

Posted on 2006-10-30
8
265 Views
Last Modified: 2010-04-30
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.

0
Comment
Question by:pander
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 5

Expert Comment

by:lunchbyte
ID: 17836148
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
0
 
LVL 5

Expert Comment

by:lunchbyte
ID: 17836319
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.
0
 

Author Comment

by:pander
ID: 17837434
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.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 5

Expert Comment

by:lunchbyte
ID: 17837468
>>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.
0
 
LVL 5

Accepted Solution

by:
lunchbyte earned 250 total points
ID: 17837529
should look something like this.


Option Explicit


Private Sub Form_Load()

end sub

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
0
 

Author Comment

by:pander
ID: 17846382
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,
0
 
LVL 5

Expert Comment

by:lunchbyte
ID: 17849079
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.
0
 

Author Comment

by:pander
ID: 17853201
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,
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question