Automating Tasks with Visual Basic

Click here to load reader

download Automating Tasks with  Visual Basic

of 32

description

Automating Tasks with Visual Basic. Introduction. 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 . - PowerPoint PPT Presentation

Transcript of Automating Tasks with Visual Basic

INTRODUCTION ACCESS 2010

Automating Tasks with Visual BasicIntroductionWhen cant 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.2Understanding VBA TerminologySome basic VBA terminology:Statement: A word or combination of words that constitutes an instruction to be performed by the VBA engine.Procedure: A collection of VBA statements that are grouped together to perform a certain task. There are two types of procedures:Subroutines: perform a single task, no return.Functions: perform a task and then return a valueUnderstanding VBA TerminologyModule: Procedures live in modules. A module consists of one or more procedures Variable: is nothing more than a name applied to represent a data value. Variable is used to hold values such as customer names, dates, and numeric values manipulated by the VBA code.The Visual Basic EditorVisual Basic code is stored in database, but it need a different tool to view and edit it, called the Visual Basic editor.Open the Visual Basic editor:In the Access ribbon, choose Database Tools Macro Visual BasicThe Visual Basic EditorThe Visual Basic editor window is divided into three main regions.Project window: shows all the modules in your database. Properties window: which shows settings for the currently selected item in the Project window.Document window: Edit code region.Adding a New ModuleIn the Visual Basic editors menu, choose Insert Module.

Adding a New ModuleWhen add a new module, the Visual Basic editor automatically opens a code window that shows the contents of that module.Initially, a brand-new module has just one line of code:Option Compare DatabaseThis line is an instruction that tells Visual Basic how to handle operations that compare pieces of text.

Adding a New ModuleBefore write code you should add one more instruction to the top of code file:Option ExplicitThis instruction tells Visual Basic to use stricter error checking, which catches common typos when using variables.Simplest Possible Code RoutineEvery module are one or more subroutines. A subroutine is a named unit of code that performs a distinct task. Subroutines start with the word Sub followed by the name of the subroutine and end with the statement End SubExample:Sub MyCodeRoutine() MsgBox "Witness the power of my code."End SubModule typesAccess stores code in two places:Class modules: attached to the form or report, it executes when the form or report is opened, the class module does not appear in the modules section.Standard modules: Code can also be stored in modules section. Standard modules is accessible to all objects in your database.Responding to a Form EventOpen a form in Design mode.Turn off Control wizard button.Add a new button.Inthe Property Sheet, choose the Event tab, and then select the buttons On Clickevent Click the ellipsis () in the On Click Event boxSelect Code Builder and click OK