Delete the Current Database (1 Viewer)

Pop_Access

Member
Local time
Today, 11:41
Joined
Aug 19, 2019
Messages
66
Is it possible to delete the current access database using VBA code?
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:41
Joined
Sep 21, 2011
Messages
14,310
Bit like sawing off the tree limb you are standing on? :)
 

GPGeorge

Grover Park George
Local time
Today, 11:41
Joined
Nov 25, 2004
Messages
1,873
Is it possible to delete the current access database using VBA code?

I can see a scenario where you'd do a juggling act to open a second accdb in order to do this, but boy would that be a high-wire act. Out of curiosity, what could possibly be the requirement behind this request?
 

jdraw

Super Moderator
Staff member
Local time
Today, 14:41
Joined
Jan 23, 2006
Messages
15,379
Further to George's post, can you describe the scenario that raised this question?
Easy to delete a database with file explorer, but why vba? And why the current database?
I'd like to think it isn't possible, but will await your rationale for this.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:41
Joined
May 7, 2009
Messages
19,245
Is it possible to delete the current access database using VBA code?
say your db is "y".
you open another instance of access and close "y" db.
from the new instance, delete "y".
quit the other db.
 

jdraw

Super Moderator
Staff member
Local time
Today, 14:41
Joined
Jan 23, 2006
Messages
15,379
And which is the "current database" in that scenario? Sounds like the second database "high-wire act" that GPG described.
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:41
Joined
Sep 21, 2011
Messages
14,310
I was thinking you could use Shell and Delete command?
Command would not work as file is in use
When you quit the DB, it *might* get deleted?

Try it?
Or a batch file that does it after a second or so?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 28, 2001
Messages
27,188
Unless you are prepared to do some complex gyrations, the answer is NO.

If you are executing code from a DB then that DB is open for reading by an active user (namely you) in the Access session. There will be a file lock associated with having a file still open. Most methods of deleting a file will try to take out an EXCLUSIVE WRITE lock on the file first before that method then issues the delete. But if there is a prior activity lock, even if only a SHARED READ lock, the attempt to establish the exclusive lock will fail due to a lock collision, and thus the deleting process will never GET to the point where the file system rules would allow the deletion. You would get an error "FILE LOCKED" and stop right there.

If this is a single-user file and ownership isn't a problem, you MIGHT be able to find the lock and downgrade it from SHARED READ to SHARED INTEREST (which is the least restrictive but non-zero lock). I wouldn't bet on that, but it MIGHT be possible. You could delete the file if the collision was over an INTEREST lock rather than a READ lock. However, lock interactions are complex on a good day and hellish when you are trying to side-step Windows file security. There are lock management API calls you could make to do the downgrade. Note that the file system might note that code is still executing from the file (assuming self-deletion) once you return from the API call and simply reassert the lock.

Arnel's solution doesn't work because your attempt to close the DB would fail if you are still executing code from a module in "y". You CAN open another DB (call it "z"), even in another instance, and you CAN run code from that DB - but at the code execution level, it is a subroutine call that leaves the caller's ("y") info still on the process stack and that leaves the READ lock still active. Mechanically speaking, there is a "reference count" property on a file handle and that count would not be zero, so the attempt to delete would fail on the non-zero counter. (Have you ever tried to delete an Access lock file when someone still had it open?)

In theory if you tried this two-instance approach, the critical moment would be when you exited the subroutine from "z" only to find that there was now a problem with a lower level stack reference to "y" that would lead to a BANG-ZOOM moment as your process aborted on some insane stack violation. Because regardless of the number of instances that are open, there is still only one process and still only one process stack involved. What counts is what is in memory, not in any of the files.

Regarding the afore-mentioned "complex gyrations: Using an API-based function, it is possible to mark a file to move to the wastebasket at next reboot.


However, there are restrictions and the file system can be "picky." Since you are talking about an app file rather than a folder, some of the file system restrictions don't apply, but if you were trying to delete a folder, it would have to be emptied first. You aren't, so this is an easier case. If you did this, your file would still run but would now be marked for deletion at the next reboot.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 19:41
Joined
Feb 19, 2013
Messages
16,616
I'm guessing this is a follow on from the OP's previous thread

if so you can probably render the db inoperable - delete the tables and queries and if supplied as a .accdb, delete most of the forms and reports. Don't know about standard and class modules.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:41
Joined
Feb 28, 2001
Messages
27,188
Gasman's approach of using a SHELL command might actually work, since SHELL creates a new process rather than making a persistent subroutine call in the original process. You would have to activate a batch file using SHELL in a way that allows execution to proceed in the Access instance so that you COULD perform the Application.Quit (or whatever else you were going to use). Your batch file would need a timer and a loop to verify that the lock file has vanished, after which it could delete the indicated file. Then you can make the batch file delete itself.


This uses a trick because you essentially declare the batch file to be a temporary file, so when it EXITs, the file system deletes it for you.

In my previous answer, none of the named approaches would work DIRECTLY because of what was on the process stack. But for a SHELL operation, that is a separate process.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:41
Joined
May 7, 2009
Messages
19,245
Out of curiosity, what could possibly be the requirement behind this request?
maybe, a demo version where on specific number of opening or specific date, it will get deleted.
 

GPGeorge

Grover Park George
Local time
Today, 11:41
Joined
Nov 25, 2004
Messages
1,873
maybe, a demo version where on specific number of opening or specific date, it will get deleted.
That certainly makes sense as a scenario, but seems really draconian for a supposed demo version. Demos tend to just stop working. If there was adequate warning, it wouldn't be so bad, but boy, I'd be unhappy if it happened without warning to a demo I'd installed.
 

Pop_Access

Member
Local time
Today, 11:41
Joined
Aug 19, 2019
Messages
66
First of all, and as I mentioned in previous posts, I'm not a programmer.

I appreciate your discussions, they were very wonderful
Thanks for your interest,

The reason behind asking this question is:
I have developed a commercial access software, and I want to be inactivate after a period of time that I specify (after the end of trial period). this software is used by many users (using frontend, while the backend is on the server).

it seems it's impossible to delete the current database as I asked.

@CJ_London, your guess is true. (y)

Appreciate your understanding
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:41
Joined
Sep 21, 2011
Messages
14,310
Done pretty well then for someone who is not a programmer? :)
 

Users who are viewing this thread

Top Bottom