Solved

Easy CombBox Question.

Posted on 2006-10-30
8
206 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
  • 5
  • 3
8 Comments
 
LVL 5

Expert Comment

by:lunchbyte
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:lunchbyte
Comment Utility
>>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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Accepted Solution

by:
lunchbyte earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now