University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 SESSION 16 VBA example.

Slides:



Advertisements
Similar presentations
1 Overview Introduction to VBA The Visual Basic Editor (VBE) –First Program Getting Started with VBA –Subroutines –Declaring variables –Input boxes and.
Advertisements

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
How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Introduction to Visual Basic Chulantha Kulasekere.
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Chapter 9 Macros, Navigation Forms, PivotTables, and PivotCharts
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Lab 9 – User Forms Design. User Forms What are user forms? –Known as dialog boxes –Major ways for getting user input An example of using user forms: Monthly.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
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.
Enhancing User Interaction Through Programming
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
Learning about Expert Systems through computer programming.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 11 Committed to Shaping the Next Generation of IT Experts. Chapter 10 Customizing a Database.
Outline Software and Programming Program Structure Tools for Designing Software Programming Languages Introduction to Visual Basic (VBA)
Programming Test #1 Solutions. Multiple Choice 1. B) the grammar of the coding language 2. C) String 3. A) Single 4. C) 2Burgers4Me 5. B) Design Time.
New Project in Visual Basic Please use speaker notes for additional information!
Fundamentals of GUI Programming. Objectives: At the end of the session, you should be able to: describe the guidelines that are used for creating user-friendly.
VISUAL BASIC Event Driven, non-procedural Place controls, from toolbox, onto form command, label, time, image Customise controls Write code for command.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
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.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Dialog Boxes – Day 1 Built-in Dialog Boxes Input Box Message Box Custom Dialog Boxes – User Forms.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Graphical User Interface You will be used to using programs that have a graphical user interface (GUI). So far you have been writing programs that have.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
1 Advanced Computer Programming Lab Calculator Project.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
You should unzip and download the beginning programs.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Pay Example (PFirst98) Please use speaker notes for additional information!
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
Calculator Program Explained by Arafa Hamed. First Designing The Interface Ask yourself how many places are there that will be used to input numbers?
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Sorry, the pen lost control. Anyway, you need to download both the.vbp and the.frm for each project. I looked at PrCalc, I did not look at inventory.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Adding Code to the Option Button. Open VB 1.Double click the Calculate button and select General from the Object list box. 2.Add the following code to.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Course ILT Using complex selection structures Unit objectives Include radio buttons and check boxes in an interface, create and call a user- defined Sub.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
The Advantage Series ©2005 The McGraw-Hill Companies, Inc. All rights reserved Chapter 12 Introducing Visual Basic for Applications Microsoft Office Excel.
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Exploring Microsoft Office Access 2007
Department Array in Visual Basic
Please use speaker notes for additional information!
Introduction to Programming
Custom Forms with VBA in Excel In-Class Exercise #11
Presentation transcript:

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 SESSION 16 VBA example

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Exercise 10.1 You are going to use VBA to create a simple calculator to multiply 2 numbers and display the answer.

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Outcomes To further understand examples of VBA code in Microsoft Access

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Setting up the objects Open a new database and call it vba.mdb Create a new form in design view Add a label with a caption Easy Calculator Add 2 textboxes, one called txtFirst and the second txtSecond, with captions of First number, Second number respectively.

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Add a button Add a command button called cmdCalculate Add a text box called txtDisplay Open the form in code view Select cmdCalculate and onclick

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Assign 3 variables, firstNo,secondNo and Answer as single Set the focus to the txtFirst Assign firstNo to the txtFirst.text firstNo=val(txtFirst.text) (val changes text input to a number)

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Repeat for txtSecond Write code for the answer Assign Answer to txtDisplay.text Test your code by opening the object view the form view

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 The number calculator: form view The number calculator: form view

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 To enter VBA code go to View Code. The 2 drop down boxes at the top contain the objects (LHS) and their associated events (RHS)

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 The code view object event

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Code for number exercise Option Explicit Private Sub cmdCalculate_click() Dim FirstNo As Single Dim SecondNo As Single Dim Answer As Single txtFirst.SetFocus FirstNo = Val(txtFirst.Text) This forces the program to check for inconsistencies This is the code for a procedure (set of functional statements) to perform a calculation when a button is pressed. In Access VBA you need to set the focus on the object

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 'decision construct to check that a positive no is entered If FirstNo < 0 Then txtFirst.Text = "" MsgBox "You must enter a positive no" txtFirst.SetFocus End If FirstNo = Val(txtFirst.Text) End Sub

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Code for the above Exercise: this must be typed into the design view of the NumberCalculator form (view code) Option Compare Database Private Sub cmdCalculate_click() Dim FirstNo As Single Dim SecondNo As Single Dim Answer As Single txtFirst.SetFocus ‘This appears by default when you open the code screen – it forms part of the General Declarations area. FirstNo = Val(txtFirst.Text) 'decision construct to check that a positive no is entered If FirstNo < 0 Then txtFirst.Text = "" MsgBox "You must enter a positive no" txtFirst.SetFocus End If

University of Sunderland CIF 104 Fundamentals of DatabasesUnit 16 Code continued FirstNo = Val(txtFirst.Text) txtSecond.SetFocus SecondNo = Val(txtSecond.Text) If SecondNo < 0 Then txtSecond.Text = "" MsgBox "You must enter a positive no" txtSecond.SetFocus End If Answer = FirstNo * SecondNo txtOutput.SetFocus txtOutput = Answer End Sub