Resetting listbox isn't working correctly

Stoss

Registered User.
Local time
Yesterday, 19:13
Joined
Nov 5, 2010
Messages
107
I am trying to reset a list box...which works in a sense.

Code:
Dim varItem As Variant
    
    With Me.lstPriorities
        For Each varItem In .ItemsSelected
            .Selected(varItem) = False
        Next varItem
    End With

It deselects all the items but then, as part of the error checking I have

Code:
If Me.lstPriorities.ListIndex = -1 Then
        MsgBox "You have not selected any Priorities!"
        Exit Sub
    Else
and it doesn't see this correctly because it isn't seeing -1. Eventhough nothing is selected, I get results back like 0 or 2 or etc...(from a msgbox listindex) so it bypasses that info and then gives me errors on the SQL string.

Any ideas what I can do?
Stoss
 
Replace this:
Code:
Dim varItem As Variant
    
    With Me.lstPriorities
        For Each varItem In .ItemsSelected
            .Selected(varItem) = False
        Next varItem
    End With
With this:
Code:
Me.lstPriorities.Selected(1) = True
Me.lstPriorities.Selected(1) = False
Me.lstPriorities.Value = vbNullString
 
Thanks You!! That worked like a charm.

One question though...How does that work? I mean, looking at it, I wouldn't think that it would deselect the rest of them in a multi select senerio.

I did also put it in after my loop and it worked there too (the vbNullString part).

Your code is much cleaning but I am scratching my head as to why it would work.

Thanks,
Stoss
 
I forgot that you have a multiselect listbox there. So all you really need is just this one line

Me.lstPriorities.Value = Null

Use Null instead of vbNullString. I prefer using vbNullString so I don't have to deal with Null afterwards.

The way it works is if you set the value of listbox it will jump to that row without highlighting it. So if we were to set it to Null, there's no ID with a Null value so it will simply not highlight anything and it will have a Null value just as it was originally when the form loaded.
 
Thanks vbaInet,

Unfortunately, the one line of Me.lstPriorities.value = Null did not work. I would have like it to because it is clear code :)

So, I went back to what we talked about earlier and that works great!

Thanks so much for your help
-Stoss
 
I see.

Another thing to note is if you were at the last record (for example) and you use that code it won't move the scroll to the top (completely). Would you want it to do so?
 
Ah yes, I didn't even check that. You are right, it doesn't scroll back up. That would be nice if it did.
 
There are two ways. Give them a try:
Code:
Me.lstPriorities.Value = Null
Me.lstPriorities.Selected(0) = True
Me.lstPriorities.Selected(0) = False
Me.lstPriorities.Value = Null
Code:
Me.lstPriorities.Requery
Me.lstPriorities.Value = Null
I prefer the first one because you won't be querying the database.
 
With the code in the first box, it doesn't seem to deselect anything and it doesn't go back to the top. I tried many combinations with that code but couldn't get it to work. I also tried the other code and that didn't seem to work either.

So, at this point, the only code that I can get to work is.
With Me.lstPriorities
For Each varItem In .ItemsSelected
.Selected(varItem) = False
Next varItem
Me.lstPriorities.Value = Null
End With

This clears all items selected and doesn't not cause a problem with running a search after that.

The only thing that this code doesn't do now it move back to the top
-Stoss
 
Interesting. Sounds like you are using the Simple multiselect option?
 
Ah, that's why it won't work. The code will work for Extended only, but here's one for Simple:
Code:
Dim strSource As String

strSource = Me.lstPriorities.RowSource
Me.lstPriorities.RowSource = vbnullstring
Me.lstPriorities.RowSource = strsource
Me.lstPriorities.Requery
Me.lstPriorities.Value = Null
You won't need the loop for this one.
 
This is a side note that maybe you could solve.

When I populate a list box, I have it sent to sent the listcount to a textbox. However, I noticed that it gives me 1 more count than what is actually there. Would you happen to know why it is doing this? Is is seeing the Column Header as a "listcount"? I can make a new post if you don't have time for this one.

Thanks,
Stoss
 
Is is seeing the Column Header as a "listcount"? I can make a new post if you don't have time for this one.
That's fine, it's only a simple question. Yes you're correct. It counts the Column Header too. So remember to minus 1 if you were using it in a loop.
 
Ok, I figured as much.

Thanks so much for your help!!!

This project is giving me a headache :) I hate customized search boxes!!!

-Stoss
 
The more the customisation, the more code is involved. :)

Did you see my post with the code for Simple? Our posts crossed.
 
This only problem is, I have a Select statement for that rowsource and if I set it to null, it will wipe out my statement right?
 
If you notice in the code, it will take a copy of the row source, wipe it out, then copy it back.
 
AWESOME! That works like a champ! Clears it and resets it right at the top. PERFECT!

Thanks you so much! It is these little things that can eat so much of my time.

-Stoss

Side Note: I see you are from the UK, I heard it is COLD there
 
You're welcome. Glad that worked for you!

It is a bit, especially up North and the East but not so much down South.
 

Users who are viewing this thread

Back
Top Bottom