VBA – Visual Basic for Applications Week 20 - Tutorial.

Slides:



Advertisements
Similar presentations
Integrated Business Applications with Databases (D3) Jenny Pedler
Advertisements

1 Overview Introduction to VBA The Visual Basic Editor (VBE) –First Program Getting Started with VBA –Subroutines –Declaring variables –Input boxes and.
Using VB with MS Applications R. Juhl, Delta College.
Stored procedures and views You can see definitions for stored procedures and views in the demo databases but you can’t change them. For views, expand.
Using Macros and Visual Basic for Applications (VBA) with Excel
Tutorial 8: Developing an Excel Application
XP New Perspectives on Microsoft Excel 2003, Second Edition- Tutorial 8 1 Microsoft Office Excel 2003 Tutorial 8 – Developing an Excel Application.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
The Web Warrior Guide to Web Design Technologies
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA By Robert T. Grauer Maryann Barber.
Automating Tasks With Macros
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Using the Visual Basic Editor Visual Basic for Applications 1.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
05/09/ Introducing Visual Basic Sequence Programming.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
XP Chapter 7 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Enhancing User Interaction Through Programming Chapter.
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.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Enhancing User Interaction Through Programming
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Using Visual Basic for Applications (VBA) – Project 8.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Committed to Shaping the Next Generation of IT Experts. Chapter 10 Customizing a Database.
McGraw-Hill/Irwin ©2009 The McGraw-Hill Companies, All Rights Reserved Business Driven Information Systems 2e Plug-In T6: Basic Skills and Tools Using.
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
University of Sunderland CIF 102/FIF102 Fundamentals of DatabasesUnit 15 Programming in Microsoft Access using VBA Using VBA to add functionality.
Chapter Two Creating a First Project in Visual Basic.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA.
Microsoft Access 2013 ®® Tutorial 10 Automating Tasks with Macros.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
1 MIS309 Database Systems Introduction to Microsoft Access.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 10 1 Microsoft Office Access 2003 Tutorial 10 – Automating Tasks With Macros.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Introduction to Excel VBA UNC Charlotte CPE/PDH Series December 17, 2009.
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Access 2010 by Robert Grauer, Keith Mast, and Mary Anne.
CHAPTER 6 LESSON B Creating Custom Forms. Lesson B Objectives  Suppress default system messages  Create alerts and messages to provide system feedback.
MIS333k(Poynor) Chapter 2. Intro to VBA Events Where to plug in your programs?
31/01/ Selection If selection construct.
Controlling Program Flow with Decision Structures.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
IE 8580 Module 4: DIY Monte Carlo Simulation
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

VBA – Visual Basic for Applications Week 20 - Tutorial

Objectives At the close of this tutorial you should understand Objects, Properties, Methods and Events Event driven programming Basic VBA programming syntax Functions and Procedures

What is VBA VBA is a programming language It can be used in all major Microsoft Office Applications VBA allows developers to create their own functions and procedures to enhance bespoke databases or spreadsheets It is more powerful and flexible than Access macros

Objects Properties Methods and Events Before we can create any VB code we must understand Objects Properties, Methods and Events Objects – can be anything in your database such as a form, textbox, combo box – the database is also an object. Other applications such as Excel are also objects

Properties Objects can have Properties that describe their characteristics A Form has a Caption property, a textbox has a Font property We can set the fonts attributes by setting some of the textboxes properties such as Bold or Italic

Methods Object also have Methods, which allow actions to be done to the objects A Recordset object has an AddNew method that allows the addition of a new record – it also has MoveFirst, Delete, Insert etc.

Events Closely related to methods are Events VBA is an Event driven programming language A Event is a specific action that occurs on or with an Object VBA is totally dependent on events being initiated or occurring

Events cont. The Click event of a button is probably the most commonly initiated event When you click a command button on a form, the code within the click event is executed All controls that are placed on a form have events – the form itself has its own events

Modules All the code for a VBA program is contained in Modules VBA has Class and Standard Modules Each Form and Report has its own Class module that contains code that is specific to that form or report A standard module contains generic code that is not specific to any particular object and can be deployed anywhere within a program

Viewing a Form Module To view a Form module open the form in Design View and click on the code icon on the toolbar

The Form Class Module Code Window The code module is displayed

Form Class Module If we click on the ‘General’ dropdown list we can see a list of Objects (controls) that have been placed on the form. It also lists the Form itself

Form Class Module If we select ‘Form’ from the list, the ‘Declarations’ dropdown list displays ‘Load’ – this refers to the Form Load Event

Form Class Module If we click on the declaration drop down list we can view all the other Events available to the form When you select an Event, a code template is created in the code window – this is where we add the code for that Event Code goes in here

Coding an Event To access both the properties and the controls on the form we use the reserved word Me This refers to the form itself To access the properties and controls we use the dot (.) notation For example, to access the Form’s Caption property we would use Me.Caption

