Solved

Easy CombBox Question.

Posted on 2006-10-30
8
218 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

929 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

11 Experts available now in Live!

Get 1:1 Help Now