Tải bản đầy đủ - 0 (trang)
Hack 13. Create an AutoNumber Field with a Custom Value

Hack 13. Create an AutoNumber Field with a Custom Value

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

see,thefirstfieldisanAutoNumberfield,anditsaddressable

propertiesfillthelower-leftareaofthetabledesignwindow.

Notethatyouhavenowheretoinputadefaultstartvalue.



Figure2-1.AutoNumber,anincrementingfield

type



Tobeclear,thetabledoescontainaNewValuesproperty,but

allittellsyouiswhethernewvaluesareincrementedorare

random.Ittellsyounothingaboutstartingtheincrementata

valueofyourchoice.So,thefirstrecordwillhaveavalueof1

inthefield,thesecondrecordwillhaveavalueof2inthefield,

andsoon.

Tooverridethedefaultstartingvalueof1,youcanuseanAppend



querytoinsertadifferentstartingvalue.Afteryouhave

designedthetableandarereadytouseit,youmustgetthe

initialvalueinplace.Figure2-2showsanAppendquerythat

specificallyplacesavalueintheAutoNumberfield.Thatis,one

recordgetsaddedtothetable,withtheAutoNumberfield

receivingthedesignatedvalue.



Figure2-2.Usingaquerytosetthebeginning

AutoNumbervalue



NotethatyouenterthisqueryintheSQLview.That'sbecause

it'snotobvioushowtoenterthisqueryinthedesignview(the

querygrid),inwhichAppendqueriesaretypicallyusedtoappend

onetabletoanother.Thisoperationworksbyappendingavalue

toafieldinatablewithnoothertableinvolved(inapinch,you

candesignanothertablejusttoholdthevalue,butyoudon't

havetodoso).

Figure2-3showstheresultsofrunningtheAppendquery.The

Employeestablewasemptybutnowitcontainsitsfirstrecord,

andtheAutoNumbervalueforthatrecordis100.



Figure2-3.Thefirstrecordwiththedesignated

startingAutoNumbervalue



Notethatnootherfieldshavebeenpopulatedyet.Ifanyother

fieldsmusthaveavalue,youmustpopulatethemwith

appropriatevaluesinthequery.Forexample,youcanmodify

thequerytopopulateadditionalfields,suchasEmployeeand

Title,likethis:







INSERTINTOEmployees(EmployeeID,Employee,Title)

VALUES(100,'JohnSmith','Supervisor');



Figure2-4showstheresultofapplyingthisnewquery.Onthe

surface,thisseemstotakecareoftwobirdswithone

stonestartingtheAutoNumberwithavalueofyourchoice,yet

withoutusingadummyrecord(asinFigure2-3)todoso.

However,thisapproachcanbeproblematic.It'salittleoddto

populatethefirstrecordinthismannerandthentopopulateall

subsequentrecordsviaforms,processing,orothermethods.

Thepointisthatitprobablyisn'tpracticaltopopulatethefirst

recordinanymethodthatdiffersfromhowotherrecordsare

inserted.



Figure2-4.UsingaquerytofilltheAutoNumber

field,alongwithotherfields



Buthowcanyougetthefirstrecordtohavethedesired

startingAutoNumbervaluewithouthavingadummyrecordas

thefirstrecordinyourtable?Thetwististostillpopulatethe

firstrecordusingaquery,buttopopulatetheAutoNumberfield

withavalueofonelessthantherealstartingvalue.Youcan

thendeletethisrecord,andtheAutoNumberwillincrementas

sub-sequentrecordsareadded.Thismeansthefirstrealdata

record,enteredinwhateverwayyoursystemhandlesit,will

havethefirstvalueofchoice.TheAutoNumberwilljust

incrementfromthere,asexpected.

Intheexampleshowninthishack,thequeryneedstopopulate

thetablewithasinglerecord,inwhichavalueof99isgivento

theAutoNumber.Thatrecordisthendeleted(manuallyor

otherwise;itdoesn'tmatterhow).Whenthefirstrealdata

recordisadded,itwillhaveanAutoNumbervalueof100.



2.2.2.HackingtheHack

YoucanresetthevalueoftheAutoNumberfieldwheneveryou

want.Thisdoesn'tchangetheexistingrecords.Itletsnew

recordsbenumberedstartingfromanewinitialvalue.Youjust

runtheAppendquery,asshowninFigure2-2(adjustedas

discussedtohandleanyotherrequiredfields),butresetthe

valuetoonethatishigherthanthehighestvaluealreadyinthe

table.Forexample,ifthelastrecordenteredinthetablehasa

valueof220,resetthecounttosomethinghigher.Obviously,

youwouldskipthenextincrementalnumber;otherwise,there



wouldbenoreasontoreestablishwheretheincrementbegins.

Thisoffersaninterestingoptionformanagingyourdata.What

ifaseriesofrecordsinatablehassomeunobviousbutrelated

attribute?Forexample,youcanresettheAutoNumberfieldtoa

newstartingvalueatthestartofeachyear.Then,eachyear's

datawillbeeasytodistinguish.Forexample,allrecordsinthe

year2005mightliewithinthe5,0005,999range,allrecordsfor

2006withinthe6,0006,999range,andsoon.



Hack14.CopyDataBetweenTablesWithoutan

AppendQuery



UsePasteAppendtoeasilycopydataacrosstables.

AccessusersoftenuseanAppendquerytoappendrecordsfrom

onetabletoanother.Inaproductionenvironmentinwhichdata

isalwaysbeingshuffledaround,usingAppendqueriescan

becometedious.Eachtimeyoudesignone,youhavetomatch

thefieldsofthedestinationtablewiththefieldsofthesource

table.Thisiseasywhenthefieldshavethesamename,butit

takesmanualinterventionwhenthefieldnamesdiffer.

IfyouhavedesignedandsavedanAppendquerydefinition,and

thesourceanddestinationtablesneverchangeinnameor

structure,alliswellforyou.However,ifevenasingleextra

characterismisplacedorismissinginthefieldnames,the

queryeitherbombsorasksyoutofillinthevalueforthe

unidentifiablefield.Neitherisanoptionyoucanlivewith.

Howcanyoudealwiththeseaccidentswaitingtohappen?

Fortunately,youcancopydatabetweentablesinanotherway:

usePasteAppend.



2.3.1.AppendingAcrossTables

ApastemethoduniquetoAccess,PasteAppendappendsthe

contentsoftheclipboardtoadatabasetable.Thedatahasto

matchthetableinstructure,butitdoesnotneedtohave

matchingfieldnames.Thatrightthereimprovesonthetedious

dataentryinvolvedwhenusingthequerygrid.Tobefairto



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



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

Hack 13. Create an AutoNumber Field with a Custom Value

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

×