#Description: Example code demonstrating creating a SELECT query builder and accessing via a DataTable.
#Author: Intersoft Systems Limited
#Version: 1.01
#Last Updated: 6/07/2020

Dim dt As DataTable
Dim dtRow As DataRow
Dim sqb As ISSelectQueryBuilder

' Create Select Query Builder
sqb = finBL.Database.CreateSelectQueryBuilder()

With sqb
  ' Clear
  .Clear()

  ' Set Table and other Options
  .Table = "Account"
  '.DistinctType = iseSelectQueryDistinctType.Distinct
  .TopLimit = 10

  ' Select Fields
  With .Fields
    .Add("Account.Pk", "AccountPk")
    .AddList("Account.AccountId,Account.Name")
    .AddList("Client.ClientId")
  End With

  ' Joins
  With .Joins
    .Add("Client", "Client.Pk", "Account.ClientPk", iseSelectQueryJoinType.Inner)
  End With

  ' Where
  With .SqlWhere
    ' Only Open Accounts
    .AppendComparisonInteger("Account.Status", "=", CInt(isefinAccountStatus.Open))

    ' User Filters - ALWAYS INCLUDE
    .Append(finBL.CurrentUserInformation.FilterAccountSqlWhere)
  End With

  ' Order By
  With .OrderByFields
    .Add("Account.AccountId")
  End With

  ' Open DataTable
  Main = .ExecuteDataTable(dt)
End With
	
If Main then
  ' Loop through each Row
  For Each dtRow In dt.Rows
    finBL.DebugPrintFormat("Account {0}, '{1}' belongs to client '{2}'", finBL.Database.GetFieldString(dtRow!AccountId), finBL.Database.GetFieldString(dtRow!Name), finBL.Database.GetFieldString(dtRow!ClientId))
  Next

  ' Close the DataTable
  finBL.Database.DataTableClose(dt)
End If