RPGPGM.
COM - From AS400 to IBM i
Advice about programming, operations, communications, and anything else I can think of
This blog is about IBM i for Power
Home About this blog List of all posts IBM i user groups Legal notices Privacy policy Search this site
F r i d a y, S e p t e m b e r 2 0 , 2 0 1 3
Validating dates in RPGLE
I received an email asking me the following question:
What about date validation?
Dates on a screen are usually displayed as MMDDYY. I
don't need to add days or months to it, I need to
ensure that the date itself is valid.
I can think of two approaches of displaying and validating dates on a display file.
Use a date field.
Use a 6,0 numberic field.
Below is a simple display file I have created for this post:
01 A DSPSIZ(24 80 *DS3)
02 A INDARA
03 A R SCREEN
04 A 6 3'Date 1 .'
05 A ZDATE1 L B 6 13DATFMT(*MDY)
06 A 7 3'Date 2 .'
Contact Form
07 A ZDATE2 6 0B 7 13EDTCDE(Y)
08 A EDTMSK(' & & ') Name
09 A 50 ERRMSG('Date 2 is not +
10 A valid')
Email *
The INDARA on line 2 means that I will be using a Indicator Data Structure in my RPGLE/RPG IV
program rather than number indicators. To learn more about the Indicator Data Structure read
the post No More Number Indicators. Message *
On line 5 I have defined the field ZDATE1 as a date data type field, L in Date Type field of the
DDS. I do not give the length of a date data type field, just the type. The format of the date is
defined with the DATFMT in the Functions field of the DDS.
On line 6 I have defined the field ZDATE2 as a numeric field six long with no decimals (6,0). I
Send
am using EDTCDE(Y) to format the number with slashes (/) in it.
On line 7 I am using the EDTMSK to control how ZDATE2 is entered. The EDTMSK(' & & ')
means that after entering the first two characters, month, the cursor automatically moves to
the fourth position to allow entry of the next two characters, day, and then the cursor moves
automatically to the seventh position to allow the entry of the year.
On line 8 is the error that will be displayed when the equivalent of indicator 50 is turned "on"
in the RPGLE/RPG IV program.
I am not going to describe the code to do with the Indicator Data Structure in the RPGLE/RPG
IV program below, as you should have learn about that from the No More Number Indicators
post.
01 FTESTDSPF CF E WORKSTN indds(IndDs)
02 D IndDs DS qualified
03 D ErrDate2 50 50N
/free
04 ZDATE1 = %date() ;
Links to useful sites
05 dow (1 = 1) ;
06 exfmt SCREEN ;
1 Getting started with IBM i
07 test(de) *mdy ZDATE2 ;
08 if (%error) ; 2 IBM Documentation for 7.5
09 IndDs.ErrDate2 = *on ;
10 else ; IBM Documentation for 7.4
3
11 IndDs.ErrDate2 = *off ;
12 leave ;
4 IBM Documentation for 7.3
13 endif ;
14 enddo ;
5 IBM Documentation for 7.2
15 *inlr = *on ;
6 IBM Documentation for 7.1
On line 4 I have had to move a date into the ZDATE1 field. If I did not I would get the 'The
PowerSystems Redbooks (inc.
year postion of a Date or Timestamp value is not in the correct range' error, RNQ0114, when 7
IBM i)
the display file's record format SCREEN was displayed, on line 6.
When the record format SCREEN is displayed it looks like:
Popular Posts: Last
30 days
Date 1 . 09/20/13
Date 2 . 0/00/00 1 Modernize your CL too
2 Getting the SQL statement
out of Query/400
If an invalid date is entered into the first field, ZDATE1, as the error is handled by the display
file the following error message is displayed: 'Field value is not an allowed date. Reason code Getting information about
3
9.'. User Profiles using SQL
Defining variables in RPG all
If a number, which is not a valid date, is entered into the second field, ZDATE2, then the 4
free
RPGLE/RPG IV program performs the validation on line 7 using the TEST(DE) operation code.
The TEST operation code is used to validate dates, times, and timestamps. The D in the 5
Handling variable length
variables in CL
parentheses (or brackets if you are not using American English) denotes that I am testing for a
date, and E is the error flag to turn on the %error indicator. The next part is the date format, in
our case *MDY, and finally the field I am validating, ZDATE2. If the number is not a valid *MDY
date then the %error indicator is turned on. Popular Posts: All
Time
On line 8 I test for the %error indicator, if it is on I execute line 9, whuich turns on the
indicator IndDs.ErrDate2, which is the equivalent of indicator 50 in the display file, see line 3.
Indicator 50 conditions the display of the error message in the display file: 'Date 2 is not valid'. 1
Defining variables in RPG all
free
If there is no error then Inds.ErrDate2 is turned off, and I exit the DO-loop to continue
Example subfile program
processing. 2
using modern RPG
Which of these two methods is my preference? Run SQL statements in your
3
CL
I prefer and use the second method, using a numeric field on the display file which I then
Defining Procedures in RPG all
validate as a date. I do this as I can display a meaningful error to the user if the date is 4
free
invalid. in my opinion a message like 'From date is invalid' is easier for a user to understand
what is wrong, rather than 'Field value is not an allowed date. Reason code 9.'. 5 File definition in RPG all free
Do you know of a method that you think is better? If so please add a Comment below
6 Email IFS files
describing how you would do it.
You can learn more about these from IBM's website: 7
Read, write, and update a file
in CL
RPGLE TEST(DE)
Creating PDF files from spool
8
Display file EDTMSK files
Getting information about
9
User Profiles using SQL
This article was written for IBM i 7.1, and it should work with earlier releases too.
Handling variable length
10
variables in CL
Written by Simon Hutchinson at 6:37 PM
Labels: programming, rpg Labels
1 CL
No comments:
DSPF
2
Post a Comment 3
HISTORY
To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here. 4 IFS
Some people have reported that they cannot post a comment using certain computers and browsers. If LF
5
this is you feel free to use the Contact Form to send me the comment and I will post it for you, please
include the title of the post so I know which one to post the comment to.
6 NEW RELEASE
NODE.JS
7
8 OPERATIONS
Newer Post Home Older Post
9 PF
PROGRAMMING
10
QUERY
11
12 RPG
13 SHARED
SQL
14
15 USER GROUP
16 VIRTUAL USER GROUP
Blog Archive
► 2022 (81)
►
► 2021 (90)
►
► 2020 (92)
►
► 2019 (89)
►
► 2018 (70)
►
► 2017 (92)
►
► 2016 (85)
►
► 2015 (86)
►
► 2014 (94)
►
▼ 2013 (59)
▼
► December (5)
►
► November (11)
►
► October (8)
►
▼ September (8)
▼
Alternative to SELECT is IF-
ELSEIF
Validating dates in RPGLE
More SQL and dates part II
More SQL and dates part I
Renaming one field in a file
in RPG
Externally described Data
Structures
Equivalent of SETLL using
SQL
CHAIN versus SETLL the
results
► August (9)
►
► July (8)
►
► June (10)
►
Copyright © 2013-2022 Simon Hutchinson. All rights reserved.
View mobile site
Powered by Blogger.