PDA

View Full Version : Solved: Cannot assign value to userform object



falconwing
09-04-2007, 02:25 PM
I've been working on some VBA in Word 2003 with a fairly simple userform. Recently I noticed that assignments to a particular object.value were failing with no error. When I went back to an earlier revision, it worked, yet the only difference was that I had deleted text from an unrelated label.

I figured the userform was corrupt and recreated it from scratch. Now after some further edits, it appears the same problem is back, so I'm assuming it is something obvious that I'm missing that I have screwed up.

The code snippet in question:
Glob_DocType.Value = DocumentType

I ran the debugger past that line and this "immediate" output shows the assignment fails with no error:
debug.Print DocumentType
FFRD
Glob_DocType.Value = "FFRD"
debug.Print Glob_DocType.Value

debug.Print typename(Glob_DocType.Value)
String
debug.Print Glob_DocType.List(0)
FDD
debug.Print Glob_DocType.List(1)
FFRD
debug.Print typename(Glob_DocType)
ListBox
Glob_DocType.Value = Glob_DocType.List(1)
debug.Print Glob_DocType.Value
debug.Print Glob_DocType.Enabled
True
debug.Print Glob_DocType.Visible
True
debug.Print Glob_DocType.Locked
False

Also when I try to enter a value in the "Locals" window, it fails to store, again with no error. Any ideas welcome as this is driving me nuts.

Thanks

mdmackillop
09-04-2007, 03:42 PM
Hi Falconwing
Welcome to VBAX
Can you zip and post your document? Use Manage Attachments in the Go Advanced section.

falconwing
09-05-2007, 06:13 AM
I have attached all of the files used in the project. Thanks!

mdmackillop
09-07-2007, 09:15 AM
I'm not familiar with this stuff, but I'll bump it the list.

TonyJollans
09-07-2007, 11:38 AM
I don't know of any reason why it should fail and it appears to work for me - in both 2003 and 2007.

falconwing
09-11-2007, 01:58 PM
So this is another case of ill behavior within VBA.

Sometimes the assign to listbox works and sometimes it doesn't. Note this comment in the "Value Property" help page:

Value cannot be used with a multi-select list box.

My listbox was 0-fmMultiSelectSingle (not fmMultiSelectMulti or fmMultiSelectExtended). So why did it work part of the time? Buggy.

Bottom line, if you are using a fmMultiSelectSingle listbox:
- Set the default with .Text
- Do not use .Value for anything
- Find the single selected value the same way you would for multiselect, by looping through .Selected

Screenshot is code that includes 4 simple listboxes and sets their defaults via .Text. The right hand side shows the output of printing .Value and .Text. The lower right shows the .Selected value obtained by looping through the list for each listbox.