How to deal with SAP tables – check boxes (part 2)

Last time I showed You how to deal with cell values of SAP tables. In today’s post we are going to deal with check boxes in SAP tables.

How to deal with SAP tables - part 2 joke

In this example we are going to loop through the table to find certain names and check their check boxes. This is the continuation of the part 1.

Table II

The code below checks the check box next to MANTR. This is the first box from check boxes, in the second row of the table (first row in this column is empty).

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/" & _
    "chkGS_SELFIELDS-MARK[5,1]").Selected = True

The goal

The goal is to write the code, which checks 4 chosen boxes.

Step-by-step solution

First of all we can see by looking at the recorded code, that ID of this table in brackets is somehow different, than in the Table I example. The ID is including the check box I selected during the recording.

Knowing that check box is the children of the table we can easily find table ID. Easy speaking, the name is before the last slash (/).

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC")

If You are not that sure to deal like that, just use the first line and add .Parent.

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/" & _
    "chkGS_SELFIELDS-MARK[5,1]").Parent

This can be safer way to get the table ID. To have much cleaner code it is good to set an object at the beginning.

Dim SAP_table As Object

Set SAP_table = session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/" & _
    "chkGS_SELFIELDS-MARK[5,1]").Parent

Watches

To get to know all of the information about table properties just right click on our first code line and select Watches.

Here You can see, that parent of the check box is the table having 7 counted Columns and each column Item have 27 row Items! The first row item of the first column item (Title – “Fld name”) has DisplayedText “Client”. All mentioned here is completely consistent with the table screen shot, so we are all good!

The technical names of the boxes we want to check will be stored in array.

string_arr = Array("Material", "Plant", "Storage Loc.", "Unrestricted")

Looping

Before You start the looping, get the boundaries, I mean the number of columns and rows of the table.

ST_rows = SAP_table.Rows.Count
ST_cols = SAP_table.Columns.Count

Don’t forget, that enumeration of rows and columns starts with 0!

For j = 0 To ST_cols - 1

You can use the number of table rows for the second loop or You can check the row number of every column item.

ST_col_ctn = SAP_table.Columns.Item(j).Count
For i = 0 To ST_col_ctn - 1

So You want to check the name in every row.

ST_col_item_text = SAP_table.Columns.Item(j).Item(i + 0).DisplayedText

Tip: if You have problem with the error about wrong index use i + 0 or Clng(i).

To check if the box name is in the string array I used IsInArray function. Short and simple.

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

In nice and easy way it helps to select as True proper check box in the 6th column (.Item(5)).

If IsInArray(Trim(ST_col_item_text), string_arr) Then
    On Error Resume Next
    SAP_table.Columns.Item(5).Item(i - 1).Selected = True
    On Error GoTo 0
End If

And the last thing – why Item(i – 1) in the check box selecting line?
In this example there is no check box in the first row (look at first screen shot). That’s why You need to subtract 1 from loop iteration number.

The code

And now it’s time for the whole code together!

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/" & _
    "chkGS_SELFIELDS-MARK[5,1]").Selected = True

Dim ST_rows As Long, ST_cols As Long, i As Long, j As Long
Dim ST_col_ctn As Long
Dim ST_col_name As String, ST_col_item_text As String
Dim string_arr As Variant
Dim SAP_table As Object

Set SAP_table = session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/" & _
    "chkGS_SELFIELDS-MARK[5,1]").Parent

string_arr = Array("Material", "Plant", "Storage Loc.", _
    "Unrestricted")

ST_rows = SAP_table.Rows.Count
ST_cols = SAP_table.Columns.Count
For j = 0 To ST_cols - 1
    ST_col_ctn = SAP_table.Columns.Item(j).Count
    For i = 0 To ST_col_ctn - 1
        On Error Resume Next
        ST_col_item_text = SAP_table.Columns.Item(j).Item(i + 0).DisplayedText
        On Error GoTo 0
        If IsInArray(Trim(ST_col_item_text), string_arr) Then
            On Error Resume Next
            SAP_table.Columns.Item(5).Item(i - 1).Selected = True
            On Error GoTo 0
        End If
    Next
Next

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

Summary

I hope You enjoyed that 2 part article about how to deal with SAP tables. Now You can easily loop through the table, extract its data or just simple choose and select the check boxes in SAP tables!

Author: Tomasz Płociński

I'm very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I'm trying now to widen my knowledge into TypeScript/JavaScript direction.

Leave a Reply

Your email address will not be published. Required fields are marked *