Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

load selected data into the listbox

Posted on 2003-10-30
13
Medium Priority
?
378 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 800 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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