Solved

vb6 Combo box Problem

Posted on 2014-09-18
5
156 Views
Last Modified: 2014-09-18
I have a type into combo box
When a user types a name I want to check if its not already been added if not
then add it and make it selected:
This does not work:
   
Dim i As Integer
    Dim CboText As String
    CboText = Me.cboBids.Text
    For i = 0 To cboBids.ListCount - 1
    If cboBids.List(i) = CboText Then
    cboBids.ListIndex = cboBids.List(i)
    Else
    cboBids.AddItem cboBids.Text
    cboBids.ListIndex = cboBids.List(i)
    End If
    Next

Open in new window

What is wrong ?
0
Comment
Question by:isnoend2001
[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
  • 2
  • 2
5 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 200 total points
ID: 40331217
You need to look through all the items until a match is found. If is not  found, then add then add the new item. This shows how to do it as a Sub procedure.
Private Sub AddText()
    Dim i As Integer
    Dim CboText As String
    CboText = Me.cboBids.Text
    For i = 0 To cboBids.ListCount - 1
        If cboBids.List(i) = CboText Then
            cboBids.ListIndex = cboBids.List(i)
            Exit Sub
        End If
    Next i
    cboBids.AddItem cboBids.Text
    cboBids.ListIndex = cboBids.List(i)
End Sub

Open in new window

0
 

Author Comment

by:isnoend2001
ID: 40331435
Thanks but  that gives type mismatch error
 Dim i As Integer
    Dim CboText As String
    CboText = Me.cboBids.Text
    For i = 0 To cboBids.ListCount - 1
        If cboBids.List(i) = CboText Then
            cboBids.ListIndex = cboBids.List(i)
            Exit Sub
        End If
    Next i
    cboBids.AddItem cboBids.Text
    cboBids.ListIndex = cboBids.List(i) 'type mismatch here

the cbo is empty if that matters
0
 
LVL 47

Accepted Solution

by:
Martin Liss earned 300 total points
ID: 40331606
I've corrected lines 8 and 13 in Graham's code and added an error message.

Private Sub AddText()
    Dim i As Integer
    Dim CboText As String
    CboText = Me.cboBids.Text
    For i = 0 To cboBids.ListCount - 1
        If cboBids.List(i) = CboText Then
            MsgBox CboText & " already exists"
            cboBids.ListIndex = cboBids.ListCount - 1
            Exit Sub
        End If
    Next i
    cboBids.AddItem cboBids.Text
    cboBids.ListIndex = cboBids.ListCount - 1
End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40331626
If your combobox is sorted then change the code to this.

Private Sub AddText()
    Dim i As Integer
    Dim CboText As String
    CboText = Me.cboBids.Text
    For i = 0 To cboBids.ListCount - 1
        If cboBids.List(i) = CboText Then
            MsgBox CboText & " already exists"
            cboBids.ListIndex = cboBids.ListCount - 1
            Exit Sub
        End If
    Next i
    cboBids.AddItem cboBids.Text
    For i = 0 To cboBids.ListCount - 1
        If cboBids.List(i) = CboText Then
            cboBids.ListIndex = i
            Exit For
        End If
    Next
End Sub

Open in new window

0
 

Author Closing Comment

by:isnoend2001
ID: 40331817
Thank you
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

732 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