Simple Function doesn't maintain focus when condition met 
Author Message
 Simple Function doesn't maintain focus when condition met

I have a simple little piece of code (that's causing me to pull my hair
out).  Basically, I have a "date" field.  The user is to enter a date,
the date can only be a "tuesday" date.
If they enter any other date, and the day is not a tuesday, they receive
an error message that tells them that they must enter a tuesday date.

Here's the code:

  Private Sub Date_AfterUpdate()

    If Format([Date], "dddd") <> "Tuesday" Then
        MsgBox "You must enter a TUESDAY date", vbExclamation, vbOK
    Else
        [AssocID].set focus
     End If

End Sub

The problem is that even though a wrong date is entered and the message
box appears the user is not forced to stay in that field until the date
is = a tuesday date.

What am I missing here?  I appreciate the help.

Thanks
Cathi



Fri, 20 Oct 2000 03:00:00 GMT  
 Simple Function doesn't maintain focus when condition met

Try this in your AfterUpdate and Exit events, you'll have to declare
bStayHere as Boolean in the declarations section of your module:

Private Sub ctlDate_AfterUpdate()
    If Format(ctlDate, "dddd") <> "Tuesday" Then
        MsgBox "You must enter a TUESDAY date", vbExclamation, vbOK
        bStayHere = True
    Else
        bStayHere = False
        ctlNextControl.SetFocus
     End If
End Sub

Private Sub ctlDate_Exit(Cancel As Integer)
    If bStayHere Then
        Cancel = -1
    End If
End Sub

BPA

Quote:

>  I have a simple little piece of code (that's causing me to pull my
> hair out).  Basically, I have a "date" field.  The user is to enter a
> date, the date can only be a "tuesday" date.
> If they enter any other date, and the day is not a tuesday, they
> receive an error message that tells them that they must enter a
> tuesday date.

> Here's the code:

>   Private Sub Date_AfterUpdate()

>     If Format([Date], "dddd") <> "Tuesday" Then
>         MsgBox "You must enter a TUESDAY date", vbExclamation, vbOK
>     Else
>         [AssocID].set focus
>      End If

> End Sub

> The problem is that even though a wrong date is entered and the
> message box appears the user is not forced to stay in that field until
> the date is = a tuesday date.

> What am I missing here?  I appreciate the help.

> Thanks
> Cathi



Sat, 21 Oct 2000 03:00:00 GMT  
 Simple Function doesn't maintain focus when condition met

This is what the BeforeUpdate event is for.  Try the following code:

Private Sub txtDate_BeforeUpdate(Cancel As Integer)
    If WeekDay(txtDate) <> vbTuesday Then
        MsgBox "You must enter a TUESDAY date", vbExclamation, vbOK
        Cancel = True
    Else
        AssocID.SetFocus
    End If
End Sub

I took the liberty of a couple of other programming pointers above also:
1. Used the WeekDay function.
2. Changed name of control from "Date".  In Access 95/97 Date is a function that returns the current date and is a data type, so you could get into trouble if you're not careful.
3. Also if AssocID is the next control in the tab order you could leave out the Else clause maybe.

Alden

I have a simple little piece of code (that's causing me to pull my hair out).  Basically, I have a "date" field.  The user is to enter a date, the date can only be a "tuesday" date.
    If they enter any other date, and the day is not a tuesday, they receive an error message that tells them that they must enter a tuesday date.
    Here's the code:

      Private Sub Date_AfterUpdate()

        If Format([Date], "dddd") <> "Tuesday" Then
            MsgBox "You must enter a TUESDAY date", vbExclamation, vbOK
        Else
            [AssocID].set focus
         End If

    End Sub

    The problem is that even though a wrong date is entered and the message box appears the user is not forced to stay in that field until the date is = a tuesday date.

    What am I missing here?  I appreciate the help.

    Thanks
    Cathi



Sat, 21 Oct 2000 03:00:00 GMT  
 Simple Function doesn't maintain focus when condition met

Test in the BeforeUpdate event, that is what it's for, you can then set the cancel parameter to true if your validation fails and
that will keep the focus on the current control.

    I have a simple little piece of code (that's causing me to pull my hair out).  Basically, I have a "date" field.  The user is to enter a date, the date can only be a "tuesday" date.
    If they enter any other date, and the day is not a tuesday, they receive an error message that tells them that they must enter a tuesday date.
    Here's the code:

      Private Sub Date_AfterUpdate()

        If Format([Date], "dddd") <> "Tuesday" Then
            MsgBox "You must enter a TUESDAY date", vbExclamation, vbOK
        Else
            [AssocID].set focus
         End If

    End Sub

    The problem is that even though a wrong date is entered and the message box appears the user is not forced to stay in that field until the date is = a tuesday date.

    What am I missing here?  I appreciate the help.

    Thanks
    Cathi



Sun, 22 Oct 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Report doesn't maintain page setup

2. Timer Event won't fire when app doesn't have focus

3. Timer Event won't fire when app doesn't have focus

4. I'm trying to use 2 conditions with variables in a DMax function

5. Label, Colour Change, If Condition Met

6. Getting a UserForm to open if certain condition is met

7. Count if Condition Met in Crystal

8. Count if Condition Met in CR

9. dynamic image until a condition is met

10. Wait till condition is met:

11. Bold a Row if condition is met

12. Help - Count if Condition Met in Crystal

 

 
Powered by phpBB® Forum Software