Coding an Event To set the Caption property we can enter a name in speech marks and click save, and no matter what name exists in the form name property, the Load Event code will over-ride it to display the name defined in the procedure

Procedures and Functions Apart from Event procedures developers can write their own procedures and functions or more correctly Sub Procedures and Function Procedures Sub Procedures perform actions and can be run directly or in response to an event Function Procedures return a single value of any data type i.e. String, Integer, Date…

Modifying Existing Event Procedures Access can satisfy most user requirements, however, by providing your own VBA code you can create individual bespoke Access database applications The code that Access provides for a control’s event procedures can be rewritten to suit your own application and confirmation and error messages can be overridden and replaced by more suitable messages

Modifying Existing Event Procedures When you create an input form using the wizard, Access does not provide a Delete option, only record navigation and add new record options Navigation buttons Add New button

Adding Delete Button To add a delete option place a command button on the form and using the wizard set the button to Delete Record When the button is clicked an internally generated message is displayed – if the user clicks ‘No’ then another message is displayed Each message has the Microsoft Office Access caption

Customising Messages To disable Accesses error messages select Tools->Options, select the edit/Find tab, uncheck Record changes and click Apply Record Changes

Customising Messages We now need to edit the Access generated code of the Delete button In Design mode click on the delete button to display the properties window Go to the ‘Click’ Event and click on the ellipses – this will take you to the click event procedure code template – see next slide

Customising Messages The code prefixed with DoCmd is the code that deletes the record – all the rest is Access generated error trapping, which we want to keep

Message Boxes To display messages to the end user when the program is running VBA uses message boxes (MsgBox) They can be deployed in various ways depending on the requirements If they are to display only messages that inform users of a particular state or condition then the format can be either of the following – see next slide

Message Boxes MsgBox”Record Saved” – which will return only the text in an OK only message box with the Access caption

Message Boxes MsgBox "Record Saved", vbInformation – which displays the same as previously shown but with an exclamation mark

Message Boxes MsgBox "Record Saved", vbInformation, "Bilbos Products” – As previous but with user defined caption

Message Boxes We can also get the message boxes to respond to user input i.e. Delete Record Yes/No/Cancel To do this we must use the MsgBox as a function i.e. return a single value When a message box uses the vbYesNoCancel function it returns an integer value dependant on whether the user has clicked Yes, No or Cancel

Message Boxes Yes = 6 No = 7 Cancel = 2 If we give this value to a variable we can then evaluate the value using an ‘If’ statement in the code and display other messages or performing other tasks dependant on the users choice

Evaluating Message Boxes The format for such a message is:

Evaluating Message Boxes We declare an integer variable to contain the response using the ‘Dim’ VB keyword The message box parameters must be encapsulated in parentheses It is then passed to the variable Dim response As Integer response = MsgBox("Delete Record?", vbYesNoCancel + vbQuestion, "Bilbos Products")

Evaluating Message Boxes The variable is then evaluated using the If statement – notice the other message boxes used to inform the user whether the record has been deleted or not If response = 6 Then DoCmd.DoMenuItem acFormBar, acEditMenu, 8,, acMenuVer70 DoCmd.DoMenuItem acFormBar, acEditMenu, 6,, acMenuVer70 MsgBox "Record Deleted", vbInformation + vbOKOnly, "Bilbos Products" Else MsgBox "Record Not Deleted", vbInformation + vbOKOnly, "Bilbos Products" End If

Evaluating Message Boxes You can also use the vbYes function in place of the number i.e. 6, to make the code more readable For more information on message boxes read pages 12 – 14 of the VBA trainer

Custom Event Procedures If a user accidentally changes data in a form and moves to the next record the data is saved We can prevent this by adding code to one of the many form events available The BeforeUpdate event is invoked if any changes have been made to the data

BeforeUpdate If we add the following code to the event and the user changes any data, clicks any of navigation buttons and then selects Yes, the data is saved otherwise the save is undone Private Sub Form_BeforeUpdate(Cancel As Integer) Dim response As Integer response = MsgBox("Save Details", vbYesNo, "Save") If response = vbNo Then Me.Undo Cancel = False End If End Sub

BeforeUpdate If we change Cancel to True, then the current record remains displayed when the user selects No If we add a Save button to the form then the BeforeUpdate event is called whenever data is changed and the Save button is clicked – see next slide

BeforeUpdate

Inbuilt Functions VBA has many inbuilt functions i.e. IsNumeric, IsDate etc that can be used by the developer when validating user entry For example, if we had a textbox named txtPurchaseDate we could trap any erroneous input by the user by coding an If statement in an event i.e. If IsDate(txtPurchaseDate) then… The IsDate function is a Boolean function that returns either True or False

