×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Integrating SAP and vb

Integrating SAP and vb

Integrating SAP and vb

(OP)
Hi all,

Assuming to have successfully installed DCOM (tested to be OK with the sample vb programs that comes with the package), do you know how I can export the result (a table) of an ABAP report (ZSDREP01) into visual basic, as an ADO recordset?

Within visual basic, I should call the ABAP report, passing to it the relevant selection fields (for instance, date from - to, sales organization, sales office) and then get the resulting table into vb. Do you know how to implement it? Which are the BAPI's to use?

Thank you very much indeed for any help.

Flavio

fc158@yahoo.com

RE: Integrating SAP and vb

Hi

First make a .DLL with DCOM using a table read BAPI.
You will need to do a BAPI search in SAP to find the exact name in your SAP instance.

To use the BAPI there are Three parts.
Part 1.  Get a SAP session in the VB program.
Part 2.  Define the table, fields, and selection criteria
Part 3.  Convert the delimited data into a recordset.

I have several examples if you want.

Good luck
Paul    

RE: Integrating SAP and vb

Paul,

  I would love to see some examples of this...specifically within the context of table reads using BAPI....I would appreciate any info, posted here or sent to my email address at pcardoso@pcdevsolutions.com

Thanks in advance!

Pedro Cardoso
A.K.A CanadianTechie

RE: Integrating SAP and vb

(OP)
Hi Paul,
Thank you for your reply.
Well, I would be very very interested in the examples you mention. If you could post them here it will be fine.

Thank you very much indeed !
Flavio

RE: Integrating SAP and vb

Hi Paul,

I would like to get examples as well. Appreciate if you could send to my email at info@ssw.ie

Thanks in advance,
Andrew

RE: Integrating SAP and vb

Hi Paul,
Can you please send the examples to me (pohling@idt.com) as well.
Thanks.

regards,
Poh Ling

RE: Integrating SAP and vb

Hi Paul would you mind sending me a copy of the examples as well (pclark@blount.ca).

Thanks in advance,
Peter

RE: Integrating SAP and vb

Hello Paul

If it's not too much trouble I too would appreciate a copy.

Thanks for sharing!

stu.mills@necsam.com

RE: Integrating SAP and vb

I'm interested as well...

Can you just paste the examples here...

Thanks,
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.

RE: Integrating SAP and vb

Here is a code sample we have put together to use BAPI from MS Access, but VB concept should hold.  This sample logs into SAP, adds records and retrieves SAP document numbers from uploaded records.  Getting any examples of this from anywhere is a real nightmare.

Hope this helps people.


Option Compare Database
Option Explicit
'Constants
Const mstrSAPLogicalSystem As String = "FinCBE"
Const mstrSAPInvoiceObjType As String = "IBKPF"
Const mstrSAPInvoiceDocType As String = "KR"
Const mstrSAPCreditDocType As String = "KG"
Const mstrSAPAccountKey As String = "VST"
Const mstrSAPInvoiceCurrency As String = "GBP"
Const mstrSAPReversalReason As String = "Z1"

'BAPI Controls
Public oBAPICtrl As Object
Public oSAPConnection As Object

'BAPI functions
Dim oPostInvoice As Object
Dim oBAPIService As Object
Dim oVendorStatement As Object

'Input structures
Dim oDocumentHeader As Object
Dim oAccountPayable As Object
Dim oAccountGL As Object
Dim oAccountTax As Object
Dim oCurrencyAmount As Object
Dim oReverseInvoice As Object

'Output Structures
Dim oInvoiceReturn As Object
Dim oCommitReturn As Object
Dim oStmtReturn As Object
Dim oLineItems As Object

' Dummy structure to hold initialised BAPI structures, prevents another call to the BAPI
Type iniBAPI
    DocumentHeader As Object
    AccountPayable As Object
    AccountGL As Object
    AccountTax As Object
    CurrencyAmount As Object
    InvoiceReturn As Object
    CommitReturn As Object
    StmtReturn As Object
    LineItems As Object
    ReverseInvoice As Object
End Type

'Global variables
Public gstrBAPIMessage As String

'Local module variables
Dim mSuccess As Boolean
Dim mLastSAPDoc As String
Dim mdfBAPI As iniBAPI
Dim mvarReturn




