Results 1 to 4 of 4

Thread: <Resolved> How to stop error message if cancel pressed

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Location
    West Midlands, UK
    Posts
    33

    Resolved <Resolved> How to stop error message if cancel pressed

    I am using the code below to validate the entry in a text field:

    Private Sub txtHomeNotes_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim RespMsg
    If Len(txtHomeNotes.Value) > 150 Then
    RespMsg = MsgBox("Notes must not be more than 150 characters in length." & Chr(10) & "Please compress message", vbExclamation + vbOKOnly, "Notes Error")
    txtHomeNotes.SetFocus
    End If
    End Sub

    If the user has entered 151 or more characters in the notes box and then presses the cancel command button, the MsgBox is still displayed. Is there any way to detect that the Cancel command button has been pressed, therefore allowing me to supress the error message.

    I would prefer all validation to be done on exit from text box rather than upon pressing the OK command button, as I am using validation for several other text boxes as well. They all give the same problem.

    I am using VBA for Excel.
    Last edited by Borg2of6; Aug 12th, 2005 at 11:40 AM. Reason: to set resolved status

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to stop error message if cancel pressed

    Hi Borg2of6, welcome to VBForums!

    I'll start with mentioning a couple of improvements to your current code. The first is that you don't need to set up a variable to use a messagebox, you just need to remove the brackets.

    The other is that you shouldn't use setfocus, as the other control (that the user was about to go to) may get the Enter and Exit events (which can create an endless loop if the other control has the same problem). Instead you should just set the Cancel parameter to true, which has the benefits of SetFocus but doesn't fire the other events.

    VB Code:
    1. Private Sub txtHomeNotes_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    2.  
    3.  If Len(TextBox3.Value) > 5 Then
    4.   MsgBox "Notes must not be more than 150 characters in length." & Chr(10) & "Please compress message", vbExclamation + vbOKOnly, "Notes Error"
    5.   Cancel = True
    6.  End If
    7.  
    8. End Sub

    Unfortunately I don't know how you can tell if the Cancel button has been pressed at this point, as this event is fired before the next control gets the focus. (you can tell the control that currently has focus by using Me.ActiveControl.Name ).

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2005
    Location
    West Midlands, UK
    Posts
    33

    Re: How to stop error message if cancel pressed

    Thank you for your reply. I have implemented the suggested improvements. I will remeber the Cancel for next time as well.

    Anybody else out there know how I can resolve the original problem?

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2005
    Location
    West Midlands, UK
    Posts
    33

    Resolved <Resolved> How to stop error message if cancel pressed

    Since I posted the problem I have continued to research this problem on the internet. I have now found a solution on another help forum. The solution incorporated into my code is: (amendments in blue)

    VB Code:
    1. [COLOR=Blue]Dim bCancel as Boolean[/COLOR]
    2.  
    3. Private Sub txtHomeNotes_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    4. [COLOR=Blue]If bCancel = False Then[/COLOR]
    5.     If Len(txtHomeNotes.Value) > 250 Then
    6.         MsgBox "Please enter a maximum of 250 characters in the notes box", vbExclamation + vbOKOnly, "Notes Error"
    7.         Cancel = True
    8.     End If
    9. [COLOR=Blue]End If[/COLOR]
    10. End Sub
    11.  
    12. Private Sub cmdCancel_Click()
    13. [COLOR=Blue]bCancel = True[/COLOR]
    14. Unload Me
    15. End Sub

    It appears that the click event on the Cancel button is processed before the txtAdjust exit event.

Posting Permissions

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



Click Here to Expand Forum to Full Width