Get the buffers of a dynamic query

If you've got a dynamic query going on, quite often the buffers will be in all sorts of different orders, and in some cases some won't even be there. Setting the buffers can be pretty tricky. This code can probably be enhanced, but it's a good starting point for programtically getting the buffers. 

 

CREATE QUERY QueryHandle.
Tables = fn-GetTables(QueryString).
DO i = 1 TO NUM-ENTRIES(Tables):
  QueryHandle:ADD-BUFFER(ENTRY(i,Tables)).
END.
QueryHandle:QUERY-PREPARE(QueryString).
QueryHandle:QUERY-OPEN().

 

FUNCTION fn-GetTables RETURNS CHARACTER
  ( INPUT QueryString AS CHARACTER ):
  /*------------------------------------------------------------------------------
   Purpose:
   Notes:
  ------------------------------------------------------------------------------*/ 
  DEFINE VARIABLE Tables    AS CHARACTER NO-UNDO.
  DEFINE VARIABLE i         AS INTEGER   NO-UNDO.
  DEFINE VARIABLE Word      AS CHARACTER NO-UNDO.
  DEFINE VARIABLE ThisTable AS CHARACTER NO-UNDO.

  QueryString = REPLACE(QueryString," ",",").
  QueryString = REPLACE(QueryString,",,",",").
  DO i = 1 TO NUM-ENTRIES(QueryString):
    Word = ENTRY(i,QueryString).
    IF Word EQ "FIRST" OR Word EQ "EACH" THEN
    DO:
      ASSIGN
        ThisTable = ENTRY(i + 1,QueryString)
        ThisTable = ENTRY(NUM-ENTRIES(ThisTable,"."),ThisTable,".")
        Tables = Tables + min(Tables,",") + ThisTable.
    END.
  END.

  RETURN Tables.

   
END FUNCTION.

Copyright © 2018 James Palmer
GS RU