Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, all. Hopefully this will be short and sweet... especially since I'm supposed to finish this up today!

    I have a (relatively) simple form I have created in VBA, and which i display in Word. One of the fields/text boxes is supposed to hold the date. For this, I want to leverage the AfterUpdate event to check if the date is valid; if it is not, I want the cursor to go BACK to the text box (named txtDate) for the user to enter a valid date.

    The problem is, the code checks the date and alerts the user, but the SetFocus method doesn't seem to kick in. For example, if I put a bogus date in the text box and tab out of it to go to the next text box, the Value is checked and the error message appears, but the cursor goes right on to the next text box in the tab order.

    Here is the code:

    Private Sub txtDate_AfterUpdate()
    Dim strDate As String
    strDate = txtDate.Value
    If IsDate(strDate) = False Then
    txtDate.SetFocus
    MsgBox "Please enter a valid date.", vbInformation + vbOKOnly, "Invalid Date"
    Else
    txtDate.Value = Format(strDate, "dd-MMM-yy")
    End If
    End Sub
    Can anyone see why it would not be working?

    Thanks in advance for the help...
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  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
    [quote name='Mesaka' post='772191' date='24-Apr-2009 11:05']The problem is, the code checks the date and alerts the user, but the SetFocus method doesn't seem to kick in.[/quote]
    I can only analogize... In HTML forms, navigation between form fields is paused while my validation code runs, but once the code completes, then the navigation continues. You may be having the same problem: your validation codes effectively sets focus, but then VB is completing the navigation. Does VB provide a way to cancel the default navigation? If not, one possible workaround is to schedule the set focus to occur a quarter second in the future, so that it takes effect after the default navigation. There probably are some other options as well.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Instead of using the AfterUpdate event, use the BeforeUpdate event. The event procedure has a Cancel argument. If you set this to True, the focus will remain in the text box - no need to use SetFocus:

    Code:
    Private Sub txtDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
      Dim strDate As String
      strDate = txtDate.Value
      If IsDate(strDate) = False Then
    	MsgBox "Please enter a valid date.", vbInformation + vbOKOnly, "Invalid Date"
    	Cancel = True
      Else
    	txtDate.Value = Format(strDate, "dd-MMM-yy")
      End If
    End Sub

  4. #4
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sweet! Once again, you guys make me look good.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

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