Solved

load selected data into the listbox

Posted on 2003-10-30
13
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

752 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