Solved

Need to deselect items in multi select list box in Access

Posted on 2014-09-03
6
649 Views
Last Modified: 2014-09-04
I have a multi select list box on a form that is used to choose items from a past report and add them to a current report.
There is a button that adds everything that is selected in the list box to a table, and then a second list box is updated with the information.

Everything works fine, but I need the selected items in the first list box to be cleared when the user clicks the button (btnAddSelected).

I am using
Me.lstprevRpts.Selected(I) =False

Open in new window

right now, but it is clearing the information that is added to the second list box.

Do I just have this command in the wrong place?

Private Sub btnAddSelected_Click()

Dim frm As Form
Dim ctl As Control


Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim i As Variant
Set frm = Forms("frm_Act_Enter")
Set ctl = frm![lstPrevRpts]

Set db = CurrentDb
Set rst = db.OpenRecordset("Act_SubTo_Date")



    For Each i In ctl.ItemsSelected
        rst.AddNew
'          Debug.Print i, ctl.Column(4, i)
            rst("ActID") = ctl.Column(4, i)
            rst("ActDate") = Nz(Me!ActDate.Value, Date)


        rst.Update

Next i

Me.lstPrevRpts.Selected(i) = False
        
rst.Close
Set rst = Nothing

Refresh

End Sub

Open in new window

0
Comment
Question by:Megin
6 Comments
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
<<Do I just have this command in the wrong place?>>

  Yes.   Your referencing the loop variable i outside of the loop.  You either:

A. Want  to move it inside the loop after you've added the record.

B. Write separate loops to write the records and clear the select.

 Right now, your only deselecting the last item.

Jim.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
Personally, I'd go with Jim's 2nd option and write a separate loop to clear the selections.

You might also want to have some check in there that prevents you from adding the same report to the "Act_SubTo_Date" table more than once.  You may already have a unique index in that table, but if you do, you should have an error handler in your procedure which will handle the error properly and not cause your application to bomb.
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
Comment Utility
Here are some code segments to select all or deselect all in a multi-select listbox' I usually place them on command buttons in the footer of the form with the listbox:
'Select all rows in listbox
Set lst = Me![lstSelectMultiple]
intRows = lst.ListCount - 1

For intIndex = 0 To intRows
   lst.Selected(intIndex) = True
Next intIndex

'Deselect all rows in listbox
Set lst = Me![lstSelectMultiple]
intRows = lst.ListCount - 1

For intIndex = 0 To intRows
   lst.Selected(intIndex) = False
Next intIndex

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:itprojks99
Comment Utility
My suggestion, here's a sub that I call from click event of the "clear" button named "cmdClr", where lstMultiSelect is the name of the list box:

Private Sub cmdClr_Click()
    ClearListBox
End Sub


Private Sub ClearListBox()
Dim iCount As Integer
       
    For iCount = 0 To Me!lstMultiSelect.ListCount
        Me!lstMultiSelect.Selected(iCount) = False
    Next iCount
   
End Sub
0
 

Author Closing Comment

by:Megin
Comment Utility
This seems to work just fine. I don't know about placing it in the footer of  the form. I added it to the end of my code, just before End Sub.  Seems to be working great.

Thanks!
0
 
LVL 31

Expert Comment

by:Helen_Feddema
Comment Utility
Here is a screen shot of a form where I use the code (on the Select All and Deselect All buttons):
Buttons to select all or deselect all
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

762 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

10 Experts available now in Live!

Get 1:1 Help Now