Solved

vb6 Combo box Problem

Posted on 2014-09-18
5
149 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
  • 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 45

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 45

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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.
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 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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now