Solved

load selected data into the listbox

Posted on 2003-10-30
13
370 Views
Last Modified: 2008-02-01
I have two list boxes lstSelected and lstAll. lstSelected is empty and lstAll if filled from the database.
Also there are two buttons "add" and "remove".
"add" button move selected items from lstAll to lstSelect, and "remove" removes selected items from lstSelect.

I would really appreciate some vba code
0
Comment
Question by:YZlat
13 Comments
 
LVL 7

Expert Comment

by:donpricejr
ID: 9653246
Go to the following URL: http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q233/3/24.asp&NoWebContent=1

There you will find an example, which uses drag and drop to populate multi-select combo boxes. You may adapt this to your needs.

-Don
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 200 total points
ID: 9653396
In your table include a check box field (Yes/No).  Call this SelectedYN (defaut is false).

lstAll , row source:
Select Name From MyTable Where SelectedYN = 0     '<--- lstAll will show everything in the beginning

With following code in its DobleClick event of lstAll:

CurrentDB.Execute "Update MyTable Set SelectedYN =-1 Where Name ='" & me!lstAll & "'"
RefrshLisBoxes

'-----------
lstSelected, row source:
Select Name From MyTable Where SelectedYN = -1     '<--- lstSelected will show nothing in the beginning

With following code in its DobleClick event of lstSelected:

CurrentDB.Execute "Update MyTable Set SelectedYN =0 Where Name ='" & me!lstSelected & "'"
RefrshLisBoxes

'----------
finally, paste this code in code window of your form:

Sub RefrshLisBoxes()
lstAll .RowSource=lstAll .RowSource
lstSelected.RowSource=lstSelected.RowSource
End Sub

Mike
0
 
LVL 11

Expert Comment

by:joekendall
ID: 9654068
Mike is right on it. That is what I have done in the past to implement your need.

Joe
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 35

Author Comment

by:YZlat
ID: 9657199
no, that's not what I need. I have two list boxes. one- lstAll I filled with fields from the database, the other one is empty. In between the list boxes I have two buttons "add" and "remove". What I want is when a user selects one or more items from the lstAll list box and clicks on "add", I want those items to be copied into the second list box lstSelected. "remove" button should delete the selected items from lstSelected
0
 
LVL 11

Expert Comment

by:joekendall
ID: 9658048
What Mike posted will do that.

Do you want them to appear in the lstAll if they are now in lstSelected?

Joe
0
 
LVL 35

Author Comment

by:YZlat
ID: 9658223
I don't want to add another database field. it has nothing to do with database. All I want is to copy selected items from one list box into another
0
 
LVL 11

Expert Comment

by:joekendall
ID: 9658303
Dim v As Variant
   
For Each v In lstAll.ItemsSelected
    lstSelected.RowSource = lstSelected.RowSource & lstAll.Selected(v) & ";"
Next

lstSelected.RowSource = Left$(lstSelected.RowSource,Len(lstSelected.RowSource)-1)

Joe
0
 
LVL 35

Author Comment

by:YZlat
ID: 9658305
Here is my code. It seems right but items don't get copied into lstSelected:

qrySQL = "SELECT Person.PerLastName + ',' + Person.PerFirstName AS FullName " & _
        "FROM Person WHERE Person.PersonID IN("
   
    'check if at least one item is selected
    If Me("lstAll").ItemsSelected.Count > 0 Then
        For Each lstItem In Me.lstAll.ItemsSelected
           
            ID = Me.lstAll.ItemData(lstItem)
         
          If Len(strTemp) = 0 Then
                strTemp = ID
            Else
                strTemp = strTemp & ", " & ID
            End If
           
        Next lstItem
         qrySQL = qrySQL & strTemp & ");"

         Me.lstAuthor.RowSource = qrySQL
       
    Else
        MsgBox "No items were selected!", vbOKOnly
        Exit Sub
    End If
0
 
LVL 11

Expert Comment

by:joekendall
ID: 9658311
We understand that you don't want to add another field to your table, but it is an easier way to do it since it can remain consistant and be used again for retrieval purposes.

Joe
0
 
LVL 35

Author Comment

by:YZlat
ID: 9658529
Joe, what's wrong with my code, why don't I get anything in lstSelected?

 'check if at least one item is selected
 
    If Me("lstAllPeople").ItemsSelected.Count > 0 Then
        For Each lstItem In Me.lstAllPeople.ItemsSelected
            ID = Me.lstAllPeople.ItemData(lstItem)
         
            qrySQL = "SELECT Person.PerLastName + ',' + Person.PerFirstName AS FullName " & _
                "FROM Person WHERE Person.PersonID = " & ID
            Set rst = db.OpenRecordset(qrySQL)
            If Not (rst.EOF And rst.BOF) Then
                AuthorName = rst(0)
            End If
            lstSelected.RowSource = lstSelected.RowSource & AuthorName & ";"
        Next

        lstSelected.RowSource = Left$(lstSelected.RowSource, Len(lstSelected.RowSource) - 1)

Else

End if
0
 
LVL 35

Author Comment

by:YZlat
ID: 9658746
OK, I changed lstSelected from list box to combo box cboSelected

and changed one line of code and now it's working

I changed Me.lstSelected.RowSource = qrySQL to
cboSelected.RowSource = qrySQL
0
 
LVL 35

Author Comment

by:YZlat
ID: 9658760
now the only thing is how do I remove an item from a combo box?
0
 
LVL 11

Expert Comment

by:joekendall
ID: 9659442
That is not an easy thing to do nor will it happen fast. That is why Mike suggested the additional field in the table. It makes it much easier to do.

Joe
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

777 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