User Defined Functions We can create our own functions and place them in a Standard Module i.e. a module that is not tied to any form or report. They contain generic code to be used anywhere in the program To add a standard module to the program click Insert->Module

User Defined Functions Assuming that the discount in the form below was calculated from the purchase date and date paid i.e. paid before 30 days then 20% discount – days 10% discount > 60 days no discount, we could code the calculation in the BeforeUpdate event

User Defined Functions However, we may need this calculation elsewhere in the program so a separate calculation function in the module would be better – to do this, click on the code icon and in the project explorer, double click the Module icon Project Explorer Module Icon

User Defined Functions To add a new function template to the module select Insert->Procedure Give the function a name. Set the Type as Function Set the Scope as Public – we need to use this in more than one place

The code below uses the DateDiff inbuilt function that has 3 parameters and returns, in this instance, an integer value based on the difference in days between the 2 nd two parameters given as parameters to the function The getDiscount function returns a Single value Given Parameters Return Value User Defined Functions

The function is useless unless it is called from within an event in this case the BeforeUpdate event Private Sub Form_BeforeUpdate(Cancel As Integer) Dim response As Integer response = MsgBox("Save Details", vbYesNo, "Save") If response = vbNo Then Me.Undo Cancel = True Else Me!txtDiscount = Null If Not Me!datePaid Then Me!txtDiscount = getDiscount(datePaid) End If End Sub

User Defined Functions We could also place the code in the AfterUpate event of the datePaid text box so that the txtDiscount is calculated when the datePaid textbox loses the focus Private Sub datePaid_AfterUpdate() Me!txtDiscount = Null If Not Me!datePaied Then Me!txtDiscount = getDiscount(datePaid) End If End Sub

User Defined Sub Procedures However, we are repeating code, which we want to avoid To do this we select the Form Module an create a procedure by selecting Insert- >Procedure Make the procedure private as it is only going to be used on this form Select Private

User Defined Sub Procedures Add the following code Private Sub calculateDiscount() Me!txtDiscount = Null If Not Me!datePaid Then Me!txtDiscount = getDiscount(PurchaseDate, datePaid) End If End Sub

User Defined Sub Procedures Call the code from within each event Private Sub Form_BeforeUpdate(Cancel As Integer) Dim response As Integer response = MsgBox("Save Details", vbYesNo, "Save") If response = vbNo Then Me.Undo Cancel = True Else Call calculateDiscount End If End Sub Private Sub datePaid_AfterUpdate() Call calculateDiscount End Sub

User Defined Sub Procedures See how much neater the code is – this is called code re-use However, there is another calculation to be done on the form –the total price based on the number of products, the product price and the discount i.e. total=productPrice*noOfProducts*discount

Finishing it off We need another function in our Standard Module – getTotalPrice that returns a currency value Public Function getTotalPrice(discount As Single, price As Currency, noProducts) As Currency Dim total As Currency If Not discount And price <> 0 And noProducts <> 0 Then total = (discount * (price * noProducts)) + price * noProducts ElseIf price <> 0 And noProducts <> 0 Then total = price * noProducts Else total = 0 End If getTotalPrice = total End Function

Finishing it off If we add the new function to our calculateDiscount procedure and change the name of the procedure to a more suitable name i.e. calculatePrice, we can call this procedure from other events Task – try adding a similar procedure to the AfterUpdate event of some of the input texboxes in a program of your own Private Sub calculatePrice() Me!txtDiscount = Null Me!txtTotal = Null If Not Me!datePaid Then Me!txtDiscount = getDiscount(PurchaseDate, datePaid) Me!txtTotal = Format((getTotalPrice(Me!txtDiscount, Me!ProductPrice, Me!noOfProducts)), "Currency") End If End Sub

Finishing it off This is my final code for the form on the right Private Sub datePayed_AfterUpdate() Call calculatePrice End Sub Private Sub Form_AfterUpdate() Call calculatePrice End Sub Private Sub Form_BeforeUpdate(Cancel As Integer) Dim response As Integer response = MsgBox("Save Details", vbYesNo, "Save") If response = vbNo Then Me.Undo Cancel = True Else Call calculatePrice End If End Sub Private Sub Form_Current() Call calculatePrice End Sub Private Sub noOfProducts_AfterUpdate() Call calculatePrice End Sub

Summary In this tutorial we have covered: Objects, Methods, Properties and Events Modules – Class and Standard Modifying Event Procedures Adding user defined messages Adding User Defined Function Adding User Defined Sub Procedures

To cover VBA in any depth would require a whole module – at least This tutorial has hopefully given you some ideas in how to customise Access programs For a more detailed coverage see references Summary cont.

References “Getting Started with MS VB for Applications (VBA) “ - Available from your lecturer or from one of it’s creators, Mary Spence “Further Uses of MS VBA” – same authors as above Any ‘Good’ book – especially Microsoft publications