Stop Users Closing Access when a form or report it open. (1 Viewer)

ThePris6ner

New member
Local time
Today, 18:29
Joined
Aug 13, 2018
Messages
3
Hi all,

My question is: Can vba tell me which forms or reports are currently open in MS Access?


Further explanation:

I have an Access 2007 database, and I have been tasked with solving the issue where users accidentally close Access by clicking the red X in the top right hand corner. This usually occurs when a user is viewing a report and they wish to close it. Instead of clicking the 'Close Print Preview' button they instinctively go for the red X.

I first looked into disabling the red X, and this can be done with code, but I am reluctant to do it. The reason being that I don't want users getting stuck without any way of closing access. This could happen if there is an error or for some other reason, the get stuck in a dead end.

The other option I have thought of, is writing code that looks at which forms are open when the user tries to close access. If there are any reports open, a message box alerts them and asks them if they wish to close access.

Which leads to my question: Is there vba code which can tell me the names of all the open forms or reports?

I know I can ask 'is a form loaded', but I have so many forms, I cant do this for all of them.

I would be grateful for any help or advice
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:29
Joined
May 21, 2018
Messages
8,525
The Forms collection has a pointer to all open forms, the reports collection contains a pointer to all open reports

Code:
dim frm as access.form
dim strForms
for each Frm in Forms
  strForms = strForms & vbcrlf & Frm.name
next frm
msgbox "open forms " & strForms

would be the same for reports
dim rpt as access.report
...
for each rpt in Reports
...
 

ThePris6ner

New member
Local time
Today, 18:29
Joined
Aug 13, 2018
Messages
3
Hi MajP,

Thanks for your quick reply, I had a play with that code and it looks good, and I should be able to use it for what I want. Thanks for the link, that is where I got the disable red x code from, but the warning about dead-ends and users being forced to use task manager to close access put me off.

Thanks again :)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:29
Joined
May 21, 2018
Messages
8,525
As the article points out it would be nice if there was an event for application close, unfortunately there is not.
Be careful what you ask for! Now that your users cannot exit your program by closing Access directly, you need to make sure your application doesn't get stuck in an infinite loop or dead-end (where there's no exit). You'll also need to properly handle errors and exit gracefully if there's a problem. If not, your users will be forced to use Task Manager to close your instance of Access. Not good.

You will need to do a good job of error handling to avoid potential issues.
The other issue
The problem with this is that it's triggered once the Access' close process starts, so other objects in the application may have closed already before this form closes which may leave the user in an inconsistent state.
To me that can be avoided with proper design, of not having a lot of open forms with dependancy.

The other option I have thought of, is writing code that looks at which forms are open when the user tries to close access.
So as the article points out, you cannot do this because there is no application close event.
 

Dangerous

Registered User.
Local time
Today, 18:29
Joined
Oct 18, 2018
Messages
73
Hi all,

My question is: Can vba tell me which forms or reports are currently open in MS Access?


Further explanation:

I have an Access 2007 database, and I have been tasked with solving the issue where users accidentally close Access by clicking the red X in the top right hand corner. This usually occurs when a user is viewing a report and they wish to close it. Instead of clicking the 'Close Print Preview' button they instinctively go for the red X.

I first looked into disabling the red X, and this can be done with code, but I am reluctant to do it. The reason being that I don't want users getting stuck without any way of closing access. This could happen if there is an error or for some other reason, the get stuck in a dead end.

The other option I have thought of, is writing code that looks at which forms are open when the user tries to close access. If there are any reports open, a message box alerts them and asks them if they wish to close access.

Which leads to my question: Is there vba code which can tell me the names of all the open forms or reports?

I know I can ask 'is a form loaded', but I have so many forms, I cant do this for all of them.

I would be grateful for any help or advice

If you can disable the Close Access X then write a bit code assigned to a button tucked away elsewhere on the form which when clicked calls up a msgbox asking if they wish to close the DB. If Y then run the close routine and any other key cancels the close action.
 

Users who are viewing this thread

Top Bottom