title: How to execute a query saved in MS Access from delphi 10.2 without ever getting error 3389
body: <p>My 32-bits application is based on an MS Access database. I developed my application in Delphi. I use dbExpress with the ODBC driver included in Delphi 10.2 Tokyo to connect to the database. Things were working all right until last Friday, when I finished work on the installer package (*.MSI). When I then installed it on a test machine, I suddenly started getting this error: "Query support unavailable". I understand that this error is associated with number 3389 and that the extended message is: "You attempted to establish referential integrity on a Relation object of a QueryDef object, which is not supported".</p>
  
  <p>I don't see how this message is relevant for my query though, because all my SQL statements start with "SELECT" meaning that I do not modify the query. In the relevant query, I've made a union of a Cartesian product of 2 tables with another table. The latter table is called CROP_INDICATOR_DATA and the records in it are already crop-specific. There's also a table INDICATOR_DATA with data which are not crop-specific and the union with the Cartesian product and the other table was introduced to make all the data "available" for each of the crops in one "object". So this query was saved in the database - would have been a view if MS Access had had such a feature. My query is as follows: </p>
  
  <pre><code>SELECT P.REGION_ID, P.YEAR, P.DEKAD, S.CROP_NO, P.INDICATOR_CODE, 
  P.INDICATOR_VALUE FROM INDICATOR_DATA P, (SELECT DISTINCT CROP_NO FROM 
  STAT_REGION) S UNION ALL SELECT * FROM CROP_INDICATOR_DATA;
  </code></pre>
  
  <p>So this was saved under the name ALL_INDICATOR_DATA. The strange thing is that the query yields results on one machine, but causes error 3389 on 2 other machines. All my machines are running Windows 7 Enterprise. I also used 32-bits Python with package pyodbc to invoke this query by means of this SELECT Count(*) FROM ALL_INDICATOR_DATA without problems, even when I do it multiple times. When however I invoke the query from my Delphi application the query does not work properly more than once - if at all. Now this even happens when I run the application in debug mode!</p>
  
  <p>I have isolated the problem in a test project, with a TSQLConnection object, a TSQLDataset and a button on a form:</p>
  
  <pre><code>object SQLConnection1: TSQLConnection
    ConnectionName = 'OdbcConnection'
    DriverName = 'Odbc'
    Params.Strings = (
      'DriverName=Odbc'
      'DataBase=Database Name'
      'User_Name=user'
      'Password=password'
      'IsolationLevel=ReadCommitted'
      'RowSetSize=20'
      'ConnectionString=')
    Left = 32
    Top = 16
  end
  object SQLDataSet1: TSQLDataSet
    MaxBlobSize = -1
    Params = &lt;&gt;
    SQLConnection = SQLConnection1
    Left = 112
    Top = 16
  end
  </code></pre>
  
  <p>The database I use was registered in the 32-bits ODBC Data Source Administrator with name "My_Local_Database". Please find my code from the implementation section of my project below. I believe any Delphi programmer can guess what the interface section would look like.</p>
  
  <pre><code>procedure TForm1.FormCreate(Sender: TObject);
  const
    CONNECTION_STRING = 'ConnectionString';
    DATABASENAME_KEY = 'Database';
  var ConnStr, MyDsn, dn: String;
  begin
    MyDsn := 'My_Local_Database';
    with SQLConnection1 do
    begin
      Params.Values['Password'] := '';
      Params.Values['User_name'] := '';
      Params.Values[DATABASENAME_KEY] := MyDsn;
      Params.Values['IsolationLevel'] := 'ReadCommitted';
      Params.Values['RowSetSize'] := '20';
      dn := 'Microsoft Access Driver (*.mdb, *.accdb)';
      ConnStr := 'DSN=' + MyDsn + ';';
      ConnStr := ConnStr + 'DRIVER={' + dn + '};';
      ConnStr := ConnStr + 'DriverId=25;FIL=MS Access;';
      ConnStr := ConnStr + 'coEmptyStrParam=1;';
      ConnStr := ConnStr + 'coEnableBCD=0;';
      Params.Values[CONNECTION_STRING] := ConnStr;
      Open();
    end;
  end;
  
  procedure TForm1.Button1Click(Sender: TObject);
  var s: String;
    Result: Variant;
  begin
    try
      s := 'SELECT COUNT(*) FROM ALL_INDICATOR_DATA';
      SQLDataset1.CommandText := s;
      SQLDataset1.CommandType := ctQuery;
      SQLDataset1.Prepared := True;
  
      SQLDataset1.Open();
      if (not SQLDataset1.EOF) then SQLDataset1.First();
      Result := SQLDataset1.Fields.Fields[0].AsVariant;
      ShowMessage(Result);
    finally
      SQLDataset1.Close();
    end;
  end;
  
  procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  begin
    with SQLConnection1 do
    begin
      if Connected then Close();
    end;
  end;
  </code></pre>
  
  <p>What could have caused this strange phenomenon? Have my ODBC drivers been affected somehow?</p>