Tải bản đầy đủ - 0 (trang)
Hack 73. Write Code Faster by Turning Off Syntax-Checking

Hack 73. Write Code Faster by Turning Off Syntax-Checking

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

thatyouhavetoclear.



Figure8-2.Apeskywarningmessage



Figure8-3.TurningoffAutoSyntaxCheck



Ofcourse,it'sagoodideatoturnsyntax-checkingbackon

whenyoufinishenteringcode!



Hack74.SubstituteDomainAggregate

FunctionsforSQLAggregateFunctions



Reducetheamountofcodeyouenterandstillgetthe

sameresults.

WithinVBAcode,itisacommonpracticetotapintotheADO

objectsandusesomeSQLtoquerydatainthedatabase.

BecauseSQListhedefactostandardforqueryingdata,

followingthisrouteisunderstandable.However,sometimesyou

don'tneedtoquerydatainthisway.

Forexample,ifyouneedtoprocessindividualrecords,using

ADOandSQLmakessense.Arecordsetiscreatedthatis

typicallyscrolledthroughusingtheMoveNextmethodwithinaDo

Untillooporsimilarconstruct.

Ontheotherhand,ADOandSQLaresometimesusedjustto

getanaggregatevaluefromasetofrecords.Inthissituation,

theindividualrecordsareofnoconcern.Instead,you'relooking

forasummary,suchasasum,acount,oranaverage.



8.4.1.TheCode

Example8-1showsaroutinethatusesADOandSQLtoreturn

thesumofsomeinvoiceamounts.

Example8-1.UsingADOandSQLtoreturnasum

Subget_SQL_Sum()

DimconnAsADODB.Connection

Setconn=CurrentProject.Connection



DimrsAsNewADODB.Recordset

rs.Open"SelectSum(Amount)AsSumOfAmountFromInvoices"&_

"WhereInvoiceDate=#12/10/04#",_

conn,adOpenKeyset,adLockOptimistic

MsgBoxrs.Fields("SumOfAmount")

rs.Close

Setrs=Nothing

Setconn=Nothing

EndSub



TheSQLstatementincludestheSQLaggregateSumfunction.

Also,thesumoftheamountsisfromasetofrecordsfilteredto

asingleinvoicedateof12/10/04.ThecodeinExample8-1

requirescreatingADOobjectsandthendestroyingthem

afterward(bysettingthemtoNothing).

Youcanboildownallthistoasinglelineusingadomain

aggregatefunction.



8.4.2.BoilingDowntheCode

DomainaggregatefunctionsprovidethesameresultsasSQL

aggregatefunctions.However,whereasyouneedtosomehow

embedSQLaggregatefunctionsintoaSQLstatement,youcan

codedomainaggregatesindependently.

Example8-2showshowashortroutineusingtheDSumdomain

aggregatefunctionreplacesthecodeinExample8-1.

Example8-2.UsingDSumtoreturnthesum



Subget_Domain_Sum()

DimamountAsSingle

amount=DSum("[Amount]","Invoices","[InvoiceDate]=#12/10



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

Hack 73. Write Code Faster by Turning Off Syntax-Checking

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

×