Solved

vb6 Combo box Problem

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

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 46

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

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

930 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