Public Function SAPLogin(Optional abUnattended As Boolean = False) As Boolean
' -------------------------------------------------------------------
' MODULE:      SAPLogin
' AUTHOR:      Nick Fry
' NARRATIVE:   Establishes a connection with SAP
' HISTORY:     11-Feb-2005 NF Created
'
' OWNERS:      B&Q PLC
' -------------------------------------------------------------------
'
' Parameters reqd : abAttended - Is this an Attended or Unattended login
    Dim lConnect As Long
    
    Set oBAPICtrl = Nothing
    Set oSAPConnection = Nothing
    Set oPostInvoice = Nothing
    Set oBAPIService = Nothing
    Set oVendorStatement = Nothing
   
    Set oBAPICtrl = CreateObject("SAP.BAPI.1")
    Set oSAPConnection = oBAPICtrl.Connection
    If gstrSAPDebug Then
        oSAPConnection.TraceLevel = gstrSAPTraceLevel
        oBAPICtrl.LogLevel = gstrSAPLogLevel
        oBAPICtrl.LogFileName = gstrSAPLogFile
    End If
    
    oSAPConnection.ApplicationServer = gstrSAPLogonAppServer
    oSAPConnection.Client = gstrSAPLogonClient
    oSAPConnection.Destination = gstrSAPLogonDest
    oSAPConnection.System = gstrSAPLogonSystem
    oSAPConnection.SystemID = gstrSAPLogonSystemID

    If abUnattended Then
        oSAPConnection.User = gstrSAPStagUser
        oSAPConnection.Password = gstrSAPStagPswd
  '  Else
  '      DoCmd.OpenForm ("frmSAPLogin")
    End If
    
    'Perform a remote logon to the R/3 System
    lConnect = 0
    SAPLogin = oSAPConnection.Logon(lConnect, abUnattended)

End Function


