Solved

load selected data into the listbox

Posted on 2003-10-30
13
367 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
 
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
IT, Stop Being Called Into Every Meeting

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!

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

11 Experts available now in Live!

Get 1:1 Help Now