Cannot Get ListBox .ItemsSelected, help!

Joined
Nov 25, 2010
Messages
6
Reaction score
0
Help.

i am unable to retrieve .ItemsSelected of a multiselect listbox on a form.


1. the form is visible
2. i manually select some items in the listbox.
3. then in VBA i get the .ItemsSelected.Count property of the listbox.
4. ItemsSelected.Count returns 0. This is a bug?
Code:
?[Form_Flex2 Hospitals].lstHospitals.ItemsSelected.Count
' returns 0, even if items are selected
5. But, this other syntax works:
Code:
?Forms![Flex2 Hospitals].Form.[lstHospitals].ItemsSelected.Count
' returns correct number of selected items
6. i can clearly see items selected in the listbox.
7. If i try to close the form, it will not close!
DoCmd.Close acForm, "Flex2 Hospitals"
8. but, after trying to close the form, THEN i can get the ItemsSelected
Code:
?[Form_Flex2 Hospitals].lstHospitals.ItemsSelected.Count
' returns correct number of selected items ONLY if i try to close the form first

What's the problem?


The SECOND time i try to close the form, it closes properly:
DoCmd.Close acForm, "Flex2 Hospitals"

More info:
i am loading several forms in Hidden mode.
then i make only one form visible at a time by VBA.

if i open this one form by itself, with no other forms loaded, then i am able to retrieve .ItemsSelected.Count value correctly.


Not Current Object:
This code returns the name of a hidden form, instead of the open form:
?CurrentObjectName
' returns name of a hidden form, instead of the visible one.

If i attempt to setfocus to this form, CurrentObjectName STILL returns name of a hidden form.
[Form_Flex2 Hospitals].SetFocus
?CurrentObjectName
' returns name of a hidden form, instead of the visible one.

Here are images of the form:
postimg.org/image/ft8enykt9
postimg.org/image/6wxmk0u71


also posted to:
accessforums.net, utteraccess.com, access-programmers.co.uk
 
Last edited:
Joined
Nov 25, 2010
Messages
6
Reaction score
0
SOLVED:

thorough explanation here:

social.msdn.microsoft.com/Forums/office/en-US/cb59d18f-3214-4fe5-bdc8-bf40c99fb483/cannot-get-listbox-itemsselected-help?prof=required

"If the form isn't loaded when you make this reference to its class module, Access will load a hidden copy of the form and add it to the Forms collection, but will not index it by name until you make it visible."

"remove all uses of Form_<form name> and replace them with the canonical usage:

'DoCmd.OpenForm "FormName"' instead of 'Form_FormName.Visible = True'
'Forms!FormName.ControlName' or 'Forms("FormName").ControlName' instead of Form_FormName.ControlName
'DoCmd.SelectObject "FormName"' instead of Form_FormName.SetFocus
 

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