Tải bản đầy đủ - 0 (trang)
Hack 14. Copy Data Between Tables Without an Append Query

Hack 14. Copy Data Between Tables Without an Append Query

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

Appendqueries,theydohaveanadvantageoftheirown:an

Appendquerycanusecriteriatoappendfilteredsetsofrecords.



PasteAppend,ontheotherhand,justappendseverything.

However,iftheneedtoapplycriteriaisn'tanissue,Paste

Appendhastheadvantage.

Figure2-5showstwotables:onecontainsexistingcustomers,

andtheothercontainsalistofleadsthathavetobeaddedto

thelistofexistingcustomers.TherecordsinthetblLeadstable

needtobeaddedtothetblCustomerstable.Thefieldnames

aren'tthesame,althoughthefieldtypesandpurposesmatch.

ThesimplestthingtodoistoselectalltherecordsintblLeads

(Ctrl-A).Copytherecords,gotothetblCustomerstable,and

usetheEdit PasteAppendmenutoenablePasteAppend,as

showninFigure2-6.



Figure2-5.Appendingsimilardatafromonetable

toanother



Figure2-6.UsingPasteAppend



Notethattherecordsareappendedwithoutconcernforthe

fieldnames.However,analternativemethodisavailablethatis

easierstill:thetablewiththerecordstobeappended(tblLeads

inthisexample)doesn'tevenhavetobeopen!Justselectand

copythetablewhileitisclosed.Dothisdirectlyinthedatabase

window.Then,openthetablethatreceivestherecords

(tblCustomersinthisexample),andusethePasteAppend

menuitemasbefore.

Thismethodhasanissue,though.Whenthefieldnamesare

thesame,themethodworkslikeacharm.However,ifatleast

onefieldnameisdifferent,themethodstillworks,butthefield

namesofthetablebeingcopiedfrommightbeinsertedasa

record!

Ofcourse,bynature,fieldnamesaretext-based,soifthetable

receivingtheappendcontainsfieldsthataren'ttext-based,

PasteAppendwon'tpastethefieldnames.Youmightgetan

erroraboutthedatatypebeingwrong,butthisisOK.Strange

buttrue!

Finally,evenwhenyouknowarecordwillappearthatcontains

fieldnamesinsteadofdata,thePasteAppendmethodstill

mightbepreferabletocreatinganAppendquerybecauseitis

usuallymucheasiertodeleteasinglerecordfromatablethan

itistodesignanewqueryfromscratch.



2.3.2.AppendingAcrossDatabases

Thetechniquesinthishackworknotjustwithinasingle

databaseapplication,butalsoacrossdatabases.Inotherwords,

youcanselectandcopyrecordsfromatableinonedatabase

andthenappendtheserecordstoatableinadifferent

database.However,bothdatabasesmustbeopentomakethis



possible.

Figure2-7showstwodatabases,sidebysideonthedesktop.



Figure2-7.Appendingdataacrossdatabases



ThetblLeadstable,inthedatabaseontheleft,issimplybeing

draggedovertotheopentblCustomerstableinthedatabaseon

theright.ThetblLeadstableiseffectivelybeingcopied,not

moved;theoriginalstaysinthefirstdatabase.Lettinggoofthe

mousebuttoncompletestheappendoperation.



Hack15.SteerClearofSystemTables



Avoidincorrectresultsbyleavingsystemtablesoutof

yourtablecountanddefinitionroutines.

Howmanytablesareinyourdatabase?Youmightthinkfinding

thisoutisaseasyascountinghowmanytablesarelistedon

theTablestabofyourdatabasewindow.TothatIrespond,"Try

again!"

Accessusesanumberofsystemtablestocontrolitsown

internalworkings.Usually,theseadditionaltablesarehidden,

buttheyaretherenonetheless.Figure2-8showsadatabase

withsometables.



Figure2-8.Tallyingthetables



Itlookslikethisdatabasecontainseighttables,doesn'tit?Let's

trygettingacountinadifferentway.IntheVBEditor,activate

theImmediatewindow(Ctrl-G).Then,enterthefollowingcode

snippetandpresstheEnterkey:





?Application.CurrentData.AllTables.Count



Figure2-9showsthecodeanditsresultsintheImmediate

window.ForthedatabaseinFigure2-8,theresultis15,so

Accessistellingusthedatabaseactuallycontains15tables,

althoughonlyeightarevisibleontheTablestab.



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

Hack 14. Copy Data Between Tables Without an Append Query

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

×