Public Function ReverseInvoices(aoRS As ADODB.Recordset, auDoc() As ReturnDoc, abBatch As Boolean, Optional aLog As Integer, Optional asLogFile As String = "")
' -------------------------------------------------------------------
' MODULE:      ReverseInvoices
' AUTHOR:      Nick Fry
' NARRATIVE:   Excute SAP BAPI function to reverse existing invoices
'              from a recordset and return the document numbers
' HISTORY:     11-Feb-2005 NF Created
'
' OWNERS:      B&Q PLC
' -------------------------------------------------------------------
'
' Parameters reqd : abAttended - Is this an Attended or Unattended login
    Dim vObjType, vObjKey, vObjSys
    Dim bCheckReverse As Boolean
    Dim lCount As Long
    Dim lSuccess As Long
    Dim lFailure As Long
    Dim i As Long
    Dim strErr As String
    
    If oPostInvoice Is Nothing Then
        Set oPostInvoice = oBAPICtrl.GetSAPObject("AcctngInvoiceReceipt")
    End If
    
    'Set up general services
    If oBAPIService Is Nothing Then
        Set oBAPIService = oBAPICtrl.GetSAPObject("BapiService")
    End If

    Do Until aoRS.EOF
        ReDim Preserve auDoc(lCount)
        ' Check SAP connection and attempt to re-establish if broken
        If oSAPConnection Is Nothing Then
            mSuccess = SAPLogin(abBatch)
            If mSuccess Then
                If oPostInvoice Is Nothing Then
                    Set oPostInvoice = oBAPICtrl.GetSAPObject("AcctngInvoiceReceipt")
                End If
                If oBAPIService Is Nothing Then
                    Set oBAPIService = oBAPICtrl.GetSAPObject("BapiService")
                End If
            Else
                Err.Raise 50010, "ReverseInvoices", "SAPLogin - unable to re-establish connection"
            End If
        End If
        
        'Reinitialise structures
        Set oReverseInvoice = Nothing
        Set oInvoiceReturn = Nothing
        Set oCommitReturn = Nothing
        Set oReverseInvoice = oBAPICtrl.DimAs(oPostInvoice, "Reverse", "Reversal")
        Set oInvoiceReturn = oBAPICtrl.DimAs(oPostInvoice, "Reverse", "Return")
        Set oCommitReturn = oBAPICtrl.DimAs(oBAPIService, "TransactionCommit", "Return")
            
        'Set up the reversal structure
        oReverseInvoice.Value("OBJ_TYPE") = mstrSAPInvoiceObjType
        oReverseInvoice.Value("OBJ_KEY") = aoRS!DocumentRef
        oReverseInvoice.Value("OBJ_SYS") = mstrSAPLogicalSystem
        oReverseInvoice.Value("OBJ_KEY_R") = aoRS!DocumentRef
        oReverseInvoice.Value("PSTNG_DATE") = CDate(aoRS!PostingDate)
        oReverseInvoice.Value("FIS_PERIOD") = aoRS!FisPeriod
        oReverseInvoice.Value("COMP_CODE") = aoRS!CompanyCode
        oReverseInvoice.Value("REASON_REV") = mstrSAPReversalReason
        oReverseInvoice.Value("AC_DOC_NO") = aoRS!SAPDocumentNo

        'Reverse the invoice
        oPostInvoice.Reverse Reversal:=oReverseInvoice, _
                             ObjType:=vObjType, _
                             ObjKey:=vObjKey, _
                             Return:=oInvoiceReturn, _
                             ObjSys:=vObjSys
                           
        'Handle errors at the remote call
        bCheckReverse = True
        If Not (oInvoiceReturn Is Nothing) Then
            gstrBAPIMessage = ""
            For i = 1 To oInvoiceReturn.rowCount
                If oInvoiceReturn.Value(i, "TYPE") <> "" And oInvoiceReturn.Value(i, "TYPE") <> "S" Then
                    bCheckReverse = False
                End If
                gstrBAPIMessage = gstrBAPIMessage + vbCRLF + "* " + oInvoiceReturn.Value(i, "TYPE") + oInvoiceReturn.Value(i, "NUMBER") + ": " + oInvoiceReturn.Value(i, "MESSAGE") + "(" + oInvoiceReturn.Value(i, "MESSAGE_V1") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V2") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V3") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V4") + ")"
                strErr = "* " + oInvoiceReturn.Value(i, "TYPE") + oInvoiceReturn.Value(i, "NUMBER") + ": " + oInvoiceReturn.Value(i, "MESSAGE") + "(" + oInvoiceReturn.Value(i, "MESSAGE_V1") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V2") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V3") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V4") + ")"
            Next
        End If
        
        If gstrBAPIMessage <> "" Then
            gstrBAPIMessage = vbCRLF & "Reversing " & aoRS!DocumentText & gstrBAPIMessage
            If Not (asLogFile = "") Then Print #aLog, gstrBAPIMessage
            If Not abBatch And Not bCheckReverse Then MsgBox gstrBAPIMessage, vbInformation, "Check Invoice"
        End If
        
        If bCheckReverse Then
          ' Commit the reverse so that we can get the document number
          oBAPIService.TransactionCommit Wait:="X", _
                                         Return:=oCommitReturn
        
          ' Key the vendor statement object to the current vendor
          Set oVendorStatement = Nothing
          Set oVendorStatement = oBAPICtrl.GetSAPObject("APAccount", aoRS!CompanyCode, aoRS!VendorNumber)
          Set oLineItems = oBAPICtrl.DimAs(oVendorStatement, "GetBalancedItems", "LineItems")
          Set oStmtReturn = oBAPICtrl.DimAs(oVendorStatement, "GetBalancedItems", "Return")
        
          ' Retrieve a statement of balanced items for the vendor
          oVendorStatement.GetBalancedItems DateFrom:=oReverseInvoice.Value("PSTNG_DATE"), _
                                            DateTo:=oReverseInvoice.Value("PSTNG_DATE"), _
                                            LineItems:=oLineItems, _
                                            Return:=oStmtReturn
                                        
          ' Loop through the items to match the one just posted
          For i = oLineItems.rowCount To 1 Step -1
              If UCase(oLineItems.Value(i, "ALLOC_NMBR")) = UCase(oReverseInvoice.Value("OBJ_KEY_R")) _
              And oLineItems.Value(i, "DOC_NO") = aoRS!SAPDocumentNo Then
                  mLastSAPDoc = oLineItems.Value(i, "REVERSAL_DOC")
                  If Not (asLogFile = "") Then Print #aLog, "Document reversed on " & mLastSAPDoc
                  Exit For
              End If
          Next
              
          ' Store the result for return
          lSuccess = lSuccess + 1
          auDoc(lCount).ID = aoRS!InvoiceID
          auDoc(lCount).Success = True
          auDoc(lCount).SAPDocNo = aoRS!SAPDocumentNo
          auDoc(lCount).SAPRevNo = mLastSAPDoc
          auDoc(lCount).FailReason = ""
          
        Else
          ' Store the result for return
          lFailure = lFailure + 1
          auDoc(lCount).ID = aoRS!InvoiceID
          auDoc(lCount).Success = False
          auDoc(lCount).SAPDocNo = aoRS!SAPDocumentNo
          auDoc(lCount).FailReason = strErr
            
        End If
        mvarReturn = SysCmd(acSysCmdUpdateMeter, lCount)
        lCount = lCount + 1
        aoRS.MoveNext
        
    Loop
    
    If Not (asLogFile = "") Then Print #aLog, vbCRLF & "Reverse Invoices: Processed " & Format(lCount, "#,##0") & ", Reversed: " & Format(lSuccess, "#,##0") & ", Failed: " & Format(lFailure, "#,##0") & vbCRLF
    
    ReverseInvoices = True

