|
-
Jun 5th, 2007, 08:45 AM
#1
Thread Starter
Frenzied Member
Object variable or With block variable not set
I have this code below in the textbox Change event. It is part of an Auto-Complete code.
It all works ok, except when I try to change the textbox string to all three columns of the listview. This makes the Change event fire again and I get the error:
Object variable or With block variable not set
on this line:
vb Code:
lvwMembers.FindItem(txtSearch.Text, , , lvwPartial).Selected = True
My complete code is:
vb Code:
Private Sub txtSearch_Change()
On Error GoTo ErrTrap
Dim LvwItm As ListItem
Dim Strt As Integer
'If firing in response to a backspace or delete, don't run the auto-complete
'complete code. (Otherwise you wouldn't be able to back up.)
If blnBackSpace = True Or txtSearch.Text = vbNullString Then
blnBackSpace = False
Exit Sub
End If
'Run through the available items and grab the first matching one.
If lvwMembers.ListItems(lvwMembers.SelectedItem.index).EnsureVisible = True Then
'search the first column (First Name)
lvwMembers.FindItem(txtSearch.Text, , , lvwPartial).Selected = True
End If
'If something was chosen then we get the rest of it
Strt = Len(txtSearch.Text)
'add full name to txtSearch
If Len(lvwMembers.SelectedItem.SubItems(1)) = 0 Then
txtSearch.Text = lvwMembers.SelectedItem.Text & " " & lvwMembers.SelectedItem.SubItems(2)
Else
txtSearch.Text = lvwMembers.SelectedItem.Text & " " & lvwMembers.SelectedItem.SubItems(1) _
& " " & lvwMembers.SelectedItem.SubItems(2)
End If
txtSearch.SelStart = Strt
txtSearch.SelLength = Len(txtSearch.Text) - Strt
Exit Sub
-
Jun 5th, 2007, 09:16 AM
#2
Re: Object variable or With block variable not set
The problem is that FindItem returns an object, and that object might legitimately be set to Nothing. So when you attempt to set a property of it to something, you'll get that error.
Generally speaking, I'm uncomfortable using any object until I've verified that it isn't Nothing. So I would do something like this:
Code:
Dim itm As MSComctlLib.ListItem
Set itm = lvwMembers.FindItem(txtSearch.Text, , , lvwPartial)
If Not itm Is Nothing Then
itm.Selected = True
End If
Set itm = Nothing
An alternate (and more compact) way to go would be:
Code:
With lvwMembers
Set .SelectedItem = .FindItem(txtSearch.Text, , , lvwPartial)
End With
This second way is nice because setting .SelectedItem = Nothing is a perfectly valid way to deselect the listview, and that's probably what you want if .FindItem returns Nothing anyway.
I'm not sure how well behaved the .SelectedItem object is if the listview supports multiple selections, though. If it does, using the .Selected property is the way to go.
Last edited by Ellis Dee; Jun 5th, 2007 at 09:21 AM.
-
Jun 5th, 2007, 09:45 AM
#3
Thread Starter
Frenzied Member
Re: Object variable or With block variable not set
Thanks Ellis Dee.
I couldn't get the second option to work, but I did get the first option, with a small change to work:
vb Code:
Set LvwItm = lvwMembers.FindItem(txtSearch.Text, , , lvwPartial)
If Not LvwItm Is Nothing Then
'item found
LvwItm.EnsureVisible
LvwItm.Selected = True
Else
'nothing to select
LvwItm.Selected = False
End If
Set LvwItm = Nothing
The second part of the If statement - would this be correct to stop any selection if nothing is returned?
Thanks again.
-
Jun 5th, 2007, 10:01 AM
#4
Re: Object variable or With block variable not set
No, it'll throw the same error (for the same reason) as in the OP. Instead, change the Else clause to this:
Set lvwMembers.SelectedItem = Nothing
The only other way to ensure no item is selected is to set up a For (Each) loop iterating all the list items, setting each one's .Selected property to False. Much more cumbersome, and much slower.
May I ask what the issue was with the second option? I don't see why the following wouldn't work, but my expertise is more for the similar TreeView control:
Code:
With lvwMembers
Set .SelectedItem = .FindItem(txtSearch.Text, , , lvwPartial)
If Not .SelectedItem Is Nothing Then .SelectedItem.EnsureVisible
End With
Last edited by Ellis Dee; Jun 5th, 2007 at 10:05 AM.
-
Jun 5th, 2007, 10:10 AM
#5
Thread Starter
Frenzied Member
Re: Object variable or With block variable not set
 Originally Posted by Ellis Dee
