Tải bản đầy đủ - 0 (trang)
Hack 76. Shrink Your Code with Optional Arguments

Hack 76. Shrink Your Code with Optional Arguments

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

Subget_state_records(stateAsString,OptionalcityAsString)

DimconnAsADODB.Connection

Setconn=CurrentProject.Connection

DimrecsetAsADODB.Recordset

Setrecset=NewADODB.Recordset

Ifstate="MA"Then

recset.Open"Select*FromCustomersWhereState='"&stat

&"'AndCity='"&city&"'",conn

Else

recset.Open"Select*FromCustomersWhereState='"&state

EndIf

DoUntilrecset.EOF

''Processrecordshere

recset.MoveNext

Loop

recset.Close

Setrecset=Nothing

EndSub



Thesubroutinetakestwoarguments:state,whichisrequired,

andcity,whichisoptional.TheOptionalkeywordisplacedin

frontoftheargumentname:



Subget_state_records(stateAsString,OptionalcityAsString



Nokeywordisavailableforspecifyingthatanargumentisrequired.All

argumentsarerequiredunlessyouspecificallysetthemasoptional

withtheOptionalkeyword.Notealsothatoptionalargumentsmust

comeafteralltherequiredarguments.Youcanhavemultipleoptional

arguments,butyoumustprecedeeachonewiththeOptionalkeyword.



Let'sassumetherequirementshavechanged;now,anyNYorCT

recordscanbeused,butforMassachusetts(MA),weneedonly

recordsforwhichthecityisBoston.

Onewaytoaccommodatethisnewrequirementistouse

subroutines:onethatacceptsasinglestateargumentandone

thatacceptstwoarguments,forthestateandthecity.The

functionalitywouldwork,buttherewouldbemorecode.

However,byincorporatingthecityasanoptionalargument,you

canusethesinglesubroutineforallthestates.Massachusetts

recordsareaccessedbythetwoargumentsbeingpassed:

get_state_records"MA","Boston"



NewYorkandConnecticutrecordsareaccessedwiththesingle

requiredstateargument:

get_state_records"CT"



We'vemodifiedthesubroutinetohandletwotypesofqueries,

onewithasinglecriterionandonethatusesbothcriteria:







Ifstate="MA"Then

recset.Open"Select*FromCustomersWhereState='"





Else





recset.Open"Select*FromCustomersWhereS





EndIf



Andthat'sit!Byjustalteringtheexistingsubroutineabit,we

don'tneedtocreateasecondsubroutine.



Hack77.ProtectProgrammingCodefrom

CuriousUsers



Yourcodeisvaluable.Addpasswordprotectiontoyour

modules.

Ithinkyou'llagreethatdevelopedcodeisvaluable,intermsof

thetimeandexpenseittooktocreateitaswellaswhatwould

beinvolvedtorecreateit.So,likeanyvaluableasset,alittle

protectiongoesalongway.Ofcourse,IwouldberemissifI

didn'tmentionthatthefirstthingyoushoulddoishavea

backupsysteminplace.Butevenifyoudohaveaback-up

system,ifthecodeinaproductionsystemgetstrampled

somehow,itstillcoststhecompanytotakedownthesystemto

fixthecode.

Luckily,thereisaneasywaytoprotectyourcode.Thishack

preventsallbutthosewhoknowthepasswordfromseeingyour

code.Toaddthisprotection,youmustaccesstheTools

PropertiesmenuintheVBeditor,asshowninFigure8-4.Inthis

example,themenuitemisCustomersPropertiesbecausemy

codeprojectisnamedCustomers.Yourmenuitemwillreflect

thenameofyourcodeproject.



Initially,thenameofthecodeprojectissettothenameofthe

database.YoucanchangethisintheGeneraltabontheProject

Propertiesdialogbox(Tools

Properties).



Figure8-4.Accessingtheprojectproperties



OntheProtectiontabintheProjectPropertiesdialogbox,select

theoptiontoprotecttheprojectandestablishapassword.

Enterapassword,andclickOK,asshowninFigure8-5.Don't

forgetthatpasswordsarecase-sensitive.



Figure8-5.Protectingtheproject



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

Hack 76. Shrink Your Code with Optional Arguments

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

×