Itemdata and only 1 item selected

  • Thread starter Cyberwolf via AccessMonster.com
  • Start date
C

Cyberwolf via AccessMonster.com

I have the following code that I know can be rewritten to be a little more
graceful.

If Forms(stF)!lstRulesTableName.ItemsSelected.Count = 1 Then

For Each varItem In Forms(stF)!lstRulesTableName.
ItemsSelected
stTableName = Forms(stF)!lstRulesTableName.ItemData
(varItem)
Next varItem

Basicaaly I first check to see if there is only 1 item selected. The I do
the for next loop, which I belive can be gotten rid of, but I haven't been
able to figure out what to put in the index part of the ItemData.

TIA.
 
D

Dirk Goldgar

Cyberwolf via AccessMonster.com said:
I have the following code that I know can be rewritten to be a little more
graceful.

If Forms(stF)!lstRulesTableName.ItemsSelected.Count = 1 Then

For Each varItem In Forms(stF)!lstRulesTableName.
ItemsSelected
stTableName = Forms(stF)!lstRulesTableName.ItemData
(varItem)
Next varItem

Basicaaly I first check to see if there is only 1 item selected. The I do
the for next loop, which I belive can be gotten rid of, but I haven't been
able to figure out what to put in the index part of the ItemData.


My first question is whether you need to be checking to make sure there is
only one item selected. If it's your intention to allow only one selection,
then you can set the list box's MultiSelect proeprty to None, and it will
not permit the user to select more than one item. If you do that, you need
only check whether the list box's Value is Null or not, to know whether an
item is selected; if it's not Null, then the value is the ItemData of the
selected item.

If you need to allow multiselect, but in this code you want to pick up the
value of the only selected item, then you might rewrite it as:

With Forms(stF)!lstRulesTableName
If .ItemsSelected.Count = 1 Then
stTableName = .ItemData(.ItemsSelected(0))
End If
End With
 
C

Cyberwolf via AccessMonster.com

Dirk said:
I have the following code that I know can be rewritten to be a little more
graceful.
[quoted text clipped - 10 lines]
the for next loop, which I belive can be gotten rid of, but I haven't been
able to figure out what to put in the index part of the ItemData.

My first question is whether you need to be checking to make sure there is
only one item selected. If it's your intention to allow only one selection,
then you can set the list box's MultiSelect proeprty to None, and it will
not permit the user to select more than one item. If you do that, you need
only check whether the list box's Value is Null or not, to know whether an
item is selected; if it's not Null, then the value is the ItemData of the
selected item.

If you need to allow multiselect, but in this code you want to pick up the
value of the only selected item, then you might rewrite it as:

With Forms(stF)!lstRulesTableName
If .ItemsSelected.Count = 1 Then
stTableName = .ItemData(.ItemsSelected(0))
End If
End With
Thanks Dirk,

That was just what I was looking for. It worked great. I know the way I had
it written worked fine, but I am always looking for the better way. BTW
there are times when 0, 1 or more lines can be selected in the list box
depending on what the user is doing. So I need to check how many are
selected and then run code based on this criteria.
 

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