Tải bản đầy đủ - 0 (trang)
Hack 66. Leverage SQL Server Power by Calling Stored Procedures

Hack 66. Leverage SQL Server Power by Calling Stored Procedures

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

comboboxselection,alltherecordsarereturnedoverthe

networktoAccessandAccessappliesthefilter.Alternatively,

theuseofstoredprocedurescanincreaseperformanceinyour

AccessMDBsbyshiftingthefilteringtotheserver.Stored

proceduresarepowerfulbecausetheycombinethedata-joining

capabilitiesofAccessqueriesorSQLviewswiththeabilityof

VBAprocedurestoacceptparametersandtoloopandprocess

data.

T-SQL,MicrosoftSQLServer'sversionoftheSQLlanguage,is

somewhatdifferentfromtheJet(Access's)flavorofSQL.Itis

alsomuchdifferentfromVBA.However,ifyoucancreate

AccessqueriesandwriteVBAfunctions,youcanlearntowrite

SQLstoredprocedures.Itisn'tdifficulttobecomegoodenough

inT-SQLtoincreasetheperformanceofyourapplications.

WhetheryouinstallMSDE(theliteversionofSQLServerthat

shipswithMicrosoftOffice)orSQLServeritself,youcanlookat

thestoredprocedureswithintheNorthwinddatabasetoget

started.

TheADOlibraryisonewaytoexecutestoredproceduresin

Access.YoudothisinVBAbyexecutingaCommandobjectwhose

commandtextisthestoredprocedurename.Firstitis

necessarytoopenaConnectionobjectontheSQLServer

database.ThecodeinExample7-9executestheCustOrdersOrders

storedprocedurethatshipswithNorthwind,sendinginthe

much-abusedcustomeridALFKItofillanADOrecordsetwithallthe

ordersbelongingtoAlfredsFutterkiste.

Example7-9.Runningastoredprocedure



DimcnAsADODB.Connection

DimspAsADODB.Command

DimrsAsADODB.Recordset

Setcn=NewADODB.Connection

cn.ConnectionString=CurrentDb.TableDefs("dbo_customers").Conn

cn.Open

Setsp=NewADODB.Command



sp.ActiveConnection=cnSQL

sp.CommandType=adCmdStoredProc

sp.CommandText="CustOrdersOrders"

sp.Parameters.Refresh

sp.Parameters("@customerid")="ALFKI"

Setrs=sp.Execute



Access,however,can'tuseADOrecordsetsincertainsituations.

AlthoughAccessusesADOmoreandmorewitheverynew

versionrelease,Access2003stillhasdeeptiestoDAO,so

muchsothatMicrosoftputbackadefaultreferencetoDAOin

VBA,afternotincludingitinAccess2002(XP).Adata-entry

formboundtoalinkedtablewillhaveanunderlyingrecordset

thatisn'tADO,butrather,isDAO.Controlssuchascombo

boxesorlistboxes,onunboundorDAO-boundforms,require

theirrecordsetstobeDAOaswell.



7.9.2.CreatingaPass-ThroughQuery

AccesscantapintostoredprocedurepowerandgetaDAO

recordsetfilledwithdataviaastoredprocedureusingan

underutilizedfeatureknownasaPass-Throughquery.Creatinga

Pass-Throughqueryisrelativelystraightforward,andtheresults

returnedareinaDAOrecordset,appropriateforuseinany

Accessobjectorcontrolthatcanuseaqueryasitsdatasource.

TocreateaPass-Throughquery,selectQueriesintheDatabase

window,andclickNew.ClickDesignView,andthenclickOK.

ClickCloseontheTablelisttogodirectlyintoDesignview.On

theQuerymenu,clickSQL-Specific,andthenclickPassThrough,asshowninFigure7-40.



Figure7-40.CreatingaPass-Throughquery



ThequerydesignerwillswitchtoSQLviewandallowonlySQL

statementstobeentered.EnterCustOrdersOrders'ALFKI'inthe

SQLviewofthequerydesigner.ClickSave,andnamethequery

qry_CustOrdersOrders_pt.

Atthispoint,Accessdoesn'tknowwheretopassthisquery.On

firstexecution,youarepromptedforthedatasource

connectiontouse:cChoosethesamedatasourceyouusedto

linkyourSQLtables.Afterchoosingtheappropriatedata

source,AccesssendstheSQLstringcontainedinthequeryto

theserver,andSQLrunsthestoredprocedureandreturnsthe

resultstoAccess,asshowninFigure7-41.



Figure7-41.DatareturnedfromSQLServerviaa

storedprocedure



SteveConklin



Hack67.ManageWordDocumentsfromAccess



TapintotheWordobjectlibrarytocopyAccessdata

directlyintoaWorddocument.

AsisthecasewithallMicrosoftOfficeproducts,Wordhasa

significantnumberofexposedobjectsandmethodstowork

with,andbecomingfamiliarwithadecentnumberoftheseisa

challengeworthundertaking.

ThishackcreatesaprocedurethatplacesdatafromAccessinto

atableinaWorddocument.Theconceptsherealsoapplyto

otherWordmanipulations.Perhapsthiswillbeyourspringboard

intoanewavenueofOfficedevelopment.



7.10.1.HookingintoWord

InanAccesscodemodule,we'regoingtoplacearoutineto

workwithanexistingWorddocument.Tomakethisalittle

easier,we'llsetareferencetoWord'sobjectlibrary.We'lldo

thisinsidetheAccessVBEditor,usingtheTools References

menuandtheReferencesdialogbox,asshowninFigure7-42.

NotethatyourversionnumberoftheWordlibrarymightdiffer,

sousewhateveryouhave.



Figure7-42.SettingareferencetotheWord

objectlibrary



7.10.2.TheCode

Thenextthingtodoisenterthecode.Thismustgointoan

Accesscodemodule:



SubAccess_to_Word()

DimconnAsADODB.Connection

Setconn=CurrentProject.Connection

DimrecsetAsADODB.Recordset

Setrecset=NewADODB.Recordset

Dimrow_numAsInteger

Dimcol_numAsInteger

Dimword_docAsObject

'AssumesWorddocisinsamepath-changenameandpathasnee



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

Hack 66. Leverage SQL Server Power by Calling Stored Procedures

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

×