Tải bản đầy đủ - 0 (trang)
Hack 65. Break Through VBA's Transformation Barrier

Hack 65. Break Through VBA's Transformation Barrier

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





Application.ExportXML(ObjectType,DataSource,DataTarget,SchemaT



ThePresentationTargetargumentdoeshavesomethingtodowith

transformation,butit'sonlyforoutput.Itidentifieswhere

AccesswillputastylesheetforturningtheXMLintoHTML

basedonitsownexpectations,notyours.

Youcangetaroundtheseproblemsintwoways.First,youcan

writesomecustomcode.Theimportversionwillinstantiatean

XMLparser(probablyMSXML),readthecontentfromthe

documenthoweveryoudeemappropriate,andthenuseADO,

DAO,orSQLUpdatequeriestoputthedatainthedatabase.The

exportversionwillreaddatafromthedatabaseandwriteitto

anMSXMLDOMtreeasnecessary.

Thismightbeappropriateifyouhavecomplicatedcases,but

it'salotofcodeforwhat'smostlikelyasimpleproblem,and

youcan'ttesthowitworks(orreusethatwork)outsideof

Access.

Amorelikelyapproach,ifyoucanstandworkingwithXSLT,is

toaddastepbeforetheimportoraftertheexportthat

performsanextratransformation.BecauseAccessdoesn'tlet

youpassobjectstotheimportorgetobjectsfromtheexport,

youneedtoworkwithtemporaryfilestoproducetheresults

youwant.Conveniently,youcanusethesamefunctionforboth

cases.

Asimpleversionofthisfunctionlookslikethis:

PrivateSubTransform(sourceFile,stylesheetFile,resultFile)

DimsourceAsNewMSXML2.DOMDocument30

DimstylesheetAsNewMSXML2.DOMDocument30

DimresultAsNewMSXML2.DOMDocument30



'Loaddata.

source.async=False

source.LoadsourceFile

'Loadstylesheet.

stylesheet.async=False

stylesheet.LoadstylesheetFile



If(source.parseError.errorCode<>0)Then

MsgBox("Errorloadingsourcedocument:"&source.parseErro

Else

If(stylesheet.parseError.errorCode<>0)Then

MsgBox("Errorloadingstylesheetdocument:"&_

stylesheet.parseError.reason)

Else

'Dothetransform.

source.transformNodeToObjectstylesheet,result

result.SaveresultFile

EndIf

EndIf

EndSub



Thetransformfunctiontakesthreearguments:thepathofa

sourcefileholdingtheoriginalXML,thepathofastylesheetfile

holdingtheXSLTthatwillbeusedtotransformit,andthepath

towhichtheresultingdocumentshouldbesaved.Typically,

you'llwanttocalltransformbeforeusingAccess'snativeImportXML

functionorafteryou'veusedtheExportXMLfunction.

Forexample,youmightimportXMLfilestoatabledirectlywith

thiscall:





Application.ImportXML"http://simonstl.com/ora/updateBo



ButifthatXMLfilestoredthedataasattributes,andyou

wantedtoapplyatransformationtothatdatabeforeyou

importeditintoAccess,youmightdothisinstead:

Transform"http://simonstl.com/ora/updateBook.xml",_

"C:\xslt\attsToElem.xsl",_

"C:\temp\tempImport.xml"

Application.ImportXML"C:\temp\tempImport.xml",acAppendData



Similarly,youcanapplyatransformationafteryouexported

data,turningitintoHTML:



Application.ExportXMLacExportTable,"books","C:\temp\tempExpo

Transform"C:\temp\tempExport.xml",_

"C:\xslt\booksToHTML.xsl",_

"C:\export\exportedBooks.html"



WritingXMLdocumentsouttofilesandthenreparsingthem

isn'tefficientbyanymeans,butitpatchesagapleftbythe

AccessAPIforimportingandexportingXML.Unlessyou're

dealingwithhugevolumesofdata,ordoingthisprocessing

constantly,usersofyourdatabasesaren'tlikelytonoticeabig

difference.Importandexportareusuallyprettyslowoperations

anyway.



7.8.1.SeeAlso

"ImportVariedXMLDataintoAccess"[Hack#63]



"ExportXMLDataSanely"[Hack#64]

SimonSt.Laurent



Hack66.LeverageSQLServerPowerbyCalling

StoredProcedures



GetaleguponperformancewhenusingSQLServer

data.

DeveloperscreatingAccessapplicationsthatarefrontendsto

SQLServerdatabaseshavetwochoicesfortheirapplication

type.TheMicrosoft-recommendedchoiceistouseanAccess

dataproject(ADP),whichisdirectlytiedtotheSQLServer

database.Thisnative-modeOLEDBconnectionresultsina

lighter-weight,better-performingfrontendthatcandirectlyuse

SQLviews,storedprocedures,anduser-definedfunctions.It

alsoletsdevelopersdesignobjectsdirectlyontheserver(no

needtouseEnterpriseManager).

Despitetheseadvantages,manysituationsforcedevelopersto

useODBClinkedtablesinatraditionalAccessMDBfile.Notthe

leastoftheseistheabilitytocreatelocaltablesintheMDB(in

anADP,eventheSwitchboardItemstablemustbeonthe

server)andtheabilitytoconnecttootherdatasources(suchas

otherAccessdatabases,Excelspreadsheets,textfiles,andso

on).JustbecauseyouchoosetouseanMDBasafrontend

doesn'tmeanyouhavetogiveuptheserver-sideprocessing

powerofSQLServerstoredprocedures.



7.9.1.HookingUpwithODBC

WhenanAccessMDBisusingODBClinkstoSQLServer,all

dataprocessingisdoneontheclientsidethatis,withinAccess

ontheworkstation.Ifalistboxonaformgetsfilteredbya



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



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

Hack 65. Break Through VBA's Transformation Barrier

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

×