Question

I have the following syntax to use C# to delete an access query...but how do I 1st check if the query exists?

DAO.Database dd;
DAO.DBEngine db = new DAO.DBEngine();
dd = db.OpenDatabase(path);
dd.QueryDefs.Delete(queryName);

I also tried this -- but received a compile error of 'Can not assign to 'Equals' because it is a 'method group'

var qd = new DAO.QueryDef();
if (qd.Name.Equals = "Hello")
{
  dd.QueryDefs.Delete(queryName);
}
Was it helpful?

Solution 2

You can loop over the QueryDefs collection and check if you find it...

DAO.Database dd;
DAO.DBEngine db = new DAO.DBEngine();
dd = db.OpenDatabase(path);
bool found= false;
foreach(DAO.QueryDef qd in dd.QueryDefs)
{
   if (qd.Name == "Hello")
   {
        found = true;
   }
}
if (found)
{
   dd.QueryDefs.Delete(queryName);
}

OTHER TIPS

Try this SQL query:

SELECT * FROM MSysObjects
WHERE Name="MyQuery" AND Type=5

If you get results, the query exists. Type=5 is for queries (at least in the database I looked at).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top