No, it'll throw the same error (for the same reason) as in the OP. Instead, change the Else clause to this:
Set lvwMembers.SelectedItem = Nothing
This throws the same error 
 Originally Posted by Ellis Dee
May I ask what the issue was with the second option? I don't see why the following wouldn't work, but my expertise is more for the similar TreeView control:
Code:
With lvwMembers
Set .SelectedItem = .FindItem(txtSearch.Text, , , lvwPartial)
If Not .SelectedItem Is Nothing Then .SelectedItem.EnsureVisible
End With
With this I get the same error on the line starting"If Len"
vb Code:
With lvwMembers
Set .SelectedItem = .FindItem(txtSearch.Text, , , lvwPartial)
If Not .SelectedItem Is Nothing Then .SelectedItem.EnsureVisible
End With
'If something was chosen then we get the rest of it
Strt = Len(txtSearch.Text)
'add full name to txtSearch
If Len(lvwMembers.SelectedItem.SubItems(1)) = 0 Then
txtSearch.Text = lvwMembers.SelectedItem.Text & " " & lvwMembers.SelectedItem.SubItems(2)
Else
txtSearch.Text = lvwMembers.SelectedItem.Text & " " & lvwMembers.SelectedItem.SubItems(1) _
& " " & lvwMembers.SelectedItem.SubItems(2)
End If
-
Jun 5th, 2007, 10:23 AM
#6
Re: Object variable or With block variable not set
Not to worry, that's easily fixable. Just remember that .SelectedItem is an object, so you need to always be sure it's valid before accessing its properties or methods. Try this:
Code:
With lvwMembers
Set .SelectedItem = .FindItem(txtSearch.Text, , , lvwPartial)
If Not .SelectedItem Is Nothing Then
With .SelectedItem
.EnsureVisible
'If something was chosen then we get the rest of it
Strt = Len(txtSearch.Text)
'add full name to txtSearch
If Len(.SubItems(1)) = 0 Then
txtSearch.Text = .Text & " " & .SubItems(2)
Else
txtSearch.Text = .Text & " " & .SubItems(1) & " " & .SubItems(2)
End If
End With
End If
End With
Note how .SelectedItem isn't accessed unless we've verified that it's valid. Also note that .SelectedItem simply begs to be referenced using a With...End With construct; the code reduces nicely when you don't have to write out the bulky object hierarchy each time. As an added bonus, it also executes faster.
Last edited by Ellis Dee; Jun 5th, 2007 at 10:35 AM.
-
Jun 5th, 2007, 10:31 AM
#7
Thread Starter
Frenzied Member
Re: Object variable or With block variable not set
Sorry if I am being a bit thick here, but the following is what I now have and I am now getting the error:
Method or Data member not found
on this line:
Code:
If Len(.SelectedItem.SubItems(1)) = 0 Then
My code in full now:
vb Code:
Private Sub txtSearch_Change()
On Error GoTo ErrTrap
Dim LvwItm As ListItem
Dim Strt As Integer
'If firing in response to a backspace or delete, don't run the auto-complete
'complete code. (Otherwise you wouldn't be able to back up.)
If blnBackSpace = True Or txtSearch.Text = vbNullString Then
blnBackSpace = False
Exit Sub
End If
With lvwMembers
Set .SelectedItem = .FindItem(txtSearch.Text, , , lvwPartial)
If Not .SelectedItem Is Nothing Then
With .SelectedItem
.EnsureVisible
'If something was chosen then we get the rest of it
Strt = Len(txtSearch.Text)
'add full name to txtSearch
If Len(.SelectedItem.SubItems(1)) = 0 Then
txtSearch.Text = .Text & " " & .SubItems(2)
Else
txtSearch.Text = .Text & " " & .SubItems(1) & " " & .SubItems(2)
End If
End With
End If
End With
-
Jun 5th, 2007, 10:40 AM
#8
Re: Object variable or With block variable not set
Yeah, sorry about that, my bad. That's an honest to god bug that I introduced. I have since edited it out of the code in the post above.
The problem was that .SelectedItem was already included in the With construct, and I had it then referencing a .SelectedItem beneath that.
The literal value it was trying to interpret was:
lvwMembers.SelectedItem.SelectedItem.SubItems(1)
Which, unsurpisingly, threw an error.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|