MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.

Slides:



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

ISOM3230 Business Applications Programming
AE6382 VBA - Excel l VBA is Visual Basic for Applications l The goal is to demonstrate how VBA can be used to leverage the power of Excel u VBA syntax.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Programming in Visual Basic
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
Control Statements Making Decision in Your Program.
Introduction to Computing Dr. Nadeem A Khan. Lecture 11.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File 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.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
General Programming Introduction to Computing Science and Programming I.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare.
EIW - ASP Introduction1 Active Server Pages VBScript.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Applications Development
Chapter 9 Macros And Visual Basic For Applications.
MS Visual Basic 6 Walter Milner. VB 6 0 Introduction –background to VB, A hello World program 1 Core language 1 –Projects, data types, variables, forms,
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
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!!
ME 142 Engineering Computation I Using Subroutines Effectively.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Chapter-1 Introduction to Visual Basic GUI- A GUI is a graphical (rather than purely textual) user interface to a computer. The term came into existence.
Types of Visual Basic Data Numeric Data Non Numeric Data.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Visual Basic Programming I 56:150 Information System Design.
ME 142 Engineering Computation I Using Subroutines Effectively.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
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?
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
A variable is a name for a value stored in memory.
Programming in visual basic .net Visual Basic Building Blocks
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Department Array in Visual Basic
Introduction to Computer Programming IT-104
Presentation transcript:

MS Visual Basic Applications Walter Milner

Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines what happens In GUI, the user triggers what application does (mostly) Event examples are key press, mouse move, timer timeouts Correspond to native Windows Messages (next slide) Event handler = a subroutine which will execute when that event happens

Variables Dim x as Integer Dim x,y as Integer NO! Is case sensitive (kind of) Variable naming conventions Microsoft Simonyi Hungarian Reddick House rules Assignment statement – x = 4 Option Explicit YES! Constants – Private Const MyInt As Integer = 5

Comments, line continuation and hex Comments start with an apostrophe 'and run to the end of the line A very long statement can use a_ to continue onto the next line Hex constants written like &HFF0012

Data types Integer Long Single Double Currency Byte unsigned String Boolean Date Object Variant NO!

Data type conversion Conversion function Converts an expression to CboolBoolean CbyteByte CcurCurrency CdateDate CDblDouble CintInteger CLngLong CSngSingle CStrString CvarVariant CVErrError DIM x as integer x = Cint("10")

Controls Private Sub CommandButton1_Click() Dim x As Integer Dim y As Integer Dim z As Integer x = TextBox1.Value y = TextBox2.Value z = x + y Label1.Caption = z End Sub Copy this Correct it Add other buttons

VB Core II Conditional statements Exception handling Loops Arrays Debugging

if statements If c > 5 Then x = 1 y = 3 End If If c > 5 Then x = 1: y = 3 If c > 5 Then x = 1 y = 3 Else z = 7 End If If c > 5 Then x = 1 y = 3 ElseIf c = 4 Then z = 7 Else x = 9 End If

select Dim Number Number = 8 ' Initialize variable. Select Case Number ' Evaluate Number. Case 1 To 5 ' Number between 1 and 5, inclusive. x=4 Case 6, 7, 8 ' Number between 6 and 8. x=5 Case 9 To 10 ' Number is 9 or 10. x=6 Case Else ' Other values. x=7 End Select

Error and exception handling exception = problem event at run-time usually related to I/O eg file not found, server connection lost, invalid user input not a programming bug VB calls exception errors (Unlike Java) VB does not force exception handling – but should do

Error handlers – example - invalid numbers Private Sub Command1_Click() Dim num1 As Integer Dim num2 As Integer Dim result As Integer On Error GoTo myErrorHandler num1 = Textbox1.Text num2 = Textbox2.Text result = num1 + num2 Label1.Caption = result Exit Sub myErrorHandler: If Err.Number = 13 Then MsgBox ("Please enter a valid number") Else MsgBox (Err.Description) End If Resume Next End Sub Exercise Try this out in the calculator program Then deal with divide by zero (11)

For next loops Dim x as Integer, total As Integer total = 0 For x = 1 To 5 total = total + x Next Dim x as Integer, total As Integer total = 0 For x = 1 To 5 Step 2 total = total + x Next

Debugging

Exercises – Use the debugger to watch.. a for next loop looping from 1 to 5 outputting the multiples of 3 from 99 to 3 adding up the odd numbers between 1 and 9

Other loops Dim c As Integer c = 1 Do While c < 5 c = c + 1 Loop Dim c As Integer c = 1 Do Until c >4 c = c + 1 Loop Dim c As Integer c = 1 Do c = c + 1 Loop While c < 5 Dim c As Integer, x as integer c = 1 x=2 Do c = c + 1 Loop Until c>4 And x<>3

Arrays (fixed size) Dim x(100) As Integer Dim i As Integer For i = 0 To 100 x(i) = 99 Next

Exercise – use the debugger – check each stage Fill an array with 25 random integers in the range 1 to 100 (Rnd() gives a random single between 0 and 1 ) Find the total Find the largest Search the array for a value given in cell(1,1)

Arrays (2 d) Dim x(1 To 3, 1 To 3) As Integer Dim i as integer, j As Integer For i = 1 To 3 For j = 1 To 3 x(i, j) = 99 Next

Dynamic arrays Dim x() As Integer ReDim x(5) Dim i, j As Integer For i = 1 To 5 x(i) = 99 Next ReDim Preserve x(10) For i = 6 To 10 x(i) = 100 Next

Exercise Use the =RAND() function to fill a column of a sheet with 20 random numbers Then set up a button and write VB code which will – read those 20 numbers into an array sort the array into increasing order (bubblesort) display the numbers in the next column of the spreadsheet