Windows 7 - Trapping "Invalid Property Value" errors

Asked By kMa on 18-Mar-07 06:29 PM
Hello all,

I have a form with comboxes, whose selections are restricted to only the
values in these (MatchRequired = True)...

Problem though, when user clicks on one of these comboboxes and exits
without selecting an entry, word spits-out a "Invalid Property Value" error
message. Is it possible to trap this error and define my own error handling
procedure, and if so, where would I do this? In the function that initialises
the form, or in the form_initialize routine?

Thanks for your help

Jay Freedman replied on 18-Mar-07 10:09 PM
The error occurs because the default value of the combobox's
.ListIndex property is -1.

The simplest fix is to set the .ListIndex value of each combobox to 0
in the UserForm_Initialize routine. That makes the first item in the
list be the default value, and there is no way for the user to force
the .ListIndex back to -1.

If for some reason you don't want any of the items in the box to be
the default but you still want to catch the condition before it causes
the error, create combobox_Exit procedure for each combobox and check
the .ListIndex value:

Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If ComboBox1.ListIndex < 0 Then
MsgBox "You must select an item in ComboBox1"
Cancel = True
End If
End Sub

Setting the Cancel parameter to True prevents the focus from going to
the next control on the form.

Jay Freedman
Microsoft Word MVP        FAQ:
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Sun, 18 Mar 2007 16:29:00 -0700, kMan
kMa replied on 29-Mar-07 10:48 PM