PDA

View Full Version : [SOLVED] check if listbox selected



Nmarkit
09-10-2013, 03:48 AM
hello

I have 2 listboxes. I need to check if either one, both or neither are selected. have tried a few things but none seem to work. Any ideas please?

i've tried


If IsNull(ListBox1) AND IsNull(ListBox2) then
MsgBox "Please select an Intrument or Account"
'ElseIf IsNull(ListBox1) Then
'MsgBox "account selected"
'ElseIf IsNull(ListBox2) Then
'MsgBox "product selected"
End If

HaHoBe
09-10-2013, 04:11 AM
Hi, Nmarkit,

have you tried using ListBox1.ListIndex > -1 which would correspond to any choice having been made?

Ciao,
Holger

Nmarkit
09-10-2013, 05:31 AM
Hi Holger, thanks for your response. Am afraid it doesn't seem to do the trick. the condition shows up as valid in all scenarios. so whether any list box item is selected or not i get the msg.



If ListBox1.ListIndex > -1 Then
MsgBox "Please select an Intrument or Account"
End If


Btw both listboxes are multi select. don;t know if that makes a difference

HaHoBe
09-10-2013, 07:56 AM
Hi, Nmarkit,

what about this code?

Dim lngItems As Long
Dim lngSel As Long

For lngItems = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lngItems) Then
lngSel = lngSel + 1
End If
Next lngItems

If lngSel = 0 Then
MsgBox "Please select an Intrument or Account"
End If
Ciao,
Holger

Nmarkit
09-10-2013, 08:17 AM
That worked perfectly! thanks a lot Holger.