Results 1 to 4 of 4
  1. #1
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    'Exit' event in a form, how to make it work? (Word 2000)

    I'm trying to do something in VBA (for Word 2000) that I haven't done before, and I'm getting an error message that I can't figure out.

    In a user-defined form, I'm trying to use a control's Exit event to validate the control's contents. The event is defined like this:

    Private Sub controlname_Exit(ByVal cancel As MSForms.ReturnBoolean)
    . . .
    End Sub

    My problem is how to set the parameter "cancel," so that the exit can indicate whether the focus should be allowed to leave the control.

    When I type the formal parameters, VBA recognizes the project name MSForms, and auto-complete confirms that ReturnBoolean is one of its members. When I finish the statement, however, it capitalizes MSForms correctly but shows ReturnBoolean in all lower case, as if it recognized the project but ReturnBoolean were NOT one of its members.

    The object browser shows me that "cancel" is an object with one member, a Boolean named "value." But when the event tries to execute a statement like "cancel.value = True," I get an error message which says, "Object variable or With block variable not set."

    My attempts to display help for that message are unavailing; VBA's help engine tells me there is not enough memory (on a 1 GB machine with nothing running but Word, VBA, and Dragon NaturallySpeaking).

    My attempts to reason out the meaning of the message take me in circles. If it's referring to "True," that's a built-in constant; how could it possibly be not set? If it's referring to "cancel," it's a parameter, so it HAS to be set. (On the theory that perhaps the exit expects me to create and assign the parameter's value, I tried "cancel = new MSForms.ReturnBoolean," only to be told that "ActiveX Object can't create object.")

    I searched the Web for an example of how to do this, and found one for VBA Excel (not Word). It simply sets "cancel = True." I don't see how that could work, since cancel is an object, not a simple variable. I tried it, though, and got the same cryptic "...variable not set" error.

    To complete my bafflement, the ByVal keyword would appear to make it impossible for the exit to change the value of "cancel" at all. Yet the help topic for the Enter and Exit events says, "To prevent the control from losing focus, assign True to the Cancel argument of the Exit event," which confirms that the event is intended for that purpose.

    Can anyone help me make sense of this?

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: 'Exit' event in a form, how to make it work? (Word 2000)

    Cancel is annoying because it has not been developed consistently by Microsoft. I think in your case you just want to set Cancel to true. I've attached a sample Word document with a userform with two textboxes and a command button and the code below. Notice that I use "Cancel = True" to prevent the user from leaving the textbox, but "Cancel = vbYes" to prevent the user from pressing the X-close button in the top-right of the form. vbYes just helps explain the code: whenever the query-close Cancel is non-zero, the close is ignored. HTH --Sam
    <pre>Option Explicit
    '
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1.Text <> "yourself" Then
    Cancel = True
    End If
    End Sub
    '
    Private Sub CommandButton1_Click()
    Unload Me
    End Sub
    '
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If TextBox1.Text <> "yourself" Then
    TextBox1.SetFocus
    Cancel = vbYes
    End If
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: 'Exit' event in a form, how to make it work? (Word 2000)

    Yes that works... although I'm baffled about why, because it looks exactly equivalent to my original code, which still does not work.

    I decided to simplify the error checking to get the job done without having to deal with these issues (several remain). I may revisit it later. Thanks for your help.

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

    Re: 'Exit' event in a form, how to make it work? (Word 2000)

    The definition and Object Browser information are misleading, or at least confusing. Despite what these may say, Cancel is just an Integer whose default value is 0 (False). If you set it to any non-zero value (such as True = -1, or vbYes = 6), you cancel the event. You can't treat Cancel as an object, just use
    Cancel = True
    or
    Cancel = False

Posting Permissions

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