Understanding Generated Code 

This section presents a generated program. In this example, the recording user uses transaction PA20 (Display Human Resources Master Data) to perform two tasks:

In the course of the session, the recorder requests input and output variables, a second subroutine ("Maternity") for the Maternity Protection display, and message checking code. The message checking code is requested at the point where, should a user enter a male employee, the system responds with a message that maternity protection applies only to females.

You can always display and edit the generated program in Windows‘ Notepad, as well as in Visual Basic, HAHTtalk Basic or Object Pascal.

 

‘’ ----------------------------------------------------------------------
‘’ The ScreenCheck routine is always generated, and checks
‘’ whether the next screen recorded in the program is in fact the
‘’ next screen generated by the runtime system.
‘’ ----------------------------------------------------------------------
Private Sub ScreenCheck(ByVal ProgName As String, _
ByVal ScrName As String)
If Sap.ProgramName <> ProgName Or _
(Len(ScrName) > 0 And Sap.ScreenName <> ScrName) Then
MsgBox "Unexpected screen " & ProgName & " " & ScrName, vbCritical, "Playback"
Stop
End If
End Sub

‘’ ----------------------------------------------------------------------
‘’ The OKCheck routine is always generated, and checks the
‘’ value of a Boolean. This routine is most often used to check the
‘’ value of the bOK variable (return code variable for calls to the
‘’ SAP System).
‘’ ----------------------------------------------------------------------
Private Sub OKCheck(ByVal bIsOK As Boolean, ByVal sMsg As String)
If Not bIsOK Then
MsgBox sMsg, vbCritical, "Playback"
Stop
End If
End Sub

‘’ ------------------------------------------------------------------------
‘’ Default subroutine begun, and variables/controls set up
‘’ ------------------------------------------------------------------------
Sub RecordedMacro()
Dim iCtrl As Integer
‘ Control index variable
Dim bOK As Boolean
‘ Return code variable
' Uncomment following 2 lines if Sap is not declared globally
' Dim Sap As Object
' Set Sap = CreateObject("SapTerminal.Event")

‘’ ------------------------------------------------------------------------
‘’ The recorder logs on, and the resulting SAP interface is
‘’ positioned on the screen.
‘’ ------------------------------------------------------------------------
bOK = Sap.Connect("orlando1", "61", SapGuiMerlin)
OKCheck bOK, "Error in opening connection"

Sap.RowDimension = 24
Sap.RowListDimension = 24
Sap.ColumnDimension = 80
Sap.ColumnListDimension = 80
Sap.SetSizeFlag = True
OKCheck bOK, "Error in opening connection"

' txtPassword is placeholder for actual password value
bOK = Sap.Logon("", "good", txtPassword, "")
OKCheck bOK, "Error in logon"
ScreenCheck "SAPMSYST", "0040"


‘’ ------------------------------------------------------------------------
‘’ The recorder calls transaction PA20.
‘’ ------------------------------------------------------------------------
Sap.OKCode = "/npa20"
bOK = Sap.SendEvent
OKCheck bOK, "Error in sending default key"
ScreenCheck "SAPMP50A", "1000"

‘’ ------------------------------------------------------------------------
‘’ The recorder selects the Personnel Number field
‘’ ------------------------------------------------------------------------
iCtrl = Sap.FindByField("RP50G-PERNR", 0, SapMatch)
' Personnel number (index 1)

‘’ ------------------------------------------------------------------------
‘’ The recorder requests an input variable definition for the
‘’ Personnel Number field in the VB program.
‘’ ------------------------------------------------------------------------
Dim PersonnelNumber As String
bOK = Sap.SetControlValue(iCtrl, PersonnelNumber)
OKCheck bOK, "Error in setting input variable"

‘’ ------------------------------------------------------------------------
‘’ Input variable definition for a TxtPers field.
‘’ ------------------------------------------------------------------------
Dim txtPers As String
bOK = Sap.SetControlValue(iCtrl, txtPers)
OKCheck bOK, "Error in setting input variable"

‘’ ------------------------------------------------------------------------
‘’ Recorder enters "1502" in Personnel Number field
‘’ ------------------------------------------------------------------------
bOK = Sap.SetControlValue(iCtrl, "1502")
OKCheck bOK, "Error in setting text value"

‘’ ------------------------------------------------------------------------
‘’ Recorder selects "Addresses" data for the employee
‘’ ------------------------------------------------------------------------
iCtrl = Sap.FindByValue("Addresses", 0, SapAnyType, SapLeft) ' index 18
bOK = Sap.SetControlSelected(iCtrl, True)
OKCheck bOK, "Error in setting selected value"

‘’ ------------------------------------------------------------------------
‘’ The Code Generator always repositions the cursor, but these statements
‘’ are not needed in this case. (You can delete them.)
‘’ ------------------------------------------------------------------------
bOK = Sap.SetCursorByControl(iCtrl)
OKCheck bOK, "Error in setting cursor position"

