View Full Version : Define how Access opens using VBA


sstasiak
03-20-2007, 07:43 AM
Hey guys

Instead of typing it over again, you can view my issue in this thread I posted in the "Forms" room:

http://www.access-programmers.co.uk/forums/showthread.php?t=125180

Is there a way to use VBA code to open Access with ONLY the default form and NOTHING else? I'm talking no toolbars, menus, NOTHING but the login form.

I'm using Access 2007 if that makes a difference.

modest
03-20-2007, 07:59 AM
Search the forum for no right click. This issue has already been addressed. Additionally, search for no toolbars. You can turn off all toolbars through code when the form opens.

sstasiak
03-20-2007, 09:19 AM
I did a search and nothing I found is working for me.

I use all the code that ghudson provides in his code listing, and it's not working.

When I try to hide menus, using his code, it only hides the "Access objects" list and disables right clicking in the forms. I need ALL menus disabled and right clicking disabled for the entire DB, meaning if I right click on the "Home" ribbon, I won't get options to edit Access Options.

I'll keep trying though.

Again, I'm using Access 2007

sstasiak
03-20-2007, 11:18 AM
Still haven't found anything that works. With the "Home" ribbon and Office button still there, a curious user can right click either one, go into Access Options, change the startup form from the main login form to the main form, thereby negating any security I have set up through forms.

The only thing that I think would work, is if I hide the Access window altogether, and just display the forms. That way there is nothing the user can right click.

But I still don't know how to do this in 2007.

HELP!!

JohnInStockie
03-20-2007, 12:18 PM
Hi

I take it you dont want to hide the whole access window completely then, or do you mean that using windows to hide the acccess window doesnt work?

Im refering to the Windows API access window hide routine from the access web.


John

sstasiak
03-20-2007, 12:35 PM
John

Right now in the "Current Database" view in "Access Options", I have the navigation pane, ribbons, menus, and shortcut menus disabled. However, when I close and re-open the DB, I still see the "Home" ribbon and the Office button on the top left corner. With those items there, the user can right click on them and select "Customize Quick Access Toolbar". Once in that customization screen, they can set the default form to be whatever they want. This totally bypasses the security I have manually built in.

I have 2 options:

1. Find out how to get rid of the "Home" ribbon and the "Office" button(or disable right clicking on them....the methods to do this that I found on here didn't work. It didn't prevent right clicking on the ribbons).

2. Make it so that the Access window doesn't actually open when the DB is opened. Meaning when I double click the icon for the DB, only the default form opens, and not the actual MS Access window.

I tried all kinds of code I found from the site and none of it works. I only saw it listed as working for Access 2003, not '07.

PC User
03-21-2007, 10:57 AM
I have a question about using the API hide access window module. This works ok for my forms, but the report preview window always gets hidden or shows up behind the form. How can I use this module and have the preview reports in front of the form?

Thanks,
PC

boblarson
03-21-2007, 11:12 AM
You have to close the form before opening the report because the forms are set to "popup" which keeps them on top.

PC User
03-22-2007, 05:55 AM
Thanks for your reply. When I close the form for the report to appear, it works. However, when I try to get the form to return to its previous state, the API module (hide/show access window) doesn't handle the form right. Have you seen a sample of the correct handling of the API module when previewing a report and then returning to the previous form? I can't seem to get it to work.

Thanks,
PC

boblarson
03-22-2007, 06:05 AM
I didn't use the API when hiding the Db window. I just made use of pop up forms and then having to close the form, maximize the db window, open the report, then in the close of the report minimizing the db window and then opening the form.

vbaDev
03-22-2007, 06:31 AM
we need to distinguish between the Database window (that shows your tables, forms, etc. This DB window is inside the Access window).

The Access Window, is the main container form (like that of Word or Excel which contains the documents). This window has the toolbars and menus, etc.

it's possible to hide the DB window via settings.

to me knowledge it's not possible to hide the Access window without API and even that has its drawbacks/difficulties, because your forms must be modal and popup and also if you open any reports/tables, you need to have your form close (and then reopen after the report/table is closed).

Although really cool, in my experience, it's not necessarily practical, but that depends on your app.

boblarson
03-22-2007, 06:32 AM
It is possible to hide the Access window without the API (it still shows in the task bar, but yes - you can hide the application window without using the API)

I didn't like all of the hassle of having to make sure that things happened in the correct order for it to work, so I just stopped doing that years ago.

