Controlling Execution

Slides:



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

CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
ISOM3230 Business Applications Programming
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
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.
Proyek Queri-queri Lanjut dan Sub-subqueri. Variables dan Data.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Control Statements Making Decision in Your Program.
Selection Structures ; String Functions. If…Then…Else If condition Then [instructions when the condition is true] [Else [instructions when the condition.
5.05 Apply Looping Structures
BACS 287 Visual Basic String Manipulation. BACS 287 Visual Basic Strings In Visual Basic, a “string” is a series of text, numbers, and special characters.
Manipulation Masterclass By the VB Gods. In this masterclass, we will learn how to use some of the string manipulation function such as Len, Right, Left,
情報基礎 B Lecture 8 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information.
ENGR 112 Decision Structures.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
Arrays Group of variables that have a similar type
Chapter Six: Working With Arrays in Visual Basic.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Introduction to MIS1 Copyright © by Jerry Post Introduction to MIS Appendix 12 Visual Basic.
Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare.
Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright by Ronald P. Kessler, Ph.D.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Week 6.  Assessment 2  Do Loops  Custom Functions.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
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.
Types of Visual Basic Data Numeric Data Non Numeric Data.
Visual Basic Programming I 56:150 Information System Design.
CompMathBSc, English 5 October 2006 Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures.
Advanced Programming Strings Arrays Arguments Modulus.
CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
ME 142 Engineering Computation I Using Subroutines Effectively.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Scope Lifetime Modules Procedures. Scope? Where can your variables be seen? Where used? Where abused (reseting the value)? Local and Global = Private.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
By “FlyingBono” 2009_02By FlyingBono.  ASP code is embedded in HTML using tags.  A ASP scripting block always starts with.  Server scripts are executed.
UNIT 5 Lesson 15 Looping.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Visual Basic Variables
Single Dimensional Arrays
2. Understanding VB Variables
البرمجة بلغة فيجول بيسيك
مراحل كتابة البرنامج بلغة فيجول بيسك ستديو
Chapter 6 Variables What is VBScript?
البرمجة بلغة فيجول بيسك ستوديو
An Introduction to Java – Part I, language basics
Visual Basic 6 Programming.
Logical Operations In Matlab.
Visual Basic Programming
Quick Test Professional Training Part 1
For...Next Statements.
10.3 Procedures Function Procedures 07/06/2019.
Introduction to Computer Programming IT-104
Presentation transcript:

Controlling Execution

IF and only if.. One line IF IF with and without ENDIF IF with and without ELSE One line ELSEIF Multiple ELSEIFs and Select Case

IF and only if.. One line IF (without END IF) IF (condition) Then Exit Sub One seeming line IF IF (condition) Then _ Exit Sub IF with ENDIF IF (condition) Then END IF

IF and only if.. One line IF with ELSE Five line IF with ELSE IF (whatever) Then Yada ELSE YadaYada Five line IF with ELSE IF (whatever) Then Yada ELSE YadaYada END IF

IF and only if.. ELSE required alone on one line IF (whatever) Then Yada ELSE YadaYada END IF THEN at end of line required END IF required alone on one line

ElseIF and only if.. ElseIF is subordinate to the previous IF IF (whatever) Then Yada ElseIF (whatever) Then YadaYada ELSE YadaYadaYada END IF

Select Case vs ElseIF Select Case neat and maintainable Case formats very flexible Case 1,3,7 Case 2 to 8 Case “CA” to “NM” Case Is > 21

Select Case Example Select Case Security End Select Case "V" ' set properties for volunteers Case "S" ' set properties for supervisors Case "" ' exit sub End Select

Immediate If! IIF is a function IIF(condition, true return, false return) IIF(blnMale=True,”Male”,”Female”) Useful in queries, properties, division IIF(intN > 0, 300/intN, 0) ' to avoid an error msg IIF(PercentCorrect>=90,"A", _ IIf(PercentCorrect>=80,"B", _ IIf(PercentCorrect>=70,"C", _ IIf(PercentCorrect>=60,"D","F"))))

For Loops For intX = 1 to 10 Step 2 For Each varX in varDataSet Next required for these two For formats Exit For Restrictions on For Each… 1-dimension only Each control variable must be Variant

For Loops: Examples Dim varDataSet(0 to 49) as Variant Dim intX as Integer, intOdd as Integer Dim intSum as Integer Dim varItem as Variant, varSum as Variant ' sum the odd numbered elements (1,3,5,…) For intX = 1 to 49 Step 2 intOdd = intOdd + varData(intX) Next ' sum all elements (1,2,3,…) For Each varItem in varDataSet intSum = intSum + varItem

Do Loops Conditional logic after 1 pass through loop Do … Loop While Do … Loop Until Or, conditional logic before 1st pass through loop Do While … Loop Do Until … Loop Exit Do

Arrays Lists (states, income levels, schedules, etc) Private, Public, fixed or dynamic size Dim ccyIncomeLevel(0 to 5) As Currency

Which Array to Go? Option Base 1 or Option Base 0? Public strState(0 to 49) As String Public strState(1 to 50) As String Public strState(50) As String Above will be (0 to 49 or 1 to 50) depending on Option Base

strMonth(1) = "January" strMonth(2) = "February" strMonth(3) = "March" strMonth(4) = "April" strMonth(5) = "May" intInput = InputBox("Enter Month Number") MsgBox “You mean “& strMonth(intInput) & “?” Or MsgBox “You mean “& strMonth(intInput+1) & “?”

Flexible Array Dynamic Expands and contracts Dim ccyLevel() As Currency Expands and contracts Good for uncertain run-time conditions Can loose contents unless… Redim Preserve ccyLevel(7) Can erase dynamic arrays: Erase ccyLevel

Strings

Notes on strings String variables (Dim strName as String) String literals (txtName = "Jones") Mixing variables and literals Select * from tblRoster Where Name = "Smith" How to embed quotes around strName? "Select * from tblRoster Where Name =" & ???

How to embed single quotes ' If the name is a hard-coded literal Debug.Print "Name = 'Smith'" ' If the name is in a variable (very likely) Dim strName As String strName = "Smith" Debug.Print "Name = '" & strName & "'"

How to embed double quotes with literal strings ' When the name is a hard-coded literal Debug.Print "LastName = ""Smith""" 'Coding hints for the triple quotes above: 'step 1: LastName = "Smith" 'step 2: LastName = ""Smith"" 'step 3: "LastName = ""Smith"" "

How to embed double quotes using variables Dim strName As String strName = "Smith" Debug.Print "LastName = """ & strName & """" 'Coding hints for triple/quadruple quotes: 'step 1: "LastName = strName" 'step 2: "LastName = """ & strName " 'step 3: "LastName = """ & strName & """ "

How to embed double quotes as a variable Dim strName As String strName = "Nguyen" Dim strQuote As String strQuote = Chr(34) ' ACSII for double quote strName = strQuote & strName & strQuote Debug.Print "LastName = " & strName

String Functions mid(strHayStack, intStart, intLength) Returns string or part of a string instr(intStart, strHayStack, strNeedle) Returns position where needle is in haystack

Mid() MyString = "Mid Function Demo" FirstWord = Mid(MyString, 1, 3) ' Create text string. FirstWord = Mid(MyString, 1, 3) ' Returns "Mid". LastWord = Mid(MyString, 14, 4) ' Returns "Demo". MidWords = Mid(MyString, 5) ' Returns "Function Demo".

InStr() strHaystack ="XXpXXpXXPXXP“ strNeedle = "p" intWhere = Instr(1, strHaystack , "W") Returns 0 intWhere = Instr(4, strHaystack , strNeedle ) Returns 6

Other String Functions Left() and Right() ' args: string, no. of chars Ucase() and Lcase() ' one arg only Trim(), Rtrim() and Ltrim() ' one arg only Val(strA) ' change a string to a number Cstr(intX) ' change a number to a string Because Cstr(57) returns " 57" not "57" use Trim(Cstr(intX))