‘’ ------------------------------------------------------------------------
‘’ Recorder requests Display with F2 function key
‘’ ------------------------------------------------------------------------
bOK = Sap.SendKey(vbKeyF2) ' Display
OKCheck bOK, "Error in sending key"
ScreenCheck "MP000600", "2001"

‘’ ------------------------------------------------------------------------
‘’ Recorder requests output variable definitions for address data
‘’ ------------------------------------------------------------------------
iCtrl = Sap.FindByField("P0006-NAME2", 0, SapEdit)
' c/o (index 17)
Dim co As String
OKCheck iCtrl >= 0, "Error in setting output variable"
co = Sap.Controls(iCtrl).Value

iCtrl = Sap.FindByField("P0006-STRAS", 0, SapEdit)
' Street and house no. (index 19)
Dim StreetAndHouseNo As String
OKCheck iCtrl >= 0, "Error in setting output variable"
StreetAndHouseNo = Sap.Controls(iCtrl).Value

iCtrl = Sap.FindByField("P0006-PSTLZ", 0, SapEdit)
' Postal code/City (index 21)
Dim PostalCode As String
OKCheck iCtrl >= 0, "Error in setting output variable"
PostalCode = Sap.Controls(iCtrl).Value

iCtrl = Sap.FindByField("P0006-ORT01", 0, SapEdit)
' Postal code/City (index 22)
Dim city As String
OKCheck iCtrl >= 0, "Error in setting output variable"
city = Sap.Controls(iCtrl).Value

iCtrl = Sap.FindByField("P0006-ORT02", 0, SapEdit)
' District (index 24)
Dim District As String
OKCheck iCtrl >= 0, "Error in setting output variable"
District = Sap.Controls(iCtrl).Value

iCtrl = Sap.FindByField("P0006-LAND1", 0, SapMatch)
' Country key (index 26)
Dim CountryKey As String
OKCheck iCtrl >= 0, "Error in setting output variable"
CountryKey = Sap.Controls(iCtrl).Value

iCtrl = Sap.FindByField("P0006-NAME2", 0, SapEdit) ' c/o (index 17)
bOK = Sap.SetCursorByControl(iCtrl)
OKCheck bOK, "Error in setting cursor position"

‘’ ------------------------------------------------------------------------
‘’ Recorder returns with F3 function key
‘’ ------------------------------------------------------------------------
bOK = Sap.SendKey(vbKeyF3) ' Back
OKCheck bOK, "Error in sending key"
ScreenCheck "SAPMP50A", "1000"

‘’ ------------------------------------------------------------------------
‘’ Recorder terminates default subroutine, and defines a new
‘’ "Maternity" subroutine. All subsequent actions are recorded
‘’
as part of the new subroutine.
‘’ ------------------------------------------------------------------------

End Sub

Sub maternity()
Dim iCtrl As Integer
Dim bOK As Boolean

iCtrl = Sap.FindByField("RP50G-PERNR", 0, SapMatch)
' Personnel number (index 1)
Dim PersonnelNumber As String
bOK = Sap.SetControlValue(iCtrl, PersonnelNumber)
OKCheck bOK, "Error in setting input variable"

‘’ ------------------------------------------------------------------------
’’ Recorder selects Maternity Protection data option
‘‘ ------------------------------------------------------------------------
iCtrl = Sap.FindByValue("Maternity Protection", 0, _
SapAnyType, SapLeft) ' index 23
bOK = Sap.SetControlSelected(iCtrl, True)
OKCheck bOK, "Error in setting selected value"

bOK = Sap.SetCursorByControl(iCtrl)
OKCheck bOK, "Error in setting cursor position"

‘’ ------------------------------------------------------------------------
‘’ Recorder requests Display with F2
‘’ ------------------------------------------------------------------------
bOK = Sap.SendKey(vbKeyF2) ' Display
OKCheck bOK, "Error in sending key"
ScreenCheck "SAPMP50A", "1000"

‘’ ------------------------------------------------------------------------
‘’ Recorder requests message-checking code. If a system msg
‘’ generated at runtime, its text will be assigned to the VB variable
‘’ maternityMsg.
‘’ ------------------------------------------------------------------------
Dim maternityMsg As String
If Sap.MessageFlag Then
' MsgBox Sap.Message
maternityMsg = Sap.Message
End If

iCtrl = Sap.FindByField("RP50G-PERNR", 0, SapMatch)
' Personnel number (index 1)
bOK = Sap.SetCursorByControl(iCtrl)
OKCheck bOK, "Error in setting cursor position"

‘’ ------------------------------------------------------------------------
‘’ Recorder returns with F3
‘’ ------------------------------------------------------------------------
bOK = Sap.SendKey(vbKeyF3) ' Back
OKCheck bOK, "Error in sending key"
ScreenCheck "SAPMSYST", "0040"


‘’ ------------------------------------------------------------------------
‘’ Recorder logs off using Code Generator option File->Logoff
‘’ ------------------------------------------------------------------------
bOK = Sap.Logoff
OKCheck bOK, "Error in logging off"

' Uncomment next line for standalone SAP program
' Sap.Quit
End Sub