Control Arrays, Records, and Record Arrays in V.B. Week 10.

Slides:



Advertisements
Similar presentations
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Advertisements

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.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Chapter 7: Sub and Function Procedures
1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Arrays in Visual Basic Week 9 CM What is an array ? An array is a data structure that enables us to store a list of values that can be thought.
Programming with Collections Collections in Java Using Arrays Week 9.
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
Introduction to Arrays Chapter 7 Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread.
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.
Lec6 P 1 CP2030 Visual Basic For C++ programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 6 Back To Index v Procedures and Parameters.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
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.
Lec4 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 4 Back To Index v Iteration - Looping v Arrays.
User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
08/09/ Arrays Defining, Declaring & Processing.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
Visual Basic: An Object Oriented Approach 5: Structured Programming.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Using Arrays and File Handling
Visual Basic Games: Prepare for Hangman
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
© 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.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Random Files Please see speaker notes for additional information!
Lecture Set 12 Sequential Files and Structures Part D - Structures.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
1.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Two Forms Please use speaker notes for additional information!
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
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.
Types of Visual Basic Data Numeric Data Non Numeric Data.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
31/01/ Selection If selection construct.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
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.
Using a Database Access97 Please use speaker notes for additional information!
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
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.
COMPREHENSIVE Excel Tutorial 12 Expanding Excel with Visual Basic for Applications.
VBA - Excel VBA is Visual Basic for Applications
Department Array in Visual Basic
CS285 Introduction - Visual Basic
To understand what arrays are and how to use them
Introduction to Computer Programming IT-104
Presentation transcript:

Control Arrays, Records, and Record Arrays in V.B. Week 10

Array example - sorting In this example a simple set of inputs are set up. Clicking the top button allows data entered to be stored in the array The middle one sorts the data The bottom button puts sorted data in the text boxes

Set Globals and Initialise data Const cmin = 0 Const cmax = 4 Private numbers(cmin To cmax) As Integer ‘declare data array’ Sub Form_Load () Dim i As Integer Rem initialise array contents For i = cmin To cmax numbers(i) = 0 Next i Rem initialise text boxes text1 = numbers(0) text2 = numbers(1) text3 = numbers(2) text4 = numbers(3) text5 = numbers(4) End Sub

Store Numbers Sub cmdAssign_Click () If (text1.Text = "") Or (text2.Text = "") Or (text3.Text = "") Or (text4.Text = "") Or (text5.Text = "") Then Beep MsgBox ("a zero length string is present") Else numbers(0) = CInt(text1.Text) numbers(1) = CInt(text2.Text) numbers(2) = CInt(text3.Text) numbers(3) = CInt(text4.Text) numbers(4) = CInt(text5.Text) End If End Sub

Sort Numbers Sub cmdRearrange_Click () Dim i As Integer Dim pass As Integer Dim temp As Integer Dim NoSwitches As Integer pass = 0 Do pass = pass + 1 NoSwitches = 1 For i = cmin To (cmax - pass) If numbers(i) > numbers(i + 1) Then NoSwitches = 0 temp = numbers(i) numbers(i) = numbers(i + 1) numbers(i + 1) = temp End If Next i Loop Until NoSwitches = 1 End Sub

Redisplay numbers Sub cmdRetrieve_Click () label1.Caption = numbers(0) label2.Caption = numbers(1) label3.Caption = numbers(2) label4.Caption = numbers(3) label5.Caption = numbers(4) End Sub

Control Arrays A control array is a group of controls that share the same: Name type event procedures. It can have from one to as many elements as your system allows. They are useful if you want several controls to share the code or if you want them to be created at run-time.

Control Arrays Sub Text1_KeyPress (Index As Integer, Keyascii As Integer) If (Keyascii Asc("9"))) Then Keyascii = 0 Beep MsgBox ("number must be between 0 and 9") End If End Sub

Previous example using a control array This time cut and paste the text box and label. This creates two control arrays

