user2078729
user2078729

Reputation: 21

How can i delete ms access query object using DoCmd.DeleteObject

I am trying to export a table by using a query and then i want to delete that query object from ms access file, but it gives me error - Cannot Update Database.File may be readonly.

My code below

MsAcs.OpenCurrentDatabase(newdb,false,"");
 MsAcs.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
                        MsAcs.DoCmd.TransferText(Microsoft.Office.Interop.Access.AcTextTransferType.acExportDelim, Type.Missing, "tmpExport", csvfile, true, Type.Missing, Type.Missing);

MsAcs.DoCmd.DeleteObject(Microsoft.Office.Interop.Access.AcObjectType, "tmpExport");

Gives Error

Upvotes: 2

Views: 3053

Answers (2)

Fionnuala
Fionnuala

Reputation: 91376

Both of these work for me:

using Access = Microsoft.Office.Interop.Access;
<...>
   MsAcs.DoCmd.DeleteObject(Access.AcObjectType.acQuery, "query1");

And drop table, yes, it is a query, nonetheless, drop table.

   MsAcs.CurrentDb().Execute("DROP TABLE query2");  
   MsAcs.Quit();

Make sure you have no databases in task manager running from previous testing.

Upvotes: 0

HansUp
HansUp

Reputation: 97131

DeleteObject expects a constant from the AcObjectType enumeration. Instead of the full enumeration (AcObjectType), use AcObjectType.acQuery as the first argument to DeleteObject, and the name of your query as the second argument.

From within an Access application session, I can delete a query named tmpExport with either of these 2 methods:

DoCmd.DeleteObject acQuery, "tmpExport"
CurrentDb.QueryDefs.Delete "tmpExport"

However, for either of those to work, the db must be updatable, not read-only. So when those methods work, Debug.Print CurrentDb.Updatable replies True.

If I open the database read-only, CurrentDb.Updatable replies False, and either attempt to delete tmpExport throws error 3027, "Cannot update. Database or object is read-only."

Unfortunately I don't know how to translate those statements to c#.

Upvotes: 2

Related Questions