Types of Visual Basic Data Numeric Data Non Numeric Data.

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Working with Intrinsic Controls and ActiveX Controls
Chapter 3: Using Variables and Constants
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
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.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
VBA Modules, Functions, Variables, and Constants
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 04.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
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.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Apply Sub Procedures/Methods and User Defined Functions
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
IE 212: Computational Methods for Industrial Engineering
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module M Programming in Excel with VBA.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
Why to Create a Procedure
Using Arrays and File Handling
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
Lab 01 Forms in excel Tahani ALdweesh Insert form into your project. 2. Change form’s properties. 3. Put controls on the form. 4. Change controls’
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
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.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
ME 142 Engineering Computation I Using Subroutines Effectively.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Visual Basic Programming I 56:150 Information System Design.
Programming with Microsoft Visual Basic th Edition
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
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.
Understanding Visual Basic Fundamentals CHAPTER 13 Understanding Visual Basic Fundamentals.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
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.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
A variable is a name for a value stored in memory.
VBA - Excel VBA is Visual Basic for Applications
VBA - Excel VBA is Visual Basic for Applications
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 3: Using Variables and Constants
2. Understanding VB Variables
Variables and Arithmetic Operations
CIS16 Application Development Programming with Visual Basic
CS285 Introduction - Visual Basic
Data Types List Box Combo Box Checkbox Option Box Visual Basic 6.0
Introduction to Computer Programming IT-104
Presentation transcript:

Types of Visual Basic Data Numeric Data Non Numeric Data

Numeric Data

Non Numeric Data

Variables Variables are areas allocated by the computer memory to hold data. Each variable must be given a name. Rules :  It must be less than 255 characters  No spacing is allowed  It must not begin with a number  Period (.) is not permitted  Valid ones:txt123,My_Car  Invalid : 123txt,My.car

Declaring Variables In Visual Basic, one needs to declare the variables before using them by assigning names and data types. They are normally declared in the genaral section of the codes' windows using the Dim statement. Dim variableName as DataType Example : Dim password As String Dim yourName As String Dim firstnum As Integer Dim secondnum As Integer Dim total As Integer Dim doDate As Date Dim password As String, yourName As String, firstnum As Integer,

If data type is not specified, VB will automatically declares the variable as a Variant. For string declaration,Two ways of declaration- variable-length string- Dim a as String fixed-length string. Dim VariableName as String * n, where n defines the number of characters the string can hold. Example: Dim yourName as String * 10 yourName can holds no more than 10 Characters. Object variable refers to one of VB’s Objects Dim a as CommandButton

Ways of declaring Variables Implicit Declaration : It is not a must that a variable should be declared before using.VB encounters a variable,it assigns a default variable type and value. Explicit Declaration:Variable declared using Dim statement are called explicit declaration Dim a as integer Using Option Explicit:This forces the user to declare all the variables.This statement checks in the module for usage of any undeclared variables and reports an error to the user.. Option Explicit (Tools menu-> Options ->Editor tab in Options Dialog box select the option Required Variable Declarations.)

Assigning Values to Variables After declaring various variables using the Dim statements, values can be assigned to those variables. The general format of an assignment is Variable=Expression The variable can be a declared variable or a control property value. The expression could be a mathematical expression, a number, a string, a boolean value(true or false) and etc. The following are some examples: firstNumber=100 secondNumber=firstNumber-99 userName="John Lyan" userpass.Text = password Label1.Visible = True

Scope of Variable Variable is scoped to procedure level or module level. Procedure or local variable: Variable declared within the procedure using Dim and static keywords. Static : They are not reinitialized each time VB invokes a procedure.They retain their values even after the procedure has finished. Static a as integer Module level: These are variable by default available to all the procedure in that module.They are declared using Private or Public keyword Private a as integer Public keyword is used to make a module level variable to other modules.(Declare section)

In some cases, we need to add a suffix behind a literal so that VB can handle the calculation more accurately. & for long, ! for single, # for for currency, % for integer String with two double quotes and date,time with two #sign Ex : Dim a% NewValue$=“Pooja”

Operators Arithmetic operators : +,-,*,/ and \,Mod,^ / referred as floating point division \referred as integer division Comparison:>,,>=,<= Logical : Not,And, Or,Xor String Concatenation :&,+

Data Type Conversion Functions CboolBoolean CbyteByte CcurCurrency CdateDate CDblDouble CintInteger CLngLong CsngSingle CstrString CvarVariant CverrError

Control Flow and Loop Statements

If Statements If Condition then VB Statements End if If Condition Then Else End if If Condition then VB Statements Elseif Condition then VB Statements Else VB Statements End if

Select Case Select Case Expression Case Value1 VB Statement Block Case Value2 VB Statement Block Case Else VB Statement Block End Select

The Do Loop formats are Do While condition Entry Controlled VB statements Block Loop Do Exit Controlled VB statements Block Loop While condition Do Until condition VB statements Block Loop Do VB statements Block Loop Until condition

For counter =sValue to eValue Step stepValue VB Statement Block Next Counter For each element in group VB Statement statements Next [element] While Condition VB Statement block Wend

With can be used in arrays or Collections. With object VB statement Block end with E.g. With txtName.Text=“Anubhav”.Width=1000 End with.

To exit from for loop, Do loop, Sub Procedure/ function Exit Exit Do Exit For Exit Sub

ARRAYS: An array is a list of variables, all with the same data type and name. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item. Declaring Arrays We could use Public or Dim statement to declare an array just as the way we declare a single variable. The Public statement declares an array that can be used throughout an application while the Dim statement declare an array that could be used only in a local procedure. The general format to declare an array is as follow: Dim arrayName(subs) as dataType where subs indicates the last subscript in the array

