Search code examples
excelvbasap-guialv

ALV grid only loads first 64 rows, how to change default load


Situation

I have created a lookup functionality for SAP GUI scripting.
If a grid row has specific values in specific columns, then it is double clicked (this triggers loading specific dependent data).
My grid has less than 300 rows, so loading so much data shouldn't strain a modern computer.

Issue

The issue I have is that from SAPGrid Row 64 it returns "" for each cell. If I enter debugging and scroll down in the ALV grid then the grid row loads and the results are found.

Possible solutions

Can I change how many rows are loaded on default?
Is there a method for pulling the full recordset?
The alternative options include scrolling up and down using scripting or setting up filters.

Code

Sub FindGridLine(SAPGrid As Object, criteria() As String)
SAPGrid.ClearSelection 'first it deselects what has been selected

For k = 0 To (SAPGrid.RowCount - 1) 'for each grid row
    For i = 1 To UBound(criteria, 1) ' for each criteria row except for the first (should be only 1)
        For j = LBound(criteria, 2) To UBound(criteria, 2) 'and for each column
            tempstr = SAPGrid.GetCellValue(k, criteria(0, j))
            If tempstr <> criteria(i, j) Then 'if the criterion doesn't match
                GoTo nextrow 'then go to the next row
            End If
        Next j
    Next i
    'if it passed the criteria then doubleclick it
    SAPGrid.DoubleClick k, criteria(0, 0)
    Exit Sub
nextrow:
Next k
'in case no results were found
MsgBox "No line was found in grid!"
End Sub

Updated code

Code updated based on correct answer from @Asger.
Since lookups mostly work with primary keys, I went for the safe solution of SAPGrid.GetCellValue(k, criteria(0, j)) = "" but the solution is in fact SAPGrid.SetCurrentCell k, criteria(0, j).

Sub FindGridLine(SAPGrid As Object, criteria() As String)
'    SAPGrid.SelectAll 'first it selects everything as to load the full grid
    SAPGrid.ClearSelection 'first it deselects what has been selected
    
    For k = 0 To (SAPGrid.RowCount - 1) 'for each grid row
        For i = 1 To UBound(criteria, 1) ' for each criteria row except for the first (should be only 1)
            For j = LBound(criteria, 2) To UBound(criteria, 2) 'and for each column
                tempstr = SAPGrid.GetCellValue(k, criteria(0, j))
                If tempstr = "" Then SAPGrid.SetCurrentCell k, criteria(0, j) 'this solution only works if the search is done in a non-empty field
                tempstr = SAPGrid.GetCellValue(k, criteria(0, j))
                If tempstr <> criteria(i, j) Then 'if the criterion doesn't match
                    GoTo nextrow 'then go to the next row
                End If
            Next j
        Next i
        'if it passed the criteria then doubleclick it
        SAPGrid.DoubleClick k, criteria(0, 0)
        Exit Sub
nextrow:
    Next k
'in case no results were found
For i = 0 To UBound(criteria, 1) ' for each criteria row except for the first (should be only 1)
    For j = LBound(criteria, 2) To UBound(criteria, 2) 'and for each column
        tempstr = tempstr & "|" & criteria(i, j)
    Next j
    If i <> UBound(criteria, 1) Then
        tempstr = tempstr & vbNewLine
    End If
Next i
MsgBox "No line was found in grid!" & vbNewLine & "Please select line" & tempstr & vbNewLine & "manually and press 'OK'" & vbNewLine & "or enter debug mode."
End Sub

Solution

  • GuiGridView / ALV Grid Control: For large amounts of data, reloading of the content takes place only after scrolling, otherwise it is likely that only one empty string will be returned as the result - even without causing an exception.

    Therefore SetCurrentCell should always be used to focus and load the dataset to be read.

    Please test e. g. SAPGrid.SetCurrentCell(k, 1)

    Maybe it's sufficient to load every new 64 rows (I can't test it):

    If k Mod 64 = 63 Then ' at least if 1 row before each 64 rows
        SAPGrid.SetCurrentCell (k, criteria(0, LBound(criteria, 2)))
    End If