Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dates in if statements 2

Status
Not open for further replies.

harrymossman

Technical User
Sep 5, 2002
255
US
I'm trying to check whether the user is matching dates and fiscal years correctly. The code below gives an error message no matter what date is entered, e.g. 7/1/2003. Brain slow today. What am I doing wrong?

Harry

Code:
if substr(Year.value,1,2) ="03" then

if self.value < 10/1/2002 or self.value > 9/30/2003 then

msgInfo(&quot;Stop&quot;,&quot;Date is outside fiscal year shown above.&quot;)

endIf
endIf
 
HarryMossman.

Not sure why you would be getting an error message, unless self.value is not a date field, if not try date(self.value).

I'm not crazy about the code substr(Year.value,1,2) =&quot;03&quot; if year.value is a date field then I would suggest using
if year(year.value) = &quot;2003&quot; then


Hope this helps
Perrin
 
Self.value is definitely a date. Year.value is not. It is a badly planned alpha field that starts with 2 digits to represent the fiscal year. Thanks for your ideas though.
 
Not a Paradox error message. The message that I provided:
&quot;Stop&quot;,&quot;Date is outside fiscal year shown above.&quot;

The code is in CanDepart. Does this seem like the right place for it?
 
HarryMossman,

Guess my brains a little slow today too.

Paradox isn't recognizing the string as a date, try this.

if self.value < date(&quot;10/1/2002&quot;) or self.value > date(&quot;9/30/2003&quot;) then

Perrin
 
Skipped past your last question.

CanDepart is ok but is called every time you attempt to depart a field, this may or may not be desired. ChangeValue is usually more appropriate because it is only called when a user enters a new value into the field.

Perrin
 
Tried:
Code:
if self.value < date(&quot;10/1/2002&quot;) or self.value > date(&quot;9/30/2003&quot;) then
Still got my error message.

Tried the reverse approach:
Code:
if self.value > 9/30/2002 and self.value < 10/1/2003 then
if substr(Year.value,1,2) <> &quot;03&quot; then
This lets everything slide through - both correct and incorrect fiscal years.

Harry
 
Harry,

We're missing something simple here, the code worked fine for me once I declared the string as a date. without the date() function Paradox is treating the string as a number.

Perrin
 
Something wrong with the if statement apparently. The following inelegant code works:
Code:
method canDepart(var eventInfo MoveEvent)

var
fy    string
endVar

fy = substr(Year.value,1,2)
;Year is a badly thought-out legacy alpha field starting w/ 2-digit fiscal year. Can't change right now.

if self.isEdit() then

 if not self.isblank() then

  if fy = &quot;00&quot; then

   if self.value < date(&quot;9/30/1999&quot;)  then

msgStop(&quot;Stop!&quot;,&quot;Date Received is outside of fiscal year.&quot;)
eventInfo.setErrorCode(CanNotDepart)

   else

    if self.value > date(&quot;10/1/2000&quot;)  then

msgStop(&quot;Stop!&quot;,&quot;Date Received is outside of fiscal year.&quot;)
eventInfo.setErrorCode(CanNotDepart)

    endIf
   endIf
  endIf

;More years

 endIf
endIf
endMethod

Thanks for your help.
Harry
 
Harry,

Here's something that works on my end:

Code:
var
   dtInput,             ; user's input date
   dtFYBeg,             ; start of the FY
   dtFYEnd    Date      ; end of the FY
   siValue    SmallInt  ; Control value.
   loDateOK   Logical   ; Flag: Is Input Date OK?
   strError   String    ; Message displayed to user.
endVar

   loDateOK = FALSE  ; assume failure to reduce code.
   strError = &quot;&quot;

   ; First, verify that we have a date value.
   try
      dtInput = dateVal( self.Value )
   onFail
      strError = &quot;\&quot;&quot; + String( self.Value ) +
                 &quot;\&quot;&quot; + &quot; is not a date value.&quot;
   endTry

   if strError = &quot;&quot; then  ; skip; there's already an error

      ; Next, calculate the current fiscal year
      if month( Today() ) < 10 then
         dtFYBeg = DateVal( &quot;10/01/&quot; + 
                   String( Year( Today() ) - 1 ) )
         dtFYEnd = DateVal( &quot;09/30/&quot; + 
                   String( Year( Today() ) ) )
      else
         dtFYBeg = DateVal( &quot;10/01/&quot; + 
                   String( Year( Today() ) ) )
         dtFYEnd = DateVal( &quot;09/30/&quot; + 
                   String( Year( Today() ) + 1 ) )
      endIf

      ; Verify the input date falls within
      ; the current fiscal year

      if dtInput >= dtFYBeg then
         if dtInput <= dtFYEnd then
            loDateOK = true
         else
            strError = &quot;Your value ( &quot; + String( dtInput ) +
                       &quot; ) falls after the end of &quot;              + 
                       &quot; the current fiscal year.&quot;
         endIf
      else
         strError = &quot;Your value ( &quot; + String( dtInput ) +
                    &quot; ) is before the start of the &quot; + 
                    &quot;current fiscal year.&quot;
      endIf
   endIf

   ; Finally, evaluate the results
   if not loDateOK then
      beep()
      msgStop( &quot;Invalid Date&quot;, strError )
      eventInfo.setErrorCode( peCannotDepart )
   endIf

This particular example should work from the canDepart event of an unlabelled field object. Now, I had to make a few assumptions, but you'll note the heavy use of typecasting to get the various values into the format needed at the moment.

Also, you'll notice I used a specific error to control Paradox in the event of a bad date. When you're overriding the event model, it's best to be as specific as possible.

Finally, you'll note that I tend to use nested IF statements in Paradox. There are two main reasons for this:

1. Paradox doesn't support short-circuit evaluation; it always evaluates all operands in an expression, even when one would fail the expression.

2. Because Paradox doesn't support short-circuit eval, you need to verify that all variables actually have values that can be evaluated. You can work around this by making sure that expressions are only evaluated when there are good values. For example, I skip the date comparison when there isn't even a date variable. Had I not done that, there would have been an uninialized variable error in the comparisons.

I know some of this seems like it's really picky, but learning these quirks is part of ObjectPAL; the quirks make sense, once you understand them. Unfortunately, you don't learn them until you get burned by them and that's primarily why a lot of people get frustrated with the language.

Fortunately, you can still get good help dousing the fires. :)

Hope this helps...

-- Lance
 
Thanks Lance. I do want to learn the right way to do things and forums like this seem to be the only way.

Excellent information that I will attempt to adapt to my app. The user wouldn't necessarily be inputing records for the current fiscal year. (Data entry always behind and moldy records turn up.) The fy code comes from the (badly named) Year field.

What is the difference between date() and dateVal()? They seem to do the same thing.

Harry
 
Harry,

I'm not sure I'm remembering this correctly, but I believe that Date() is more restrictive regarding the format of the input string (e.g. it almost always wants it in MM/DD/YYYY) while dateVal() respects the current date format settings.

Again, I may be mis-membering that.

I do remember that I've always had far more luck using dateVal() instead of Date() when convering from string values.

Hope this helps...

-- Lance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top