Danh mục

Automating Tasks with Visual Basic

Số trang: 32      Loại file: pptx      Dung lượng: 166.58 KB      Lượt xem: 14      Lượt tải: 0    
Jamona

Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.Example:Modify a whole batch of records at once.Perform complex calculations.Interact with other programs on computer. Write even more complicated validation routines that lock out bad data.
Nội dung trích xuất từ tài liệu:
Automating Tasks with Visual BasicAutomatingTaskswithVisualBasicIntroductionqWhencan’t findareadymademacroactionthat doesthejobyouwant,youcanuseVisualBasic code.qExample: − Modifyawholebatchofrecordsatonce. − Performcomplexcalculations. − Interactwithotherprogramsoncomputer. − Write even more complicated validation routinesthatlockoutbaddata.UnderstandingVBATerminologyqSomebasicVBAterminology: − Statement: A word or combination of words thatconstitutesaninstructiontobeperformed bytheVBAengine. − Procedure: A collection of VBA statements thataregroupedtogethertoperformacertain task.Therearetwotypesofprocedures: Subroutines: perform a single task, no § return. Functions: performataskandthenreturna §UnderstandingVBATerminology− Module:Proceduresliveinmodules.Amodule consistsofoneormoreprocedures− Variable:isnothingmorethananameapplied to represent a data value. Variable is used to hold values such as customer names, dates, and numeric values manipulated by the VBA code.TheVisualBasicEditorqVisual Basic code is stored in database, but it need a different tool to view and edit it, called theVisualBasiceditor.qOpentheVisualBasiceditor: In the Access ribbon, choose Database § Tools➝Macro➝VisualBasicTheVisualBasicEditorqThe Visual Basic editor window is divided into threemainregions. − Projectwindow:showsallthemodulesinyour database. − Properties window: which shows settings for the currently selected item in the Project window. − Documentwindow:Editcoderegion.AddingaNewModuleqIntheVisualBasiceditor’smenu,chooseInsert ➝Module.AddingaNewModuleqWhenaddanewmodule,theVisualBasiceditor automatically opens a code window that shows thecontentsofthatmodule.qInitially,abrandnewmodulehasjustonelineof code: OptionCompareDatabase − ThislineisaninstructionthattellsVisualBasic howtohandleoperationsthatcomparepieces oftext.AddingaNewModuleqBefore write code you should add one more instructiontothetopofcodefile: OptionExplicitqThis instruction tells Visual Basic to use stricter error checking, which catches common typos whenusingvariables.SimplestPossibleCodeRoutineqEvery module are one or more subroutines. A subroutineisanamedunitofcodethatperforms adistincttask.qSubroutinesstartwiththeword Sub followedby the name of the subroutine and end with the statementEndSubqExample: SubMyCodeRoutine() MsgBoxWitnessthepowerofmycode. EndSubModuletypesqAccessstorescodeintwoplaces: − Classmodules:attachedtotheformorreport, itexecuteswhentheformorreportisopened, the class module does not appear in the modulessection. − Standard modules: Code can also be stored in modules section. Standard modules is accessibletoallobjectsinyourdatabase.RespondingtoaFormEventqOpenaforminDesignmode.qTurnoffControlwizardbutton.qAddanewbutton.qIn the Property Sheet, choose the Event tab, andthenselectthebutton’sOnClick eventqClicktheellipsis(…)intheOnClickEventboxqSelectCodeBuilderandclickOK

Tài liệu được xem nhiều: