Additional items appearing in list box

Posted on 2011-05-11
Last Modified: 2012-05-11

I am trying to populate a list box with selections made in another list box.  However, when I execute the code below, the number one appears at the top of the list in addition to all the other selections.  For example, if I select Apple from the source listbox, the number 1 is listed above the Apple entry in the destination list box.  
Private Sub cmdSelectIndividual_Click()
    Dim varSelectedItem As Variant
    Dim x As Integer
    Me.lstDestinationFields.RowSource = vbNull
    If Me.lstSourceFields.ItemsSelected.Count > 0 Then
      For x = 0 To Me.lstSourceFields.ListCount - 1
        If Me.lstSourceFields.Selected(x) Then
            Me.lstDestinationFields.AddItem Me.lstSourceFields.ItemData(x)
        End If
    End If
End Sub

Open in new window

Question by:chtullu135
    LVL 75

    Accepted Solution

    One thing for sure ... change  this

    Me.lstDestinationFields.RowSource = vbNull


    Me.lstDestinationFields.RowSource = ""

    since the RowSource is a String property.

    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    Since you did not post any details on the design of this system or what the rowsources are...
    ...All I can do is post a sample of how I do this...


    Author Closing Comment

    Thanks that did it
    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    Interesting ....
    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    From Help:

    VarType Constants

    The following constants can be used anywhere in your code in place of the actual values:

    Constant Value Description
    vbEmpty 0 Uninitialized (default)
    vbNull 1 Contains no valid data

    Pretty clear now what happened.  Setting RowSource to vbNull essentially added one item ... a '1' ... and then the AddItem method added the remaining items.

    Who would have thought ...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This isn't a frequent question on EE. I must have seen it three or four times (among several thousand questions). However, I use this trick quite often, most frequently as a delayed Current event. A form does not expose it's calculation dependenc…
    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    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…
    In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now