PC User
03-22-2007, 07:51 AM
That's a good point about clarifying terminology. The website "The Access Web" at http://www.mvps.org/access/api/api0019.htm uses the term "The Main Access Window." I use the code from this site to show only the active form, but is cumbersome to figure out how to effectively do a report preview and return to the previous form.

How do you do the same thing without API?

Thanks,
PC

boblarson
03-22-2007, 08:10 AM
You set all of your forms as Popup, and you have to set the code to minimize the app window first, then open the form. Then, if you need to open a different form (only one can be open at a time with this method - at least that I've found), you have to close the first one and then open the second. To open a report from a form, you have to first close the form, restore the app window, open the report and then have code in the report's on close event to minimize the app window and then open the form. ...Painful process.

vbaDev
03-22-2007, 08:19 AM
painful, but... no API! Could you please provide sample code?

I like the fact that Access could still show up in Taskbar. changing window's modality could be handled from code, could it not?

Thx!

boblarson
03-22-2007, 08:26 AM
There really isn't any code to give you. It's just a matter of running the normal form opening and closing code, the report opening code, and code within the on close event of the reports. The key things to remember are:

1. When Access opens you need to minimize the app window using DoCmd.RunCommand acCmdAppMinimize and THEN open the form. So, an autoexec macro is probably the way you have to go instead of setting a startup form.

2. Make sure all forms are set to POPUP.

3. Then, whenever you need to move from form to form, you will close the first form first and then open the second (the code to close the first and open the second can be on the first form)

4. When opening a report, use
DoCmd.Close acForm, Me.Name, acSaveNo
Docmd.RunCommand acCmdAppMaximize
Docmd.Open Report "YourReportName"

5. Then in each Report's on close event, put
DoCmd.RunCommand acCmdAppMinimize
DoCmd.Open Form "YourFormNameHere"

vbaDev
03-22-2007, 08:35 AM
Hey, thanks! It works, even if I put the docmd.RunCommand acCmdAppMinimize line into my main form's OnOpen event (you see Access for a sec, then it's minimized). And I don't even have to close my main form when a dialog box opens (a custom form opened via acDialog argument of the open form method).

So when you view the reports, Access must be visible, right? That would be the downside, of course.

