Solved

Need to deselect items in multi select list box in Access

Posted on 2014-09-03
6
726 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
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.

 
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

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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