Results 1 to 7 of 7
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Text vs. Value (Word XP)

    I'm working with some code where I move items in a list box up or down, as well as move items from one list box to another. I've always used lstBox.Value to determine the selected text, but I've noticed on occasion that the result is an empty string. So I've tried using lstBox.Text, which seems to do the job... at least for a moment. Then I have to switch back to Value.

    What exactly is the difference? Every time I think I've found a consistency to the behavior, something proves me wrong. Sometimes the llist item is selected interactively, and other times it's selected by my code. I thought that might be the variable, but I'm not certain.

    Is there a sure-fire way to know that I have the text /value of the item in the list box? I am always assigning it to a string variable before doing anything further with it, and from time to time that variable come up empty.

    Thanks,
    Richard Barrett

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Text vs. Value (Word XP)

    It depends on the properties of the list box.

    The Text property is the value of the first column in the selected row that has non-zero width.
    The Value property is the value of the bound column in the selected row (as specified by the BoundColumn property)

    You can also inspect the ListIndex property. If no item is selected, this has value -1, otherwise it is the zero-based index of the selected item (first item = 0, second item = 1, etc.)
    You can get the value of any column in the selected item using the List property. Column index is also zero-based, so to get the value of the 3rd column in the selected row, use

    If Me.ListBox1.ListIndex = -1 Then
    MsgBox "No item selected."
    Else
    MsgBox "Item in column 3: " & Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
    End If

    If your list box is multi-select, you cannot use the Text or Value property. Instead, loop through all items and use the Selected(n) property to determine which items are selected, and the List property to retrieve the column(s) you need.

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Text vs. Value (Word XP)

    Thanks. My list boxes have one column. When I remove or move up or down within the same list box, I generally don't have problems. The command buttons for these three actions (remove, up, down) are disabled unless an item is selected. I use the Change event of the list box to monitor that. I'm running into problems when I remove an item from one list box and put it in another, or mimic the selection in what I call a "shadow" list box.

    I start with one list box files with text. For testing purposes, I'm using One, Two, etc. through Seven. In the 'shadow' list box, I have 001, 002, 003, etc. through 007. I want the user to select a name in the "main" list box, but I want a list of corresponding ID numbers to be removed or move up or down in the 'shadow' list box. Also, I want the user to be able to 'move' an item from list box A to list box B or C. And when this happens, I want the 'shadow' list boxes to mimic what the three (visible) list boxes show. So if the user has chosen to move One and Four to list box B, the shadow of list box B will display 001 and 004.

    The Change event causes sets the same list index in the shadow list box. That generally works well. But to be certain, before manipulating the shadow list box item, I always get the list index of the corresponding visible list box and then set the list box for the shadow list box to be the same. The shadow list boxes will eventually be hidden. I always work with the shadow box first when moving to another list box so that the changing count or index of the main list box doesn't adversely affect the shadow.

    This is working for me about 80% of the time. Every once in a while, things seem to go awry and when I move an item from list box A to lixt box B, this is not mimicked in the shadow list boxes. The item is removed from shadow A, but it does not get added to shadow B. (It gets added, but as an empty string.) If I step through the code, I can see that lstBox.Value = "" even though I have just set the list index to, for example, 3.

    Given that I always have one column, which property -- Value or Text -- does it make more sense to use. I realize that Value is the default, but I'm not having 100% consistent results with it.

    Thanks,
    Richard

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Text vs. Value (Word XP)

    In a list box with one column, the Value and Text properties should be equivalent.

    In my experience, setting the value of ListIndex to select an item in a list box is not always dependable, so that may be the cause of your problem, You might experiment to see if setting the Selected property works better: instead of using

    Me.ListBox2.ListIndex = Me.ListBox1.ListIndex

    try

    Me.ListBox2.Selected(Me.ListBox1.ListIndex) = True

  5. #5
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Text vs. Value (Word XP)

    I'll give this a try. Thank you. The dependability does seem to be hit or miss.

    Thanks,
    Richard

  6. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Text vs. Value (Word XP)

    Interestingly, this gave me good results for about 45 minutes. Then my variable was not reading the text/value of the list box items about 50% of the time. On the next go, it was 100% consistent in not getting the value on the 2nd and 3rd of my list boxes, although the first one was consistently dependable. I exited and restarted Word. Same results. I re-booted the PC, and then everything functioned as expected.

    I would assume that this was the result of memory leaks or unreleased memory after an hour or so of steady testing and code modification. Do you agree? (when I'm doing serious VBA developement, I try to re-boot every several hours.) Or do you think this is something I should be concerned about when (and if) it's released to the general user population?

    Thanks,
    Richard

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Text vs. Value (Word XP)

    To be honest, I don't know. The userforms I create are usually not very complicated, so I haven't experienced such problems. (I use Access for 'serious' development, and Access forms behave differently from userforms)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •