📄 ch21.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '</head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
figDoc.write(zhtm);
figDoc.close();
popUpWin.focus();
}
//-->
</SCRIPT>
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0.1">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Sams Teach Yourself ABAP/4® in 21 Days -- Day 21- Selection Screens</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/sams.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/sams.gif" BORDER="0"></H1>
</CENTER>
<CENTER>
<P><A HREF="../ch20/ch20.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch20/ch20.htm"><IMG SRC="../button/previous.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/previous.gif" BORDER="0"></A>
<A HREF="../index.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/index.htm"><IMG SRC="../button/contents.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/contents.gif" BORDER="0"></A>
<A HREF="rev03.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch21/rev03.htm"><IMG SRC="../button/next.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/next.gif" BORDER="0"></A>
<H1>Sams Teach Yourself ABAP/4<sup>®</sup> in 21 Days</H1></CENTER>
<HR SIZE=4>
<H1>Day 21</H1>
<H1>Selection Screens</H1>
<UL>
<LI><A HREF="#ChapterObjectives">
Chapter Objectives</A>
<LI><A HREF="#EventDrivenProgramming">
Event-Driven Programming</A>
<UL>
<LI><A HREF="#UsingtheinitializationEvent">
Using the initialization Event</A>
<LI><A HREF="#UsingtheatselectionscreenEvent">
Using the at selection-screen Event</A>
<LI><A HREF="#UsingtheatusercommandEvent">
Using the at user-command Event</A>
</UL>
<LI><A HREF="#DataValidationUsingForeignKeys">
Data Validation Using Foreign Keys</A>
<LI><A HREF="#DataValidationUsingMatchcodes">
Data Validation Using Matchcodes</A>
<UL>
<LI><A HREF="#SyntaxforUsingaMatchcode">
Syntax for Using a Matchcode</A>
</UL>
<LI><A HREF="#FormattingSelectionScreens">
Formatting Selection Screens</A>
<LI><A HREF="#UsingselectionscreenStatements">
Using selection-screen Statements</A>
<UL>
<LI><A HREF="#Syntaxforselectionscreenblockwithframe">
Syntax for selection-screen block with frame</A>
</UL>
<LI><A HREF="#SelectionScreenParameters">
Selection Screen Parameters</A>
<LI><A HREF="#SelectionScreencheckbox">
Selection Screen checkbox</A>
<UL>
<LI><A HREF="#Syntaxforselectionscreencheckbox">
Syntax for selection-screen checkbox</A>
</UL>
<LI><A HREF="#SelectionScreenselectoptions">
Selection Screen select-options</A>
<UL>
<LI><A HREF="#Syntaxforselectoptions">
Syntax for select-options</A>
</UL>
<LI><A HREF="#SelectionScreenRadioButtons">
Selection Screen Radio Buttons</A>
<UL>
<LI><A HREF="#SyntaxforaTTFONTSIZEradiobuttonFONTTTFONTSIZEgroupFONT">
Syntax for a <TT>radiobutton</TT> group</A>
</UL>
<LI><A HREF="#SelectionScreenExampleProgram">
Selection Screen Example Program</A>
<LI><A HREF="#UsingtheMessageStatement">
Using the Message Statement</A>
<UL>
<LI><A HREF="#SyntaxforthemessageidStatement">
Syntax for the message-id Statement</A>
</UL>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#QampABR">
Q&A<BR>
</A>
<LI><A HREF="#Workshop">
Workshop</A>
<UL>
<LI><A HREF="#Quiz">
Quiz</A>
<LI><A HREF="#Exercise">
Exercise 1</A>
</UL></UL>
<HR>
<H2><A NAME="ChapterObjectives"><FONT SIZE=5 COLOR=#FF0000>
Chapter Objectives</FONT></A></H2>
<P>
After you complete this chapter, you should be able to
<UL>
<LI>Code selection screens that interact with the user.
<LI>Understand how foreign keys can help validate user input.
<LI>Understand how matchcodes are used in ABAP/4 programs.
<LI>Use formatting elements to create efficient, well-designed
selection screens.
<LI>Use selection screens to maintain data integrity.
<LI>Use messages to provide effective communication.
</UL>
<H2><A NAME="EventDrivenProgramming"><FONT SIZE=5 COLOR=#FF0000>
Event-Driven Programming</FONT></A></H2>
<P>
In this day and age, with the emergence of the World Wide Web
and all that it entails, program code must be capable of interacting
and communicating with the end user. This is done in ABAP/4 using
events that are invoked by the users' actions.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
Processing blocks are defined by <I>event keywords</I> and are
thus executed on the invocation of certain relevant events.
<P>
By default, the event <TT>start-of-selection</TT> is attached
to all events in ABAP/4. In your programs you can define a processing
block and attach this block to an event keyword.
<P>
For generally easy-to-read code, it is good practice to define
sequential processing blocks in the order by which they will most
likely be triggered during selection screen execution. It is also
good practice to utilize the most important events for selection
screen programming. These events are as follows:
<UL>
<LI>The <TT>initialization</TT> event
<LI>The <TT>at selection-screen</TT> event
<LI>The <TT>at user-command</TT> event
</UL>
<H3><A NAME="UsingtheinitializationEvent">
Using the initialization Event</A></H3>
<P>
The following code shows the syntax for the <TT>initialization</TT>
event:
<BLOCKQUOTE>
<PRE>
report ywhatyr.
tables: marc, mvke.
..
data: p_year for sy-datum.
initialization.
if sy-datum ge '01012000'
p_year = '2000'.
else.
p_year = 'Yesteryear'.
endif.
</PRE>
</BLOCKQUOTE>
<P>
In this example, when the program executes for which a selection
screen is defined, this <TT>initialization</TT> processing block
is executed, setting the parameter field <TT>p_year</TT> equal
to a value depending on the system date at the moment of execution.
It is this block in which you specify the initial default values
of your selection screen based on whatever criteria is necessary
to maintain data integrity of user input. Some examples include
setting title bars, assigning text elements to graphical user
interface (GUI) elements, and function code status.
<P>
You have seen how the <TT>initialization</TT> event works, so
now it's time to take a closer look at some uses of the <TT>at
selection-screen</TT> and <TT>at user-command</TT> events.
<H3><A NAME="UsingtheatselectionscreenEvent">
Using the at selection-screen Event</A></H3>
<P>
The <TT>at selection-screen</TT> event is processed after user
input on the active selection screen. This can occur when the
user presses a function key or clicks a pushbutton, as well as
a host of other elements that can be interacted on by the user.
In addition to data validation checks, warning messages, GUI status
change, or even pop-up windows can be called using the <TT>at
selection-screen</TT> event. You will look at more examples of
these later in the chapter when you look at formatting selection
screens and the events involved with screen formatting elements.
For now, however, let's take a look at a pushbutton on a selection
screen and how the events <TT>at selection-screen</TT> and <TT>at
user-command</TT> are used with this next example.
<H3><A NAME="UsingtheatusercommandEvent">
Using the at user-command Event</A></H3>
<P>
Pushbuttons, as well as many other event-driven selection screen
options, can be very useful in maintaining user interaction and
validating user input. In this next section, you will explore
how to use pushbuttons to invoke the <TT>at user-command</TT>
event and look at an example of how pushbuttons can be used to
process user input.
<H4>Syntax for the selection-screen pushbutton Event</H4>
<P>
The following code shows the syntax for the <TT>selection-screen
pushbutton</TT> event:
<BLOCKQUOTE>
<PRE>
selection-screen pushbutton example1 user-command 1234.
</PRE>
</BLOCKQUOTE>
<P>
This statement, when used together with the <TT>at selection-screen</TT>
command, is a great way to interact with the user as he enters
data. The syntax is similar to that of a <TT>selection-screen
comment</TT> except that data is passed when the user presses
the button. Pushing the button triggers <TT>sccrfields-ucomm</TT>
in the <TT>at selection-screen</TT> event and the input fields
are imported. This data can then be validated and the user issued
a message depending on the purpose of the button. This is an example
of how you can use two pushbuttons to determine which language
to report selected data in.
<P>
The following code shows the syntax for two pushbuttons that are
used to choose a language:
<BLOCKQUOTE>
<PRE>
selection-screen pushbutton 10(20) text-003 user-command engl.
selection-screen pushbutton 50(20) text-004 user-command germ.
at selection-screen.
at user-command.
case sy-ucomm.
when 'engl'.
lang-english = 'Y'.
when 'germ'.
lang-german = 'Y'.
endcase.
</PRE>
</BLOCKQUOTE>
<P>
In this example, you can check which of the two pushbuttons were
pressed by the user by using a <TT>case</TT> statement (see Figure
21.1). When the user triggers the <TT>at user-command</TT> event,
the field <TT>sy-ucomm</TT> holds the unique four-byte name of
the item that the user selected. In this way, you can code various
data validations or command user input based on the combination
of data entered and the items, in this case pushbuttons, selected
by the user.
<P>
<A HREF="javascript:popUp('f21-1.gif')"><B>Figure 21.1 : </B><I>A printout of the pushbuttons</I>.</A><P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
You can also create up to five pushbuttons on the Application toolbar. These buttons on the Selection Screen toolbar are associated to specific function keys and are restricted to function keys 1 through 5.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
Now that you have learned a little about data validation using
events in ABAP/4, it is time to look at some data validation techniques
that are more system maintained and defined. You will first look
at foreign keys. A <I>foreign key</I> is one or more fields that
represent the primary key of a second table.
<H2><A NAME="DataValidationUsingForeignKeys"><FONT SIZE=5 COLOR=#FF0000>
Data Validation Using Foreign Keys</FONT></A></H2>
<P>
The SAP environment's relational data model can contain many tables,
views, structures, and linked tables. Foreign keys, it can be
said, define these relationships between multiple tables. The
functions that foreign keys perform include providing help data
and creating dictionary objects, but the one that you will focus
on will be in the data validation area. After all, maintaining
data integrity is one of the main goals when defining selection
screens and the most important use of foreign keys.
<P>
A foreign key field is, by definition, restricted to values that
correspond to those of the primary key of the input field's check
table. This is how the link between the two tables is made. One
table, <TT>FORKEY1</TT>, can be thought of as the foreign key
(or dependent) table mainly because it includes foreign key fields
that are assigned to primary key fields in <TT>CHECK1</TT>, which
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -