The addviaSQLStment contains 2 parameters:
  1. The actual SQL Statement.
  2. The IDFieldIndex.  (Number sequence starts at zero)
The following sample code can be modified as necessary:

Public Sub CreateStaticQueryviaSQL()
   Dim oStaticQuery As CStaticQ

   Set oStaticQuery = New CStaticQ
   oStaticQuery.Init REApplication.SessionContext

   With oStaticQuery
       .CreateWithoutPrompt (SEARCH_CONSTITUENT), "Custom", "Test", _
           , , , , , True 'The Last true overwrites queries with the same name
       .AddviaSQLStmt "SELECT RECORDS.ID FROM dbo.RECORDS WHERE RECORDS.LAST_NAME = 'Smith'", 0
       .EndCreate Nothing
   End With
  
   oStaticQuery.CloseDown
   Set oStaticQuery = Nothing

End Sub

In the above example there is one select field i.e. the id field so the "IDFieldIndex" that corresponds to it is 0. If the id field were the third field in the SQL statement, the IDFieldIndex would be 2.
Disclaimer: We provide programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes you are familiar with Microsoft Visual Basic and the tools used to create and debug procedures. Blackbaud Customer Support may help explain the functionality of a particular procedure, but we will not modify, or assist you with modifying, these examples to provide additional functionality.