End Function

Public Function PostNewInvoices(aoRS As ADODB.Recordset, auDoc() As ReturnDoc, abBatch As Boolean, Optional aLog As Integer, Optional asLogFile As String = "")
' -------------------------------------------------------------------
' MODULE:      PostNewInvoices
' AUTHOR:      Nick Fry
' NARRATIVE:   Excute SAP BAPI function to check and post new invoices
'              from a recordset and return the document numbers
' HISTORY:     11-Feb-2005 NF Created
'
' OWNERS:      B&Q PLC
' -------------------------------------------------------------------
'
' Parameters reqd : aoRS - Recordset of new invoices or credit notes to be posted
'                   auDoc - structure in which to return posted invoice numbers
'                   abBatch - attended or unattended logon
'                   aLog+asLogFile - name and number of logfile if required

    PostNewInvoices = False
    
    Dim sLastRef As String
    Dim sLastSAPDoc As String
    Dim lRead As Long
    Dim lCount As Long
    Dim lSuccess As Long
    Dim lFailure As Long
    Dim lItem As Long
    Dim lAPRow As Long
    Dim lGLRow As Long
    Dim lTaxRow As Long
    Dim bPost As Boolean
    
    bPost = False
    lCount = 0
    lItem = 0
    lAPRow = 0
    lGLRow = 0
    lTaxRow = 0
    lRead = 1
    
    Do Until aoRS.EOF
        ReDim Preserve auDoc(lCount)
        ' Check SAP connection and attempt to re-establish if broken
        If oSAPConnection Is Nothing Then
            mSuccess = SAPLogin(abBatch)
            If mSuccess Then
               Call InitInvoiceStructures
               sLastRef = ""
               bPost = False
            Else
                Err.Raise 50010, "PostNewInvoices", "SAPLogin - unable to re-establish connection"
            End If
        End If
            
        If aoRS!InvoiceID <> sLastRef Then
            If bPost Then
                mSuccess = PostInvoice(oDocumentHeader.Value("COMP_CODE"), oAccountPayable.Cell(1, "VENDOR_NO"), abBatch, aLog, asLogFile)
                auDoc(lCount).ID = sLastRef
                If mSuccess Then
                    auDoc(lCount).Success = True
                    lSuccess = lSuccess + 1
                    If sLastSAPDoc = "" Then
                        auDoc(lCount).SAPDocNo = mLastSAPDoc
                        auDoc(lCount).SAPRevNo = ""
                    Else
                        auDoc(lCount).SAPDocNo = sLastSAPDoc
                        auDoc(lCount).SAPRevNo = mLastSAPDoc
                    End If
                    auDoc(lCount).FailReason = ""
                Else
                    lFailure = lFailure + 1
                    auDoc(lCount).Success = False
                    auDoc(lCount).SAPDocNo = sLastSAPDoc
                    auDoc(lCount).FailReason = gstrBAPIMessage
                End If
                bPost = False
                lCount = lCount + 1
                lItem = 0
                lAPRow = 0
                lGLRow = 0
                lTaxRow = 0
            End If

            'Reset structures
            Call InitInvoiceStructures
            
            'Store this invoice reference
            sLastRef = aoRS!InvoiceID
            sLastSAPDoc = aoRS!SAPDocumentNo
        End If

        If Not bPost Then
        ' Store the header and Vendor details
            ' Header Details
            oDocumentHeader.Value("OBJ_TYPE") = mstrSAPInvoiceObjType
            oDocumentHeader.Value("OBJ_KEY") = aoRS!DocumentRef
            oDocumentHeader.Value("OBJ_SYS") = mstrSAPLogicalSystem
            oDocumentHeader.Value("USERNAME") = gfsNetworkUserID
            oDocumentHeader.Value("HEADER_TXT") = aoRS!HeaderText
            oDocumentHeader.Value("COMP_CODE") = aoRS!CompanyCode
            oDocumentHeader.Value("FISC_YEAR") = aoRS!FisYear
            oDocumentHeader.Value("DOC_DATE") = CDate(aoRS!DocumentDate)
            oDocumentHeader.Value("PSTNG_DATE") = CDate(aoRS!PostingDate)
            oDocumentHeader.Value("FIS_PERIOD") = aoRS!FisPeriod
            If aoRS!SAPDocumentNo = "" Then
                oDocumentHeader.Value("DOC_TYPE") = mstrSAPInvoiceDocType
            Else
                oDocumentHeader.Value("DOC_TYPE") = mstrSAPCreditDocType
            End If
            oDocumentHeader.Value("REF_DOC_NO") = aoRS!DocumentRef
            
            'Vendor details
            lItem = lItem + 1
            lAPRow = lAPRow + 1
            oAccountPayable.Rows.Add
            oAccountPayable.Cell(lAPRow, "ITEMNO_ACC") = lItem
            oAccountPayable.Cell(lAPRow, "VENDOR_NO") = aoRS!VendorNumber
            oAccountPayable.Cell(lAPRow, "ITEM_TEXT") = aoRS!DocumentText

            'Currency row for vendor line
            oCurrencyAmount.Rows.Add
            oCurrencyAmount.Cell(lItem, "ITEMNO_ACC") = lItem
            oCurrencyAmount.Cell(lItem, "CURRENCY") = mstrSAPInvoiceCurrency
            oCurrencyAmount.Cell(lItem, "AMT_DOCCUR") = aoRS!VendorAmount
            oCurrencyAmount.Cell(lItem, "AMT_BASE") = aoRS!GLAmount
            
            bPost = True
        
        End If
            
        ' GL Details
        lItem = lItem + 1
        lGLRow = lGLRow + 1
        oAccountGL.Rows.Add
        oAccountGL.Cell(lGLRow, "ITEMNO_ACC") = lItem
        oAccountGL.Cell(lGLRow, "GL_ACCOUNT") = aoRS!GLAccount
        oAccountGL.Cell(lGLRow, "TAX_CODE") = aoRS!TaxCode
        oAccountGL.Cell(lGLRow, "FISC_YEAR") = aoRS!FisYear
        oAccountGL.Cell(lGLRow, "FIS_PERIOD") = aoRS!FisPeriod
        oAccountGL.Cell(lGLRow, "ITEM_TEXT") = aoRS!DocumentText
        oAccountGL.Cell(lGLRow, "COSTCENTER") = aoRS!CostCentre
    
        'Currency row for GL line
        oCurrencyAmount.Rows.Add
        oCurrencyAmount.Cell(lItem, "ITEMNO_ACC") = lItem
        oCurrencyAmount.Cell(lItem, "CURRENCY") = mstrSAPInvoiceCurrency
        oCurrencyAmount.Cell(lItem, "AMT_DOCCUR") = aoRS!GLAmount
        oCurrencyAmount.Cell(lItem, "AMT_BASE") = aoRS!GLAmount
        
        ' Tax Details
        lItem = lItem + 1
        lTaxRow = lTaxRow + 1
        oAccountTax.Rows.Add
        oAccountTax.Cell(lTaxRow, "ITEMNO_ACC") = lItem
        oAccountTax.Cell(lTaxRow, "TAX_CODE") = aoRS!TaxCode
        oAccountTax.Cell(lTaxRow, "GL_ACCOUNT") = aoRS!TaxAccount
        oAccountTax.Cell(lTaxRow, "ACCT_KEY") = mstrSAPAccountKey
    
        'Currency row for GL line
        oCurrencyAmount.Rows.Add
        oCurrencyAmount.Cell(lItem, "ITEMNO_ACC") = lItem
        oCurrencyAmount.Cell(lItem, "CURRENCY") = mstrSAPInvoiceCurrency
        oCurrencyAmount.Cell(lItem, "AMT_DOCCUR") = aoRS!TaxAmount
        oCurrencyAmount.Cell(lItem, "AMT_BASE") = aoRS!GLAmount
            
        mvarReturn = SysCmd(acSysCmdUpdateMeter, lRead)
        
        aoRS.MoveNext
        If Not aoRS.EOF Then lRead = lRead + 1

    Loop
            
    ' Is there one last item to output?
    If bPost Then
        mSuccess = PostInvoice(oDocumentHeader.Value("COMP_CODE"), oAccountPayable.Cell(1, "VENDOR_NO"), abBatch, aLog, asLogFile)
        ReDim Preserve auDoc(lCount)
        auDoc(lCount).ID = sLastRef
        If mSuccess Then
            lSuccess = lSuccess + 1
            auDoc(lCount).Success = True
            If sLastSAPDoc = "" Then
                auDoc(lCount).SAPDocNo = mLastSAPDoc
                auDoc(lCount).SAPRevNo = ""
            Else
                auDoc(lCount).SAPDocNo = sLastSAPDoc
                auDoc(lCount).SAPRevNo = mLastSAPDoc
            End If
            auDoc(lCount).FailReason = ""
        Else
            lFailure = lFailure + 1
            auDoc(lCount).Success = False
            auDoc(lCount).SAPDocNo = sLastSAPDoc
            auDoc(lCount).FailReason = gstrBAPIMessage
        End If
    End If
    
    If Not (asLogFile = "") Then Print #aLog, vbCRLF & "Post Invoices/CreditNotes: Processed " & Format(lRead, "#,##0") & ", Posted: " & Format(lSuccess, "#,##0") & ", Failed: " & Format(lFailure, "#,##0") & vbCRLF
    
    PostNewInvoices = True
    
