Solved

Easy CombBox Question.

Posted on 2006-10-30
8
255 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
Technology Partners: 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!

 
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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

752 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