But I still agree overall: it's not that practical and wouldn't impress anyone. In most companies that use Access, users are usually familiar with Access toolbars and can be trusted with many actions (and depend on them), unless your system is a complete app (which isn't always the case in this job).

Still, I had to know how to do it! Thanks!

PC User
03-22-2007, 04:41 PM
Thanks for the discussion. I'll have to try your method for my reports. As for forms, I don't open and close them. Instead, I design my forms to swap subforms and I use multiple subforms. See this example (http://www.utteraccess.com/forums/showflat.php?Cat=&Board=48&Number=1345174&page=0&view=collapsed&sb=5&o=&fpart=1)

Thanks,
PC

gold007eye
03-23-2007, 10:21 AM
I use this non-API method of hiding the database window for all my access applications. IMO it looks better and more like a program only taking up a little of your screen. The method I use it when my default form opens (Main Menu) I have the On Open Event Procedure set as:

Private Sub Form_Open(Cancel As Integer)
DoCmd.RunCommand acCmdAppMinimize
End Sub

So when the db opens it automatically hides the DB window/ access window.

@sstasiak

If you want to turn off toolbars and such programatically simply create 2 modules and put this code in it.

Module - turnon:
Option Compare Database
Option Explicit

Function toolbarson()
DoCmd.ShowToolbar "Menu Bar", A_TOOLBAR_YES

'DoCmd.ShowToolbar "Database", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Relationships", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Table Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "table Datasheet", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Query Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Query Datasheet", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Form Design", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Form View", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Filter/Sort", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Report Design", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Print Preview", A_TOOLBAR_YES
DoCmd.ShowToolbar "Toolbox", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Palette", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Macro", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Microsoft", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Web", A_TOOLBAR_NO
DoCmd.ShowToolbar "Formatting (Page)", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Print Menu", A_TOOLBAR_YES

End Function

Module turnoff:
Option Compare Database

Function toolbarsoff()

DoCmd.ShowToolbar "Menu Bar", A_TOOLBAR_NO
DoCmd.ShowToolbar "Database", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Relationships", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Table Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "table Datasheet", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Query Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Query Datasheet", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Form Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Form View", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Filter/Sort", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Report Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Print Preview", A_TOOLBAR_NO
DoCmd.ShowToolbar "Toolbox", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Palette", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Macro", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Microsoft", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Web", A_TOOLBAR_NO
DoCmd.ShowToolbar "Formatting (Page)", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Print Menu", A_TOOLBAR_NO

End Function

Just adjust the commented toolbars to show or not show which ever toolbars; if you create your own custom toolbars you can also set that code in here as well.

Once you have the modules set up then just create a macro called "Autoexec" (this will run automatically everytime you open the database so you won't have to worry about adding this code to a button or form)

In the Autoexec macro refer to the "turnoff" function.

RunCode
toolbarsoff ()

**When hiding the database window -
On the note of only being able to have 1 pop-up form open at a time this is not true; you also don't have to set the Modal property to "YES" if you leave Modal as "NO" and PopUp as "YES" for the form(s) then it will allow you to open more than 1 form at a time and still be able to switch focus between the different forms. What's better is that if you minimize the forms they will minimize just above the "Start" button instead of all going to the taskbar seperately.

There is a way to view reports without having to Unhide the access db window. You can set the reports PopUp property to "YES" and this will open just like the forms. I have leaned away from this because I did find the only downside is trying to get the popup report sized correctly to be able to view easily, but it is possible to do.

gold007eye
03-23-2007, 10:26 AM
But I still agree overall: it's not that practical and wouldn't impress anyone. In most companies that use Access, users are usually familiar with Access toolbars and can be trusted with many actions (and depend on them), unless your system is a complete app (which isn't always the case in this job).

I have learned in my company that the less you let the user do the better off you are. Lot's of people her like to "see how things work". So needless to say I have created my own security/design features to prevent these users from being able to do so (without using MsAcess Security which is a nightmare) The databases all look like applications and the user can only see and go where I let them or they have the access level to get to based on their network ID. It's amazing what you can do. You might also want to consider disabling the "SHIFT" function. (Holding shift while opening the database will bring up the database showing the DB Window where the user can see/modify,etc forms, table, etc.) If you need any code for that let me know. It is definetly a good feature to implement.

sstasiak
03-23-2007, 11:32 AM
I should have updated this thread a few days ago....I figured out how to disable pretty much everything but what the user can do in the forms.

Once the DB is converted to .accde(i'm using 2007), you have to change the extension to .accdr. This opens the DB using the runtime switch. Pretty easy solution.

vbaDev
03-23-2007, 12:34 PM
This works great, actually, much better for my purposes than the API method.

Access is hidden and the form can still be minimized/restored from the taskbar and switched to from other apps via the Taskbar.

I also have a Show/Hide access toggle button on my form, for which I have a Select Case structure (based on button's state).

One is exactly the same as the form's On-Open event:

docmd.runcommand acCmdAppMinimize

and the other, to restore Access:

docmd.runcommand acCmdAppRestore

but these commands don't work from my toggle button. Why is that?
I also tried the Maximize method instead of Restore and with it, but it didn't work.

2nd question, of course, is how to allow viewing of reports and I also have to display a table at one point (i know i can make a form based on the table, but can i show the table?)

Problem is, in Access 2000 reports don't have a PopUp property yet :(

Thanks!

I use this non-API method of hiding the database window for all my access applications. IMO it looks better and more like a program only taking up a little of your screen. The method I use it when my default form opens (Main Menu) I have the On Open Event Procedure set as:

Private Sub Form_Open(Cancel As Integer)
DoCmd.RunCommand acCmdAppMinimize
End Sub

So when the db opens it automatically hides the DB window/ access window.

@sstasiak

If you want to turn off toolbars and such programatically simply create 2 modules and put this code in it.

Module - turnon:
Option Compare Database
Option Explicit

Function toolbarson()
DoCmd.ShowToolbar "Menu Bar", A_TOOLBAR_YES

'DoCmd.ShowToolbar "Database", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Relationships", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Table Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "table Datasheet", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Query Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Query Datasheet", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Form Design", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Form View", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Filter/Sort", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Report Design", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Print Preview", A_TOOLBAR_YES
DoCmd.ShowToolbar "Toolbox", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Palette", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Macro", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Microsoft", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Web", A_TOOLBAR_NO
DoCmd.ShowToolbar "Formatting (Page)", A_TOOLBAR_YES
'DoCmd.ShowToolbar "Print Menu", A_TOOLBAR_YES

End Function

Module turnoff:
Option Compare Database

Function toolbarsoff()

DoCmd.ShowToolbar "Menu Bar", A_TOOLBAR_NO
DoCmd.ShowToolbar "Database", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Relationships", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Table Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "table Datasheet", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Query Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Query Datasheet", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Form Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Form View", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Filter/Sort", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Report Design", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Print Preview", A_TOOLBAR_NO
DoCmd.ShowToolbar "Toolbox", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Palette", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Macro", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Microsoft", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Web", A_TOOLBAR_NO
DoCmd.ShowToolbar "Formatting (Page)", A_TOOLBAR_NO
'DoCmd.ShowToolbar "Print Menu", A_TOOLBAR_NO

End Function

Just adjust the commented toolbars to show or not show which ever toolbars; if you create your own custom toolbars you can also set that code in here as well.

Once you have the modules set up then just create a macro called "Autoexec" (this will run automatically everytime you open the database so you won't have to worry about adding this code to a button or form)

In the Autoexec macro refer to the "turnoff" function.

RunCode
toolbarsoff ()

**When hiding the database window -
On the note of only being able to have 1 pop-up form open at a time this is not true; you also don't have to set the Modal property to "YES" if you leave Modal as "NO" and PopUp as "YES" for the form(s) then it will allow you to open more than 1 form at a time and still be able to switch focus between the different forms. What's better is that if you minimize the forms they will minimize just above the "Start" button instead of all going to the taskbar seperately.

There is a way to view reports without having to Unhide the access db window. You can set the reports PopUp property to "YES" and this will open just like the forms. I have leaned away from this because I did find the only downside is trying to get the popup report sized correctly to be able to view easily, but it is possible to do.

PC User
03-23-2007, 02:13 PM
I noticed that also. Access 2K doesn't have the PopUp property for reports. What version of Access are you using, gold007eye? Is there an alternate way to make a report into a PopUp or equivelant for Access 2K? I do like your non-API method of hiding the database window.

Thanks,
PC

PS. Can a new property like PopUp be added to AllReports using AccessObjectProperties?
I tried this, but can't get it to work.
Sub AddCustomReportProperty(strReportName As String, _
strPropName As String, _
varPropValue As Variant)
' Add custom properties to the AccessObjectProperties
' collection that is associated with an AccessObject object.

With CurrentProject.AllReports(strReportName).Propertie s
.Add strPropName, varPropValue
End With
End Sub

vbaDev
03-27-2007, 08:34 AM
Hi. I tried your method and it works, of course, BUT :)

1. while the report is open, users have access to the Access window. I know, I can disable many features, but isn't there a way around that, maybe somehow showing the report maximized or really even adding a PopUp property as someone suggested?

2. if report is maximized, user can always accidentally click the "Close" box of Access (and quit application) instead of Report's "close" button. That's not a good form either.

I wonder if we can all perfect this method. I know, the easier way is to upgrade Access :D but it's up to my employer, not me (and it's not as simple for large organizations).

Thanks!

There really isn't any code to give you. It's just a matter of running the normal form opening and closing code, the report opening code, and code within the on close event of the reports. The key things to remember are:

1. When Access opens you need to minimize the app window using DoCmd.RunCommand acCmdAppMinimize and THEN open the form. So, an autoexec macro is probably the way you have to go instead of setting a startup form.

2. Make sure all forms are set to POPUP.

3. Then, whenever you need to move from form to form, you will close the first form first and then open the second (the code to close the first and open the second can be on the first form)

4. When opening a report, use
DoCmd.Close acForm, Me.Name, acSaveNo
Docmd.RunCommand acCmdAppMaximize
Docmd.Open Report "YourReportName"

5. Then in each Report's on close event, put
DoCmd.RunCommand acCmdAppMinimize
DoCmd.Open Form "YourFormNameHere"

boblarson
03-27-2007, 08:41 AM
All of your questions are reasons why I chose not to use it. It is a big pain in the butt to try to implement and make all of the little idiosyncracies work. So, I don't have answers for you on that one as I abandoned trying to do this years ago for many of the same reasons listed in your questions.

vbaDev
03-27-2007, 08:44 AM
Thanks, Bob. It sure looks slick, though. I guess our answer is: we should use VB.NET or some real environment and not Access :)

Or maybe Microsfot could redesign Access to allow final apps to become stand-alone (or "almost" stand-alone).

no end to wishing...

:D

All of your questions are reasons why I chose not to use it. It is a big pain in the butt to try to implement and make all of the little idiosyncracies work. So, I don't have answers for you on that one as I abandoned trying to do this years ago for many of the same reasons listed in your questions.