There are two types of Arrays in Visual basic: Fixed size : Size remains same Dynamic Array : size can be changed at run time. Fixed arrays can be declared by giving the upper limit. Dim or Public length(9) as Integer(10 elements) Limit should be within a range of Long data type. Dim length ( 1 to 10) as integer Dynamic array can be initially declared and can add elements. Dim a() Actual no of elements can be allocated using Redim statement ReDim a(10) This Redim statement can appear only in a procedure ReDim a(4 to 10)

When we execute Redim statement the current data stored in the array is lost.Without losing the previous data,we have to use the Preserve keyword with the Redim statement. Redim Preserve a(Ubound(a)+1) Multidimensional Arrays: Dim marks(50,50) Dim marks(50 to 100,50 to 100) Any number of dimensions can be declared When we use preserve keyword only the upper limit of the last dimension in a multidimensional array can be changed.No other dimensions or the lower limit of the last dimension can be changed.

User Defined Data Types Variables of different datatype when combined a single variable to hold several related information is called user defined datatype.It is useful when we create a single variable to hold different pieces of information. The Type statement is used to create a user defined datatype in the general declaration section of a form or module. Private Type Courseinfo Coursename As string Startdate as Date Fees as Currency End Type

Variable can be declared using user defined datatype.Array on these type can also be declared. Dim a as Courseinfo Dim a(10) as Courseinfo To assign a value : a.coursename=“MCA” a(3).coursename=“bca” User defined datatype can be passed to procedures also. User defined datatype can only be private in form module while in standard module can be public or private Class module also it can be private or public

Constants Constants are named storage location in memory. Const statement is used to create a constant and it requires initialization also. Constants can be declared as public or private. They can be declared in any module. Public Const pie as single=3.14 In VB,predefined constants can be used anywhere in the code in the place of actual numeric values Ex: Form1.windowstate=2

Procedures Procedures are a group of related commands that perform certain tasks. VB programs can be broken into smaller logical components called procedures Benefits: Easier to debug Code can be reused in other modules with little or no modification Two type : Event,General

Difference between two Event procedures are invoked in response to keyboard,mouse click or system action.Maximum number of events a control can have is fixed.. Event procedures declaration as Private by Default General procedure is not executed unless explicitly invoked General procedures can be private or Public If the codes are repeating in event procedure,that can be placed in general procedure and can be invoked from event procedure. General Procedures are further classified as: Sub Function Property

Sub Procedure Sub procedure can be placed in standard,class and form modules.It do not return values.whenever the procedure is called the code between Sub and End Sub are executed. Syntax: [Private|public] [static] sub procedurename[(arg list)] statements end sub Calling the procedure: procedurename ar1,ar2,…… If there is no argument,only we have to write procedurename. Arguments are not enclosed in the parentheses.

Function Functions are like sub procedure except they return a value to the calling procedure. There are two types of functions: 1. Built-in 2. User Defined Functions A function returns a specific value to the calling block. Syntax: [Public|Private] Function functionName (Arg As dataType, ) [As dataType] End function Calling the function: Function call appears in an assignment statement variable name =functionname(arg1,arg2,..) Return type is an optional one.

Property Procedure It is used to create and manipulate custom properties. It is used to create read only properties for forms,standard modules and class modules. Three procedures: Let : Sets the value of a property get :Returns the value of a property set :sets the reference to an object

Event Procedure An event procedure is a procedure that contains the control’s actual name,an underscore(_) and the event name. Event procedures are stored in a form module are private by default. Maximum number of events a control can have is fixed. Syntax: Sub form_load() statements end sub

Built-in Functions Msgbox() : MsgBox is to produce a pop-up message box and prompt the user to click on a command button before he /she can continues. This message box format is as follows: yourMsg=MsgBox(Prompt, Style Value, Title) InputBox() An InputBox( ) function will display a message box where the user can enter a value or a message in the form of text. The format is myMessage=InputBox(Prompt, Title, default_text, x- position, y-position)

Passing Parameters Code in a procedure needs some information about the state of the program to do its job.These are passed to the procedure as arguments. Argument data type by default variant.Other datatypes can be declared. There are two types of passing arguments: 1. Pass by value 2.Pass by reference Pass By Value: In this only copy of a variable passed to it. Any change in the value caused by the procedure,it won’t affect the value and not the variable itself. ByVal must be used to indicate this. Sub Emp(Byval a as Integer) ………. End Sub

By Reference: Passing by reference is by default. Variable’s value can be permanently changed by the procedure to which it is passed by value. In this type a data type is specified for that parameter and when a value is passed,it must be of that type for that particular parameter. We can pass the expression also,rather than a data type for a parameter. Sub emp(a as integer) ………… end sub Optional : Argument can be defined as optional also. Optional argument is given a default value within the procedure. Optional argument should always be passed in the end. Sub emp(a as String, Optional straddress as string) ……. End Sub

Collections A Collection object is an ordered set of items that can be referred to as a unit. An object that contains a set of related objects. An object's position in the collection can change whenever a change occurs in the collection; therefore, the position of any specific object in the collection can vary. Similar to an array, stores related items We can access the element using key To use a collection we must declare a collection variable Dim a as New Collection new keyword is used to create a new collection Methods : Add,Remove,Item,Count

Add: It adds new Item into the collection. Collection.add value, key, before, after Remove : It removes an Item from a collection Collection.Remove index or key Item : Returns the value of an item in the collection collection.item(key or index) Count: It returns the number of items in a collection Collection.count

Whether the before or after argument is a string expression or numeric expression, it must refer to an existing member of the collection, or an error occurs. An error also occurs if a specified key duplicates the key for an existing member of the collection.

User interface SDI MDI Explorer Style Interface