Vous êtes sur la page 1sur 8

5/16/2011

1
CreatingandAccessingFiles
CIS1005 ComputerProgramming
F d t l Fundamentals
UniversityofMalta Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering.
1
Summary
Introduction
CreatingafilefromwithinaVBAprocedure
ReaddatafromafileintoaVBAprocedure
CreateafilewithExcelthatcanbereadby
VBA
Allowtheusertobrowseforafilefor
accessingandsaving
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
2
5/16/2011
2
Introduction
Sofarinputdatawasimplementedusing:
Range(a1).Select g ( )
x=ActiveCell.Value
Outputdatawasimplementedusing:
Range(a1).Select
ActiveCell.Value =x
Inadditionmessagesandinputboxeswereusedto
outputsinglemessagesandinputsinglevalues
Msgbox Theansweris&x
Thesemethodscanbequitelimitingwhenlarge
quantitiesofdataareinvolvedorwhendatais
availableinafile.
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
3
Sequentialfiles
Areusedto
Write data from program into a file Writedatafromprogramintoafile
Inputtingdatafromfilebackintoprogram
Tooutputtofile:
DimaAsDouble,bAsDoubleStudentName AsString
A=5
B=6
StudentName =IamEngineer
Openc:\test.datForOutputAs#1
Write#1,abStudentName
Close#1
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
4
5/16/2011
3
Openstatements
TheOpenstatementisneededtocreatethe
fil It l file.Italso
Specifiesthenameofthefileasc:\test.dat
Thisthenamethatisreferencedondisk
Fullpathmustbeused
Thatdatawillbeoutputfromprogramtofile
Itassignsanumber(filehandle)tothefile(#1)
Thisisthefilereferencewiththeprogram
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
5
Write&Closestatements
TheWritestatement isusedtoactuallywrite
th i f ti i t th fil theinformationintothefile
TheClosestatement isusedtoclosethefile
Inbothcasesthefileisreferencedbyits
handle.Canbeveryusefulifanumberoffiles
are opened simultaneously areopenedsimultaneously.
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
6
5/16/2011
4
Verification
Aftertheprogramisexecuted,afilecalled
test dat containing the numbers 5 and 6 and test.dat containingthenumbers5and6and
thestringconstantIamEngineershould
resideonthedisk
Filemaybeopenedusingatexteditorto
verifythattheinformationhasbeenwritten
to the file tothefile
Commasareusedtodelimit(separate)the
values
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
7
Readingdatafromfile
Toreaddatafromafile
bl b bl d DimaAsDouble,bAsDouble,StudentName AsString
Openc:\test.datForInputAs#2
Input#2,a,b,StudentName
Close#2
Msgbox a&&b&&StudentName
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
8
5/16/2011
5
Readfromfile
InthiscaseOpenstatementspecifiesthatdata
will be input from the file into the program willbeinputfromthefileintotheprogram
Anewnumberwasusedtoreferencefile
Filereferencecouldbeanynumberbetween1to
511
AnInput#statementwasusedtoreadthefile p
Afterprogramexecutes,themessageboxshows
56IamEngineer
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
9
Thegeneralsyntax...1
Openpathname Formode As[#]filenumber
Wh Where
Pathnamespecifiesthecompletepath&filename
Modeisakeywordspecifyingthefilemode
Append
Binary,
Input Input,
Output
Random(defaultmode)
Filenumber isavalidfilenumberbetween1&511
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
10
5/16/2011
6
Thegeneralsyntax...2
Write#filenumber,[outputlist]
Where outputlist is one or more comma delimited Whereoutputlist isoneormorecommadelimited
numericorstringexpressionstowritetoafile
Input#filenumber,varlist
Wherevarlist isacommadelimitedlistofvariables
thatareassignedvaluesreadfromthefile
varlist cannotbeanarray
Elementsofanarrayoruserdefinedtypesmaybe
used
Close#ilenumber closesthefile
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
11
EndoffileEOF
Whenreadingafilebyloopingthroughits
t t ith t k i it l th th EOF contents,withoutknowingitslength,theEOF
statementisusedtodetecttheendofthefile
EOF(filenumber)
ReturnsanintegercontainingtheBooleanvalue
True(1)whentheendoffileisreached.
ReturnsFalse(0)aslongastheendoffilehasnot
beenreached
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
12
5/16/2011
7
EOFExample
Dimnd AsInteger
Open datapairs dat For Input As #1 Open datapairs.dat ForInputAs#1
nd =1
Do
IfEOF(1)ThenExitDo
nd =nd +1
I t #1 T ( d) D ( d) Input#1,Temp(nd),Dens(nd)
Loop
Close#1
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
13
Otherfileoperations
Excelsbuiltindialogueboxesmayalsobe
d used
GetOpenFilename method
Thisprovidesaninteractivemethodtoselecta
filenamefromdiskratherhardcodethefilenameinthe
program.
Method does not open the file it just provides the full Methoddoesnotopenthefile,itjustprovidesthefull
pathandfilename.
GetSaveAsFilename method
Thismethodoperatesthesamewayasabove
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
14
5/16/2011
8
Summary
Introduction
CreatingafilefromwithinaVBAprocedure
ReaddatafromafileintoaVBAprocedure
CreateafilewithExcelthatcanbereadby
VBA
Allowtheusertobrowseforafilefor
accessingandsaving
UniversityofMalta
Oct2010
Ing.SaviourM.Baldacchino
ICTSystemsEngineering
15
CreatingandAccessingFiles
Discussion
UniversityofMalta Oct2009
Ing.SaviourM.Baldacchino
ICTSystemsEngineering.
16

Vous aimerez peut-être aussi