?
Solved

vb6 Combo box Problem

Posted on 2014-09-18
5
Medium Priority
?
166 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 800 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 49

Accepted Solution

by:
Martin Liss earned 1200 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 49

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Suggested Courses

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