Help With Code

G

Guest

Hello,

I have some code that loops through a ListBox named "lstAdmin" to find all
selected items. What I want to do is de-select each selected item in the
ListBox after it's no longer needed.

I have used a Control reference "ctl" for de-selecting the selected items,
but I've been informed that I should get rid of "ctl" and just refer to
lstAdmin directly. As you can see below, my attempt to use lstAdmin directly
is not working.

How do I use lstAdmin directly to de-select the selected items? Your help on
this will be greatly appreciated.

Following is the code:

Dim rs As Recordset
Dim db As Database
Dim SelectedItem As Variant
Dim ctl As Control

Set db = CurrentDb
Set rs = db.OpenRecordset("StudentCourses", dbOpenDynaset)
Set ctl = Me.[lstAdmin]

For Each SelectedItem In lstAdmin.ItemsSelected
rs.AddNew
rs![Course_ID] = Me.lstAdmin.Column(0, SelectedItem)
rs![Employees_ID] = Me.cboEmp_Name.Value
rs("Doc_Number") = Me.lstAdmin.Column(1, SelectedItem)
rs.Update
'Me.lstAdmin.SelectedItem = False
ctl.Selected(SelectedItem) = False
Next
 
G

Guest

It is probably more efficient to simply clear all of the selection at once
when you're done with the set of selected items. Especially since you can't
(with your code) hit an item a second time.

After your Next statement add a line that says:

Me!lstAdmin.ItemsSelected = nothing

Good Luck!
 
G

Guest

Chaim,

Your line of code returned an error message but that's cool because after
reading through the help section I found something interesting. Here's a
quote from the help sections MultiSelect Property:

"If the MultiSelect property is set to Extended, requerying the list box
clears any selections made by the user."

Sure enough, I did the requery on lstAdmin and it cleared all the selected
items. Don't know why I had not noticed that line in help before, especially
since I had read that section a couple of times before.

Maybe you have some kind of magic with you Chaim that rubs off :)

Thanks Chaim for your input and your magic touch too!

-AKA Spawn

Chaim said:
It is probably more efficient to simply clear all of the selection at once
when you're done with the set of selected items. Especially since you can't
(with your code) hit an item a second time.

After your Next statement add a line that says:

Me!lstAdmin.ItemsSelected = nothing

Good Luck!
--
Chaim


AKA Spawn said:
Hello,

I have some code that loops through a ListBox named "lstAdmin" to find all
selected items. What I want to do is de-select each selected item in the
ListBox after it's no longer needed.

I have used a Control reference "ctl" for de-selecting the selected items,
but I've been informed that I should get rid of "ctl" and just refer to
lstAdmin directly. As you can see below, my attempt to use lstAdmin directly
is not working.

How do I use lstAdmin directly to de-select the selected items? Your help on
this will be greatly appreciated.

Following is the code:

Dim rs As Recordset
Dim db As Database
Dim SelectedItem As Variant
Dim ctl As Control

Set db = CurrentDb
Set rs = db.OpenRecordset("StudentCourses", dbOpenDynaset)
Set ctl = Me.[lstAdmin]

For Each SelectedItem In lstAdmin.ItemsSelected
rs.AddNew
rs![Course_ID] = Me.lstAdmin.Column(0, SelectedItem)
rs![Employees_ID] = Me.cboEmp_Name.Value
rs("Doc_Number") = Me.lstAdmin.Column(1, SelectedItem)
rs.Update
'Me.lstAdmin.SelectedItem = False
ctl.Selected(SelectedItem) = False
Next
 
G

Guest

Your very welcome!

I don't know about the 'magic touch' part. That seems to work by and large
for anyone but me <g>. I don't post most of my problems or you might not even
have read my response. <g>
--
Chaim


AKA Spawn said:
Chaim,

Your line of code returned an error message but that's cool because after
reading through the help section I found something interesting. Here's a
quote from the help sections MultiSelect Property:

"If the MultiSelect property is set to Extended, requerying the list box
clears any selections made by the user."

Sure enough, I did the requery on lstAdmin and it cleared all the selected
items. Don't know why I had not noticed that line in help before, especially
since I had read that section a couple of times before.

Maybe you have some kind of magic with you Chaim that rubs off :)

Thanks Chaim for your input and your magic touch too!

-AKA Spawn

Chaim said:
It is probably more efficient to simply clear all of the selection at once
when you're done with the set of selected items. Especially since you can't
(with your code) hit an item a second time.

After your Next statement add a line that says:

Me!lstAdmin.ItemsSelected = nothing

Good Luck!
--
Chaim


AKA Spawn said:
Hello,

I have some code that loops through a ListBox named "lstAdmin" to find all
selected items. What I want to do is de-select each selected item in the
ListBox after it's no longer needed.

I have used a Control reference "ctl" for de-selecting the selected items,
but I've been informed that I should get rid of "ctl" and just refer to
lstAdmin directly. As you can see below, my attempt to use lstAdmin directly
is not working.

How do I use lstAdmin directly to de-select the selected items? Your help on
this will be greatly appreciated.

Following is the code:

Dim rs As Recordset
Dim db As Database
Dim SelectedItem As Variant
Dim ctl As Control

Set db = CurrentDb
Set rs = db.OpenRecordset("StudentCourses", dbOpenDynaset)
Set ctl = Me.[lstAdmin]

For Each SelectedItem In lstAdmin.ItemsSelected
rs.AddNew
rs![Course_ID] = Me.lstAdmin.Column(0, SelectedItem)
rs![Employees_ID] = Me.cboEmp_Name.Value
rs("Doc_Number") = Me.lstAdmin.Column(1, SelectedItem)
rs.Update
'Me.lstAdmin.SelectedItem = False
ctl.Selected(SelectedItem) = False
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top