End Function

Private Sub InitInvoiceStructures()
' -------------------------------------------------------------------
' MODULE:      InitInvoiceStructures
' AUTHOR:      Nick Fry
' NARRATIVE:   Initializes the BAPI structures used to Post Invoices
'              from a recordset and return the document numbers
' HISTORY:     11-Feb-2005 NF Created
'
' OWNERS:      B&Q PLC
' -------------------------------------------------------------------
'
' Parameters reqd : abAttended - Is this an Attended or Unattended login

    'Create local instance of BAPI
    If oPostInvoice Is Nothing Then
        Set oPostInvoice = oBAPICtrl.GetSAPObject("AcctngInvoiceReceipt")
    End If
    
    'Set up general services
    If oBAPIService Is Nothing Then
        Set oBAPIService = oBAPICtrl.GetSAPObject("BapiService")
    End If

'Destroy current structures
     Set oDocumentHeader = Nothing
     Set oAccountPayable = Nothing
     Set oAccountGL = Nothing
     Set oAccountTax = Nothing
     Set oCurrencyAmount = Nothing
     Set oInvoiceReturn = Nothing
     Set oCommitReturn = Nothing

'Create a new table or structure
     Set oDocumentHeader = oBAPICtrl.DimAs(oPostInvoice, "Post", "DocumentHeader")
     Set oAccountPayable = oBAPICtrl.DimAs(oPostInvoice, "Post", "AccountPayable")
     Set oAccountGL = oBAPICtrl.DimAs(oPostInvoice, "Post", "AccountGL")
     Set oAccountTax = oBAPICtrl.DimAs(oPostInvoice, "Post", "AccountTax")
     Set oCurrencyAmount = oBAPICtrl.DimAs(oPostInvoice, "Post", "CurrencyAmount")
     Set oInvoiceReturn = oBAPICtrl.DimAs(oPostInvoice, "Post", "Return")
     Set oCommitReturn = oBAPICtrl.DimAs(oBAPIService, "TransactionCommit", "Return")

