Solved

vb6 Combo box Problem

Posted on 2014-09-18
5
159 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

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!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

729 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