Results 1 to 8 of 8

Thread: Object variable or With block variable not set

  1. #1

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    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:
    1. lvwMembers.FindItem(txtSearch.Text, , , lvwPartial).Selected = True

    My complete code is:
    vb Code:
    1. Private Sub txtSearch_Change()
    2. On Error GoTo ErrTrap
    3.  
    4. Dim LvwItm As ListItem
    5. Dim Strt As Integer
    6.  
    7. 'If firing in response to a backspace or delete, don't run the auto-complete
    8. 'complete code. (Otherwise you wouldn't be able to back up.)
    9.  
    10. If blnBackSpace = True Or txtSearch.Text = vbNullString Then
    11.      blnBackSpace = False
    12.      Exit Sub
    13. End If
    14.  
    15. 'Run through the available items and grab the first matching one.
    16. If lvwMembers.ListItems(lvwMembers.SelectedItem.index).EnsureVisible = True Then
    17.     'search the first column (First Name)
    18.     lvwMembers.FindItem(txtSearch.Text, , , lvwPartial).Selected = True
    19. End If
    20.  
    21. 'If something was chosen then we get the rest of it
    22. Strt = Len(txtSearch.Text)
    23. 'add full name to txtSearch
    24. If Len(lvwMembers.SelectedItem.SubItems(1)) = 0 Then
    25.     txtSearch.Text = lvwMembers.SelectedItem.Text & " " & lvwMembers.SelectedItem.SubItems(2)
    26. Else
    27.     txtSearch.Text = lvwMembers.SelectedItem.Text & " " & lvwMembers.SelectedItem.SubItems(1) _
    28.                                                  & " " & lvwMembers.SelectedItem.SubItems(2)
    29. End If
    30. txtSearch.SelStart = Strt
    31. txtSearch.SelLength = Len(txtSearch.Text) - Strt
    32.  
    33. Exit Sub
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  2. #2
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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.

  3. #3

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    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:
    1. Set LvwItm = lvwMembers.FindItem(txtSearch.Text, , , lvwPartial)
    2. If Not LvwItm Is Nothing Then
    3.     'item found
    4.     LvwItm.EnsureVisible
    5.     LvwItm.Selected = True
    6. Else
    7.     'nothing to select
    8.     LvwItm.Selected = False
    9. End If
    10. Set LvwItm = Nothing

    The second part of the If statement - would this be correct to stop any selection if nothing is returned?

    Thanks again.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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.

  5. #5

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Object variable or With block variable not set

    Quote 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

    Quote 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:
    1. With lvwMembers
    2.     Set .SelectedItem = .FindItem(txtSearch.Text, , , lvwPartial)
    3.     If Not .SelectedItem Is Nothing Then .SelectedItem.EnsureVisible
    4. End With
    5.  
    6.  
    7. 'If something was chosen then we get the rest of it
    8. Strt = Len(txtSearch.Text)
    9. 'add full name to txtSearch
    10. If Len(lvwMembers.SelectedItem.SubItems(1)) = 0 Then
    11.     txtSearch.Text = lvwMembers.SelectedItem.Text & " " & lvwMembers.SelectedItem.SubItems(2)
    12. Else
    13.     txtSearch.Text = lvwMembers.SelectedItem.Text & " " & lvwMembers.SelectedItem.SubItems(1) _
    14.                                                  & " " & lvwMembers.SelectedItem.SubItems(2)
    15. End If
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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.

  7. #7

    Thread Starter
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    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:
    1. Private Sub txtSearch_Change()
    2. On Error GoTo ErrTrap
    3.  
    4. Dim LvwItm As ListItem
    5. Dim Strt As Integer
    6.  
    7. 'If firing in response to a backspace or delete, don't run the auto-complete
    8. 'complete code. (Otherwise you wouldn't be able to back up.)
    9.  
    10. If blnBackSpace = True Or txtSearch.Text = vbNullString Then
    11.      blnBackSpace = False
    12.      Exit Sub
    13. End If
    14.  
    15. With lvwMembers
    16.     Set .SelectedItem = .FindItem(txtSearch.Text, , , lvwPartial)
    17.     If Not .SelectedItem Is Nothing Then
    18.         With .SelectedItem
    19.             .EnsureVisible
    20.             'If something was chosen then we get the rest of it
    21.             Strt = Len(txtSearch.Text)
    22.             'add full name to txtSearch
    23.             If Len(.SelectedItem.SubItems(1)) = 0 Then
    24.                 txtSearch.Text = .Text & " " & .SubItems(2)
    25.             Else
    26.                 txtSearch.Text = .Text & " " & .SubItems(1) & " " & .SubItems(2)
    27.             End If
    28.         End With
    29.     End If
    30. End With
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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
  •  



Click Here to Expand Forum to Full Width