Calling BAPIs from Visual Basic
This example illustrates a BAPI call in Visual Basic using the BAPI ActiveX Control. This report uses the service BAPI BapiService.MessageGetDetail(), to display the short text and the long text of error messages.
'
' Visual BASIC 5.0
' Copyright SAP AG Walldorf Juli 1998
'
' read a message short and longtext using the BAPI
' BAPI_MESSAGE_GETDETAIL of the object BapiService
' constant for user identification
Const cstrMUsrClient As String = "000"
Const cstrMUsrUser As String = "MYUSER"
Const cstrMUsrPassword As String = "MYPASS"
Const cstrMUsrLanguage As String = "EN"
' constant for system identification
Const cstrMSysSystem As String = "P45"
Const cstrMSysMessageServer As String = "p45main.wdf.sap-ag.de"
Const cstrMSysGroupName As String = "PUBLIC"
'
' constant values for reading message texts
Const cstrMMsgId As String = "SX"
Const cstrMMsgNumber As String = "101"
Const cstrMMsgVariable1 As String = "var1"
Const cstrMMsgVariable2 As String = "var2"
Const cstrMMsgVariable3 As String = "var3"
Const cstrMMsgVariable4 As String = "var4"
Const cstrMMsgLanguage As String = "DE"
' other constant
Const cstrMPathfile As String = "D:\A\saptext.rtf"
' password for login in R/3
Dim strMUsrPassword As String
' react on button START
Private Sub cmdMsgStart_Click()
' define object for BAPI ActiveX control
Dim oBAPICtrl As Object
' define object for R/3 logon control
Dim oLogonCtrl As Object
' business object BapiService
Dim boBapiSercice As Object
' for BAPI: BapiService.MessageGetDetail
Dim oMsgReturn As Object
Dim oMsgText As Object
Dim intCounter As Integer
' to open the file you need a file channel
Dim intChannel As Integer
' create BAPI ActiveX control object
Set oBAPICtrl = CreateObject("SAP.BAPI.1")
' create R/3 logon control object
Set oLogonCtrl = CreateObject("SAP.Logoncontrol.1")
' connection object is part of the BAPI ActiveX Control object
Set oBAPICtrl.Connection = oLogonCtrl.NewConnection
' fill logon parameters for system to use
oBAPICtrl.Connection.System = txtSysSystem
oBAPICtrl.Connection.MessageServer = txtSysMessageServer
oBAPICtrl.Connection.GroupName = txtSysGroupName
' fill logon parameter for user
oBAPICtrl.Connection.Client = txtUsrClient
oBAPICtrl.Connection.User = txtUsrUser
oBAPICtrl.Connection.Password = strMUsrPassword
oBAPICtrl.Connection.Language = txtUsrLanguage
' user logon to R/3
If oBAPICtrl.Connection.Logon(frmStart.hWnd, False) = False Then
MsgBox "R/3 connection failed"
End
End If
' create BAPI service object
Set boBapiService = oBAPICtrl.GetSAPObject("BapiService")
' call method of BapiService
boBapiService.MessageGetDetail id:=txtMsgId, _
Number:=txtMsgNumber, _
Language:=txtMsgLanguage, _
Textformat:=cboMsgTextformat.Text, _
message:=strMsgShorttext, _
Return:=oMsgReturn, _
Text:=oMsgText
' fill field in form
' If txtMsgShorttext = "" Then
' MsgBox "No message read"
' End If
' user logoff from R/3
oBAPICtrl.Connection.Logoff
' error handling check if RETURN parameter is not empty and react
If oMsgReturn.Value("TYPE") <> "" Then
lblReturn.Caption = oMsgReturn.Value("TYPE") + _
". " + _
oMsgReturn.Value("ID") + _
". " + _
oMsgReturn.Value("NUMBER") + _
". " + _
oMsgReturn.Value("MESSAGE") + _
". " + _
oMsgReturn.Value("MESSAGE_V1") + _
". " + _
oMsgReturn.Value("MESSAGE_V2") + _
". " + _
oMsgReturn.Value("MESSAGE_V3") + _
". " + _
oMsgReturn.Value("MESSAGE_V4") + _
". " + _
oMsgReturn.Value("LOG_NO") + _
". " + _
oMsgReturn.Value("LOG_MSG_NO")
Else
' fill form fields
txtMsgShorttext = strMsgShorttext
arrayText = oMsgText.Data
' handling of non RTF texts
If cboMsgTextformat.Text <> "RTF" Then
For intCounter = 1 To oMsgText.RowCount
If intCounter = 1 Then
rtfMsgLongtext.Text = arrayText(intCounter, 1)
Else
rtfMsgLongtext.Text = rtfMsgLongtext.Text + _
Chr(13) + Chr(10) + _
arrayText(intCounter, 1)
End If
Next intCounter
End If
' handling of RTF texts
If cboMsgTextformat.Text = "RTF" Then
' save text as rtf file
intChannel = FreeFile
Open cstrMPathfile For Output As #intChannel
For intCounter = 1 To oMsgText.RowCount
Print #intChannel, arrayText(intCounter, 1)
Next intCounter
Close #intChannel
rtfMsgLongtext.LoadFile cstrMPathfile, rtfRTF
End If
End If
End Sub