Solved

Need to deselect items in multi select list box in Access

Posted on 2014-09-03
6
752 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
ID: 40301891
<<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)
ID: 40301911
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
ID: 40303609
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
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.  

 
LVL 1

Expert Comment

by:itprojks99
ID: 40303685
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
ID: 40304318
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
ID: 40304380
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
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.

861 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