Tải bản đầy đủ - 0 (trang)
Hack 80. Prevent Users from Disabling Your Startup Options

Hack 80. Prevent Users from Disabling Your Startup Options

Tải bản đầy đủ - 0trang

EndFunction

PublicFunctionChangeProperty(strPrpNameAsString,_















varPr















varPr















bolDD

DimprpAsVariant

ConstconPrpNotFoundError=3270

OnErrorGoToChangePrp_Err

CurrentDb.Properties(strPrpName)=varPrpValue

ChangeProperty=True

ChangePrp_Bye:

ExitFunction

ChangePrp_Err:

IfErr=conPrpNotFoundErrorThen'Propertynotfound

Setprp=CurrentDb.CreateProperty(strPrpName,_

















CurrentDb.Properties.Appendprp

ResumeNext

Else





'UnknownError





MsgBoxErr.Description





ChangeProperty=False





ResumeChangePrp_Bye



EndIf

EndFunction



AfterthecodechecksfortheAllowByPass.txtfile,itcallsthe

ChangePropertyfunctionandsetsittoFalseifthefileisn'tfoundor

toTRueifitisfound.

FirsttheChangePropertyfunctionattemptstosetthevaluepassed

toitinvarPrpValuetothestrPrpNameproperty,whichinthiscaseis

AllowBypassKey.Anerrornumber(3270)occursthefirsttimethis

functioniscalledwithanewpropertybecausethatpropertyhas



notyetbeenappendedtothedata-base'spropertiescollection.

Thefunctiontrapsforthiserrornumberandcreatesthe

propertywiththeerror-handlingroutine.

ThisexampleusesCurrentDBsothatyoudon'tneedtomanually

setareferencetotheDAOlibraryforAccess2000and2002

(Access2003hasthereferencesetbydefault).Itisworth

notingthattheCurrentDBmethodestablishesahiddenreference

totheMicrosoftDAO3.6objectlibrarywhenusedina2000or

2002MDBfile.

ThelastparametertotheChangePropertyfunction(bolDDL)setsthe

DDLparameteroftheAllowBypassKeyproperty.TheDDLparameter

determinesifthepropertycanbealteredviaautomation.By

settingthisparametertotrue,youpreventsomeonewithVBA

codingexperiencefromresettingtheparameterbyautomation.

OncethecodeisinplaceinanAccessmodule,youneedto

makesureitgetsrunwhenthedatabasestartsup.Thebest

waytodothisistousetheAutoExecmacro.TheAutoExecmacrois

nothingmorethanaregularmacrowithaspecialname.Access

automaticallyrunsitbasedonitsname.Youshouldnotethat

theAutoExecmacrogetsexecutedafteranyformisopenedfrom

theStartupproperties.Thisissomethingtoconsiderifyouare

usingtheStartuppropertiesandyouplaceanyeventsinthe

macrootherthanacalltotheDetermineByPassfunction.

Onceyou'vesetuptheAutoExecmacrotocallyourfunctionand

thestartupformisinplace(calledfromeithertheAutoExec

macroortheStartupproperties),openthedatabasewithout

holdingdowntheShiftkey.ThisallowstheAllowBypassKey

propertytobeaddedtothedatabaseforthefirsttime.

IfyouopentheAccessdatabasewiththeAllowByPass.txtfilein

thesamedirectoryastheAccessMDB,itsetstheAllowBypassKey

propertytotruebutdoesn'tallowthebypassthefirsttimethe

databaseisopened.TheAccessdatabasemustbeopeneda

secondtimewhileholdingdowntheShiftkeytobypassthe



Startupproperties,includingtheAutoExecmacro,becausenow

theAllowBypassKeyinternalpropertyissettoTRue.Removingthe

AllowByPass.txtfilefromthedatabase'sdirectoryresetsthe

propertytoFalsethenexttimethedatabaseisstartedup

withoutholdingdowntheShiftkey,therebypreventingthenext

userfrombypassingtheStartuppropertiesandtheAutoExec

macro.

IfyouuseAccess2003,andyoudon'thaveyourmacrosecurity

settingsettoLow,youneedtoholddowntheShiftkeywhen

clickingtheOpencommandbuttonoftheAccessSecurity

WarningscreenshowninFigure8-15.



Figure8-15.Access2003securitywarning



8.10.2.AccessADP

BecausemostADPsuseADOonly,thedevelopersatMicrosoft

providedawaytodisabletheShiftkeywithoutreferencinga

DAOlibrarydirectlyorindirectly.Thismethodofaddingthe

AllowBypassKeyismuchsimpler,butunfortunately,itworksonly

withADPprojects.Thefollowingsamplecodeworksidentically

tothecodeforanAccessMDB,withouttheneedforafunction



suchasChangeProperty:



PublicFunctionDetermineByPass()

IfLen(Dir(CurrentProject.Path&"\AllowByPass.txt"))=0T

CurrentProject.Properties.Add"AllowBypassKey",False

Else

CurrentProject.Properties.Add"AllowBypassKey",True

EndIf

EndFunction



AlthoughthismethodforsettingtheAllowBypassKeypropertyis

muchshorterandarguablyeasiertousewithanADP,itdoes

haveadrawback.YouhavenowayofsettingtheDDLparameter

topreventsomeonefromchangingyourAllowBypassKeyproperty

withautomation.



8.10.3.BeCareful

ThistechniqueofdisablingtheShiftkeyispowerful;makesure

youdon'tlockyourselfoutofyourdatabase.Youmightwantto

considerchangingtheDDLparameterinthesamplecodeforthe

AccessMDBtoFalsesothatyoucanuseremoteautomationto

resetitifyouneedto.Ifyoudolockyourselfout,youcan

alwayscreateanewdatabaseandimportalltheobjectsoverto

thenewdatabase,thenresetyourStartuppropertiesand

replacethedatabaseyougotlockedoutofwiththenewcopy

youjustmade.

SteveHuff



Hack81.InformUsersofaLongProcess



Whileyourcodeisconqueringalongloopingprocess,

usersmightthinktheirsystemhascrashed,unlessyou

providesomevisualcluethataprocessisrunning.

Whenauserclicksabuttontorunaprocess,andthatprocess

takesawhiletocomplete,theuserwon'tknowiftheprocessis

stillrunningorifthesystemhascrashed.Justimagineit:you

clickabuttononaformand…nothing.Aminuteortwolater,

stillnothing.Maybeeven5or10minuteslater,thesystemis

stillunresponsive.

Thisisnerve-wrackingfortheusersittinginfrontofhis

computer.Hehastoweighwhetherheshouldtrythebreakkey

orletthesystemcontinuetolooklikeitishungup.Onthe

otherhand,ifhestopsaprocessthatwasrunningsmoothly

afterall,hewilljusthavetostartitalloveragain.Ugh!

Don'tleaveyourusersinthispredicament.Youknowthat

100,000recordsarebeingprocessedandittakesawhile.But

yourusersmightnotknowthisandgetfrustratedwaiting.

ThishacktakesadvantageoftheSysCmdmethod.WithSysCmd,you

canwritemessagesinthestatusbarduringCPU-intensive

processing,evenwithscreenrefreshturnedoff.Figure8-16

showsaform(arathersimpleone,Iadmit).Thebuttonhas

beenclicked,andtheprocessischuggingaway.Noticethe

statusbarinthelower-leftcornerofthescreen;acontinuously

updatedmessageisbeinggeneratedthere.



Figure8-16.Providingafeedbackmessageinthe

statusbar



Themessageinthestatusbarsays,"Processing2741of8500."

Thinkinglikeaprogrammer,youprobablyrealizethattohave

thetotalnumberofrecordsbeingprocessedinthemessage

meansarecordcountpropertyisbeingused.



8.11.1.TheCode

Hereisthecodebehindthebutton:



PrivateSubcmdProcessSales_Click()

DimconnAsADODB.Connection

Setconn=CurrentProject.Connection

DimrecsetAsNewADODB.Recordset

Dimtotal_recordsAsLong

Dimrecord_numAsLong

record_num=0

recset.Open"Select*FromSalesRecords",conn,adOpenKeyset,

adLockOptimistic

total_records=recset.RecordCount

DoUntilrecset.EOF

recset.Fields("NetSales")=recset.Fields("Sales")-recset

Fields("Costs")

record_num=record_num+1

feedback_msg="Processing"&record_num&"of"&total_

SysCmdacSysCmdSetStatus,feedback_msg

recset.MoveNext



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 80. Prevent Users from Disabling Your Startup Options

Tải bản đầy đủ ngay(0 tr)

×