Results 1 to 5 of 5
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Userform with 2 listboxes; default values. (Word 2002 SP-2)

    I've got a Word userform with 2 listboxes. The code for the two listboxes is identical. I populate each listbox from an array of strings, and I set a default value for each listbox (e.g., ctlList1.Value = Choice1) before I Show the dialog. The dialog duly displays, the correct values show as selected, so far so good.

    The problem is that if I simply hit the OK button to accept both default values (which hides the dialog, at which point the code continues), the value of one (and only one) of the listboxes is always an empty string, rather than the default value (even though the default value is always correctly and visibly selected on the screen).

    To add further confusion, depending on which items in the lists I choose as the default values, the empty listbox is sometimes the first one and sometimes the second one.

    If I affirmatively (and redundantly, if things were working properly) select the default value in the listbox that would otherwise turn up empty, the macro works as it should.

    As a final confusing note (and possible further clue), if I set a breakpoint on the frm.lblList1 = strLabel1 line (see my code, below) and hover over ctlList1.Value, Choice1, ctlList2.Value and Choice2 (in the preceding 6 lines), Choice1 and Choice2 are always the correct strings, while one of the two .Value references is always correct and the other is always an empty string. (And again, whether it's ctlList1.Value or ctlList2.Value that's empty varies, depending on which list items I've chosen as the default values). So it looks from this like the problem has already occurred before the dialog shows (since the .Show line comes later; see below) -- and yet the default value is correctly displayed onscreen for BOTH listboxes.

    Here's my code. The commented lines are my kludgey "fix" for the problem, but they shouldn't be necessary. Anyone know what the real problem is?

    Sub ShowFrm2Lists(ListArray1 As Variant, ListArray2 As Variant, _
    Choice1 As Variant, Choice2 As Variant, _
    strLabel1 As String, strLabel2 As String)

    Dim frm As UserForm
    Dim ctlList1 As ListBox, ctlList2 As ListBox

    Load frm2Lists

    Set frm = frm2Lists
    Set ctlList1 = frm.lstList1
    Set ctlList2 = frm.lstList2

    ctlList1.List() = ListArray1
    ctlList2.List() = ListArray2

    If Not IsNull(Choice1) Then
    ctlList1.Value = Choice1
    End If
    If Not IsNull(Choice2) Then
    ctlList2.Value = Choice2
    End If

    frm.lblList1 = strLabel1
    frm.lblList2 = strLabel2

    frm2Lists.Show

    If Not IsLoaded("frm2Lists") Then
    Choice1 = Null
    Choice2 = Null
    Exit Sub
    Else
    ' If ctlList1.Value <> "" Then
    Choice1 = ctlList1
    ' End If
    ' If ctlList2.Value <> "" Then
    Choice2 = ctlList2
    ' End If
    End If

    Unload frm

    End Sub

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Userform with 2 listboxes; default values. (Word 2002 SP-2)

    It has been a while since I've run into this, but .Value probably is the wrong property to use to set an initial value in a ListBox. Instead, try using one of these:

    ctlList1.ListIndex = 0

    ctlList1.Selected(0) = True

  3. #3
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Userform with 2 listboxes; default values. (Word 2002 SP-2)

    <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15> Well, I guess there's something to be said for consistency. I'm getting buggy behavior with your alternative lines that is similar to the bugginess I get with the lines that use the .Value property. The particular list item that I designate by its index using either of your alternatives displays correctly in the dialog, but the Choice2 = ctlList2 line at the end of the procedure still fails, because ctlList2.Value remains an empty string (unless I make a point of clicking on the displayed default value in ctlList2 before I hit the OK button, in which case ctlList2.Value gets set and the macro works properly).

    ctlList1, on the other hand -- the code for which is identical to the code for ctlList2 -- works properly (i.e., its .Value property correctly reflects the default index) without the need to click on it before hitting OK.

    This is slightly different from the bugginess I experienced using .Value to set the default values in that, when I used .Value, the misbehavior would sometimes be with ctlList1 and sometimes with ctlList2, whereas for each case I tried using the index language, it was consistently ctlList2 whose .Value refused to "take".

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Userform with 2 listboxes; default values. (Word 2002 SP-2)

    I think the better solution would be to stop using the value, and use:

    <pre>Choice2 = ctlList2.List(ctlList2.ListIndex)</pre>

    But you also could use .SetFocus to update the .Value after you set .ListIndex. For example:

    <pre>With Me.ListBox2
    .AddItem "1 (Box 2)"
    .AddItem "2 (Box 2)"
    .ListIndex = 0
    .SetFocus
    End With
    With Me.ListBox1
    .AddItem "1 (Box 1)"
    .AddItem "2 (Box 1)"
    .ListIndex = 0
    .SetFocus
    End With</pre>

    But really that's work work that you should have to do.

  5. #5
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Userform with 2 listboxes; default values. (Word 2002 SP-2)

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> I am pleased to report that either one of your latest 2 solutions does the trick by itself.

    I had actually thought to try "SetFocus", but as I typed "ctlList2.SetFocus" I noted that SetFocus wasn't on the list in the whatchamacallit pop-up-as-you-type box, so I immediately bailed on that approach. In any case, the ctlList2.List(ctlList2.ListIndex) reference is clearly the cleaner approach, although why that doesn't equate to ctlList2.Value remains a mystery to me -- particularly given that the default choice that is correctly displayed in the listbox is in response to the line "ctlList2.Value = Choice2".

    I'm a Word newbie but I've been creating Access forms since Access 2, and I don't recall ever having a comparable problem referring to an Access listbox's value.

    Anyway, thanks for your persistence and have a good weekend.

Posting Permissions

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