End Sub

Private Function PostInvoice(asCompany As String, asVendor As String, abBatch As Boolean, Optional aLog As Integer, Optional asLogFile As String = "")
' -------------------------------------------------------------------
' MODULE:      PostInvoice
' AUTHOR:      Nick Fry
' NARRATIVE:   Calls the BAPI functions to Check, Post and Commit the
'              Invoice and then read back open items for the vendor to
'              get the SAP document number.
' HISTORY:     11-Feb-2005 NF Created
'
' OWNERS:      B&Q PLC
' -------------------------------------------------------------------
'
' Parameters reqd : asCompany + asVendor - who this is posting to so we can retrieve line items

Dim bCheckValid As Boolean
Dim i As Long

    PostInvoice = False
    
    'Perform a validation check on the details set up first
    oPostInvoice.Check DocumentHeader:=oDocumentHeader, _
                       AccountPayable:=oAccountPayable, _
                       AccountGL:=oAccountGL, _
                       AccountTax:=oAccountTax, _
                       CurrencyAmount:=oCurrencyAmount, _
                       Return:=oInvoiceReturn

    'Handle errors at the remote call
    bCheckValid = True
    If Not (oInvoiceReturn Is Nothing) Then
        gstrBAPIMessage = ""
        For i = 1 To oInvoiceReturn.rowCount
            If oInvoiceReturn.Value(i, "TYPE") <> "" And oInvoiceReturn.Value(i, "TYPE") <> "S" Then
                bCheckValid = False
            End If
            gstrBAPIMessage = gstrBAPIMessage + vbCRLF + "* " + oInvoiceReturn.Value(i, "TYPE") + oInvoiceReturn.Value(i, "NUMBER") + ": " + oInvoiceReturn.Value(i, "MESSAGE") + "(" + oInvoiceReturn.Value(i, "MESSAGE_V1") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V2") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V3") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V4") + ")"
        Next
    End If
    
    If gstrBAPIMessage <> "" Then
        'gstrBAPIMessage = "Posting " & oDocumentHeader.Value("REF_DOC_NO") & "-" & oDocumentHeader.Value("DOC_DATE") & vbCRLF & gstrBAPIMessage
        If Not (asLogFile = "") Then Print #aLog, gstrBAPIMessage
        If Not abBatch And Not bCheckValid Then MsgBox gstrBAPIMessage, vbInformation, "Check Invoice"
    End If
        
    If bCheckValid Then
        ' Post the invoice
        oPostInvoice.Post DocumentHeader:=oDocumentHeader, _
                          AccountPayable:=oAccountPayable, _
                          AccountGL:=oAccountGL, _
                          AccountTax:=oAccountTax, _
                          CurrencyAmount:=oCurrencyAmount, _
                          Return:=oInvoiceReturn
        
        ' Commit the post so that we can get the document number
        oBAPIService.TransactionCommit Wait:="X", _
                                       Return:=oCommitReturn
      
        ' Key the vendor statement object to the current vendor
        Set oVendorStatement = Nothing
        Set oVendorStatement = oBAPICtrl.GetSAPObject("APAccount", asCompany, asVendor)
        Set oLineItems = oBAPICtrl.DimAs(oVendorStatement, "GetOpenItems", "LineItems")
        Set oStmtReturn = oBAPICtrl.DimAs(oVendorStatement, "GetOpenItems", "Return")
      
        ' Retrieve a statement of open items for the vendor
        oVendorStatement.GetOpenItems KeyDate:=oDocumentHeader.Value("PSTNG_DATE"), _
                                      LineItems:=oLineItems, _
                                      Return:=oStmtReturn
                                      
        ' Loop through the items to match the one just posted
        For i = oLineItems.rowCount To 1 Step -1
            If UCase(oLineItems.Value(i, "ALLOC_NMBR")) = UCase(oDocumentHeader.Value("REF_DOC_NO")) Then
                mLastSAPDoc = oLineItems.Value(i, "DOC_NO")
                If Not (asLogFile = "") Then Print #aLog, "Document posted to " & mLastSAPDoc
                Exit For
            End If
        Next
    End If

    PostInvoice = True

End Function




RE: Integrating SAP and vb

All,

I suspect Paul is getting fed up of Emailing the examples, so if anyone of you who have received it wouldnt mind sending me a copy, it would be appreciated. My add is ianrasor@liberata.com.

Cheers

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close