Skip to Content

Tip: How To Use F4 Search Help with VBA and CCo

Often it is helpful to select data in your application equal as in the SAP system. Therefore exists the RFC enabled function module RHF4_RFC_FIELD_VALUE_REQUEST. With this FM is it possible to call a search help or for a field a determine search help. Here a VBA code snippet for an Excel application. In the example below I call a determine search help for the field OBJECT from the table TADIR and, which is actual commented, a search help for the table USR10. To call the FM I use the COM Connector (CCo).

  '-Directives----------------------------------------------------------
    Option Explicit

  '-Constants-----------------------------------------------------------
    Const RFC_OK = 0

  '-Function GetF4Help--------------------------------------------------
    Function GetF4Help(SAP As CCo.COMNWRFC, hRFC As Long, _
      TableName As String, FieldName As String, SearchHelpName As String)

      '-Variables-------------------------------------------------------
        Dim rc As Integer
        Dim hFuncDesc As Long
        Dim hFunc As Long
        Dim hTable As Long
        Dim hRow As Long
        Dim RowCount As Long
        Dim i As Integer
        Dim charBuffer As String
        Dim F4Help As String
        Dim F4Rows
        Dim F4Cols
        Dim F4Values As String

      hFuncDesc = SAP.RFCGETFUNCTIONDESC(hRFC, _
        "RHF4_RFC_FIELD_VALUE_REQUEST")
      If hFuncDesc = 0 Then
        Exit Function
      End If

      hFunc = SAP.RFCCREATEFUNCTION(hFuncDesc)
      If hFunc = 0 Then
        Exit Function
      End If

      rc = SAP.RFCSETCHARS(hFunc, "TABNAME", TableName)
      rc = SAP.RFCSETCHARS(hFunc, "FIELDNAME", FieldName)
      rc = SAP.RFCSETCHARS(hFunc, "SEARCHHELP", SearchHelpName)

      If SAP.RFCINVOKE(hRFC, hFunc) <> RFC_OK Then
        rc = SAP.RFCDESTROYFUNCTION(hFunc)
        Exit Function
      End If

      If SAP.RFCGETTABLE(hFunc, "RETURN_TAB", hTable) = RFC_OK Then

        rc = SAP.RFCGETROWCOUNT(hTable, RowCount)
        rc = SAP.RFCMOVETOFIRSTROW(hTable)
        For i = 1 To RowCount
          hRow = SAP.RFCGETCURRENTROW(hTable)
          rc = SAP.RFCGETCHARS(hRow, "SHLPNAME", charBuffer, 30)
          F4Help = F4Help & Trim(charBuffer) & "~"
          rc = SAP.RFCGETCHARS(hRow, "FIELDNAME", charBuffer, 30)
          F4Help = F4Help & Trim(charBuffer) & "~"
          rc = SAP.RFCGETCHARS(hRow, "RECORDPOS", charBuffer, 4)
          F4Help = F4Help & Trim(charBuffer) & "~"
          rc = SAP.RFCGETCHARS(hRow, "FIELDVAL", charBuffer, 132)
          F4Help = F4Help & Trim(charBuffer) & "~"
          rc = SAP.RFCGETCHARS(hRow, "RETFIELD", charBuffer, 132)
          F4Help = F4Help & Trim(charBuffer) & vbCrLf
          If i < RowCount Then
            rc = SAP.RFCMOVETONEXTROW(hTable)
          End If
        Next

        F4Rows = Split(F4Help, vbCrLf)
        For i = 0 To UBound(F4Rows) - 1
          F4Cols = Split(F4Rows(i), "~")
          F4Values = F4Values & F4Cols(3)
          If i < UBound(F4Rows) - 1 Then
            F4Values = F4Values & "~"
          End If
        Next

      End If

      rc = SAP.RFCDESTROYFUNCTION(hFunc)
      GetF4Help = F4Values

    End Function

  '-Sub Button1_Click---------------------------------------------------
    Sub Button1_Click()

      '-Variables-------------------------------------------------------
        Dim SAP As CCo.COMNWRFC
        Dim hRFC As Long
        Dim rc As Integer
        Dim F4Help As String

      Set SAP = CreateObject("COMNWRFC")
      If Not IsObject(SAP) Then
        Exit Sub
      End If

      hRFC = SAP.RFCOPENCONNECTION("ASHOST=ABAP, SYSNR=00, " & _
        "CLIENT=001, USER=BCUSER, USE_SAPGUI=1")
      If hRFC = 0 Then
        Set SAP = Nothing
        Exit Sub
      End If

      '-Call search help------------------------------------------------
      '-
      '- You can find all search helps in table TADIR, with PGMID = R3TR
      '- and OBJECT = SHLP
      '-
      '-----------------------------------------------------------------
      'F4Help = GetF4Help(SAP, hRFC, "USR10", "", "PROFILES_ALL_ACTIVE")


      '-Call determine search help for a field--------------------------
      F4Help = GetF4Help(SAP, hRFC, "TADIR", "OBJECT", "")

      Tabelle1.Cells(10, 2).Value = F4Help

      rc = SAP.RFCCLOSECONNECTION(hRFC)
      Set SAP = Nothing

    End Sub

 

001.JPG

 

/wp-content/uploads/2016/08/002_1017775.jpg

 

Hint: To use this FM it is necessary that a SAP GUI for Windows is installed.

Enjoy it.

Cheers
Stefan

Be the first to leave a comment
You must be Logged on to comment or reply to a post.