Forum Discussion

jackson_1's avatar
jackson_1
Frequent Contributor
13 years ago

how to verify a table if exist in access database in delphi script

hi all,



in my testing project. i need a function to verify the special table if created in access database, and i use the following script to verify it, but the result always is false. so i want to know there is any issue in my script?



-----------------------------------------------

function ValidateSpecialTable(aStrDBPath,aStrTableName): boolean;


var

    ADOReSchema,ADOCnn;


begin


    ADOCnn: = Sys.OleObject('ADODB.Connection');

    ADOCnn.ConnectionString: = 'Provider=Microsoft.ACE.OLEDB.12.0;' +'Data Source=' + aStrDBPath;

    ADOCnn.Open();

    ADOReSchema: = ADOCnn.OpenSchema(20);

    ADOReSchema.MoveFirst;

   

    repeat

        begin

            if  ((ADOReSchema[('TABLE_TYPE')]) = 'TABLE' ) and ((ADOReSchema[('TABLE_NAME')]) = aStrTableName) then

            begin

                    result: = true;

                    exit;

             end

             else

                    result: =false;

        end;

        ADOReSchema.MoveNext;

    until ADOReSchema.Eof

   

end;

1 Reply


  • Hi,





    Try using the following script based on the example:







    function ValidateSpecialTable(aStrDBPath,aStrTableName): boolean;

    var ConnStr,adoxConn,adodbConn,found,i;

    begin

      ConnStr := 'Provider=Microsoft.ACE.OLEDB.12.0;' +'Data Source=' + aStrDBPath; 

      adoxConn := Sys.OleObject('ADOX.Catalog');  

      adodbConn := Sys.OleObject('ADODB.Connection');  

      adodbConn.Open(ConnStr);  

      adoxConn.activeConnection := adodbConn;  

      found := false; 

      for i:=0 to adoxConn.tables.Count - 1 do 

        if aqString.ToLower(adoxConn.tables.item(i).name) = aqString.ToLower(aStrTableName) then begin 

          found := true; 

          break;

        end; 

      adodbConn.close;

      adodbConn := nil;  

      adoxConn := nil;  

      result := found;

    end;