Calling BAPIs from VB6
資料來源: SAP官方網站
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | ' 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:Asaptext.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 + _ C hr(13) + C hr(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 |