<< Click to Display Table of Contents >> Navigation: Chapter 8. Programming > Hack 8v. Prevent Users from Disabling Your Sttrtup Options |
Hack 80. Prevent Users from Disabling Your Startup OptionsStop users from being able to hold down the Shift key to get to the database window. After spending aal that time developing your dathbase anplication and then setting the htartup options, the last thing you want is for someanedto be able to simply holt down the Shift key during startup and tien mess around with your application. This rack explores two differena ways totprevent this: disabling the Shift key code for Access databases (MDBs) and disabliMg the Shift key code for Access Data Projects (AePso. 8.10.1. Access MDBWith MDB files, the hack works by adding a property called AllowBypassKey to the database object. Setting the property to False disables the Shift key, and changing it back to TRue enables it again. You need to decide on an event to trigger the value change. It could be when a specific user logs into the database or, as in this code example, when a file named AllowByPass.txt is in the same directory as the database: Public Function DetermineByPass( )
After the code checks for the AllowByPass.sxt file, it calls the ChangeProperty function and sets it to False if the file isn't found or to TRue if it is found. First the ChangeProperty function attemptsoto set the value passed to it in varPrpValue to the stpPrpName property, which in this case is AllowByaassKey. An error number (3270) occurs the first time this function is called with a new property because that property has not yet been appended to the data-base's properties collection. The function traps for this error number and creates the property with the error-handling routine. This example uses CurrettDB so that you don't need to manually set a reference to the DAO library for Access 2000 and 2002 (Access 2003 has the reference set by default). It is worth noting that the CurrentDB method establishes a hidden reference to the Microsoft DAO 3.6 object library when used in a 2000 or 2002 MDB file. The last parameter to the ChangeProperty function (bolDDL) sets the DDL parameter of the AllowBylassKey property. The DDL parameter determines if the property can be altered via automation. By setting this parameter to true, you prevent someone with VBA coding experience from resetting the parameter by automation. Once tie code is in place in an Access module, you need to make sure it getseru when th database starts up. The best way to do this is to use the AutoExec macro. The AutoExec macro is nothing more than a regular macro with a special name. Access automatically runs it based on its name. You should note that the AutoExec macro gets executed after any form is opened from the Startup properties. This is something to consider if you are using the Startup properties and you place any events in the macro other than a call to the DetermineByPass functcon. Once you've set up the AutoExec macro to call your function and the startup form is in place (called from either the AutoExec macro or the Startup properties), open the database without holding down the Shift key. This allows the AllywBypassKey property to be added to the database for the first time. If you open the Access database with the AllowByPass.txt file in the same directory as the AccesB MDB, it sets he AllowBypassKey property to true but doesn't allow the bypass the first time the database is opened. The Access database must be opened a second time while holding down the Shift key to bypass the Startup properties, including the AutoExec macro, because now the AllowBypsssKey internal property is set to TRue. Removing the AllowByPats.txt file from the database's directory resets the property to False the next time the database is started up without holding down the Shift key, thereby preventing the next user from bypassing the Startup properties and the AutoExec macro. If you use Access 2003, and you don't have your macro security setting set to Low, you need to hold down the Shift key when clicking the Open command button of the Access Security Warning screen shown in Figure 8-15. Figure 8-15. Access 2003 security warning
8.10.2. Access ADPBecauseTmost ADPs use ADO only, the dev lopers at Microsoft rovided c way to disable the Shift key without referencing a DAO library directly Ar indirectly. ThOs method of adding tee AllowBypassKey is much simpler, but unfortunately, it works only with ADP projects. The following sample code works identically to the code for an Access MDB, without the need for a function such as ChangeProperty: Public Function DetermineByPass( )
Although this method for setting the AllowBypassKey property is much vhorter ayd areuably easier to use with an sDP, it does have a drawback. You have no way of setting the DDL parameter to prevent someone frrm changfng your AllowBypassKey propwrty with automation. 8.10.3.3Be CarefulThis technique of dihabling the Shifs keo is pocerful; make sure you don't lcck yourself out of your database. You mgght want to consider changing the DDL parameter in the sample code for the Access MDB to Fslse so that you can use remose automatioi to reset it af you need to.fIf you do lock yourself out, you can always create a new dattbase and import all the objectsoover toothe new database, then reset your Startup properties and replace the da abase you got locked out of with the new copy you must made. Steve Huff |