Attaching code to control array Add the following coding to the text1_keypress event attached to the FIRST box only Private Sub Text1_KeyPress (Index As Integer, Keyascii As Integer) If (Keyascii Asc("9"))) Then Keyascii = 0 Beep MsgBox ("number must be between 0 and 9") End If End Sub On analysis you will see that the same code appears in all the textboxes

Amend assign button Private Sub cmdAssign_Click () Dim i, error_count As Integer error_count = 0 For i = cmin To cmax If text1(i).Text = "" Then error_count = error_count + 1 End If Next i If error_count > 0 Then Beep MsgBox (error_count & " zero length string(s) present") Else For i = cmin To cmax numbers(i) = CInt(text1(i).Text) Next i End If End Sub

Simplify data access and retrieval In the retrieve and display button event replace the code with the following : Private Sub cmdRetrieve_Click () For i = cmin To cmax label1(i).Caption = numbers(i) Next i End Sub Lastly in the form load procedure replace the code with the following code : Private Sub Form_Load () Dim i As Integer Rem initialise array contents and text boxes For i = cmin To cmax numbers(i) = 0 text1(i) = numbers(i) label1(i) = "" Next i End Sub

Control Arrays ‘Copy and paste’ to create Create controls at run-time using load method Match to data arrays Can be used to create a Multiple Document Interface Create positional based displays such as games and panels (e.g. calculator number pad)

Knowledge of arrays so far… Data arrays or control arrays Data arrays multi-dimensional Referenced collectively or individual elements But… Same type only Simple data Cannot store composite data relating to same entity Are a fixed size (this is addressed by using redim and preserve – further reading !!) You should ignore my lack of usage of the hungarian notation when naming controls and variables !!!

Consider the data associated with a book: FieldTypeExample TitleString Better I.T. AuthorString A.N.Other PublisherString Corgi ISBNString DateInteger 1976 PriceCurrency £12.95

Problems with variables: We could use 5 separate variables to hold the data for 1 book : sTitle Better I.T. sAuthor A.N. Other sPublisher Corgi sISBN iDate 1976 cPrice £12.95 but this would not show that the data items are logically related to each other

A User-defined type groups related data items together specifies the format of a record, which may have several fields does not hold data

An example : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Fields A Record structure

Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type sTitle sAuthor sPublisher sISBN iDate cPrice bookType These statements define a type with this structure

Declaring user-defined variables : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType t for user-defined type The user-defined type DOES NOT CONTAIN DATA. It is a template for a variable, which is defined just like any other variable

Declaring user-defined variables : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType MUST be DEFINED IN THE MODULE Variables declared either in the module or in an Event Procedure

Using user-defined variables (or records) : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType Fields are accessed using the syntax (e.g.): tBook.sAuthor

A fuller example : Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType tBook.sAuthor = txtAuthor.Text tBook.iDate = 1978 tBook.Publisher = “Corgi” lblAuthor.Caption = tBook.sAuthor

Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Dim tBook As bookType tBook.sAuthor = txtAuthor.Text tBook.iDate = 1978 tBook.Publisher = “Corgi” lblAuthor.Caption = tBook.sAuthor Note : fields don’t have to be used in same order as defined not necessary to always use them all

Arrays of records: Example : Private aLibrary (1 to 100) As bookType Better I.T.A.N.OtherCorgi SnookerF.BloggsMacmillan :::: :::: :::: :::: :::: :::: :::: :::: :::: :::: :::: :::: Flight of Fancy P.TaylorFontana WoodyW.AllenArden aLibrary(1) aLibrary(2) aLibrary(99) aLibrary(100)

Accessing arrays of user-defined types An example syntax: Type bookType sTitle As String sAuthor As String sPublisher As String sISBN As String iDate As Integer cPrice As Currency End Type Private aLibrary (1 to 100) As bookType lblTitle.Caption = aLibrary(99).sTitle array name field name array index

Summarising ….

User-defined types: are usually called “records” group related data items (or “fields”) together can have fields of different data types are usually used in conjunction with Files