Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions in Visual Basic; the most commonly used arrays are one- dimensional and two-dimensional Programmers use arrays to store related data in the internal memory of the computer Data stored inside the computer can be both written and read much faster than data stored in a file on a disk
Tutorial 102 Variable Arrays A one-dimensional array is simply a row (or column) of variables; a two-dimensional array resembles a table in that it has rows and columns Each element in an array is identified by a subscript You refer to an array element by the array’s name followed by the element’s subscript
Tutorial 103 One-dimensional Array NebraskaNew JerseyNew Mexico Tennessee Texas Nebraska New Jersey New Mexico Tennessee Texas
Tutorial 104 Two-dimensional Array Nebraska New Jersey New Mexico Tennessee Texas Lincoln Trenton Santa Fe Nashville Austin
Tutorial 105 One-dimensional Array Dim arrayname(lower subscript To upper subscript) As datatype Public arrayname(lower subscript To upper subscript) As datatype Lower subscript and upper subscript are numbers that define the lowest and the highest subscript in the array The Dim (and Public) statements create and initialize the array variables in memory
Tutorial 106 One-dimensional Array Dim strMonthArray(1 To 6) As String Dim intSquareArray(1 To 5) As Integer Dim sngNum(1 To 10) As Single Dim udtEmp(1 To 20) As EmpStruc Note: It is not necessary for an array name to contain the word “Array”
Tutorial 107 One-dimensional Array strMonthArray(1) = “Jan” strMonthArray(2) = “Feb” strMonthArray(3) = “Mar” strMonthArray(4) = “Apr” strMonthArray(5) = “May” strMonthArray(6) = “June”
Tutorial 108 One-dimensional Array For intNum = 1 to 5 intSquareArray(intNum) = intNum * intNum Next intNum For intNum = 1 to 10 sngNum(intNum) = Val(InputBox(“Enter number”)) Next intNum
Tutorial 109 One-dimensional Array Do While Not EOF(1) intNum = intNum + 1 Input #1, udtEmp(intNum).Name, udtEmp(intNum).Salary Loop
Tutorial 1010 Calculating the Average Declare variables Repeat for intNum = 1 to 20 by 1 add array score to intTotal variable End repeat for intNum Calculate the average by dividing intTotal by 20 Display the average
Tutorial 1011 Finding the Highest Value Declare variables Assign first array value to intHigh variable Repeat for intNum = 2 to 20 by 1 If current array value > intHigh value then assign current array value to intHigh End If End repeat for intNum Display the highest value(stored in intHigh)
Tutorial 1012 Updating an Array Declare variables Prompt user for the value to add Repeat for intNum = 2 to 20 by 1 add value to current array value End repeat for intNum Display “Updated” message Open a file Repeat for intNum = 1 to 20 by 1 Write array value to file End repeat for intNum Close the file
Tutorial 1013 Two-dimensional Array Dim arrayname(lower subscript To upper subscript, lower subscript To upper subscript) As datatype Public arrayname(lower subscript To upper subscript, lower subscript To upper subscript) As datatype
Tutorial 1014 Two-dimensional Array Dim strNameArray(1 To 6, 1 To 3) As String Dim intNum(1 To 5, 1 To 10) As Integer Dim sngSales(1 To 20, 1 To 2) As Single Note: It is not necessary for an array name to contain the word “Array”
Tutorial 1015 Two-dimensional Array Declare variables Open the file Repeat for intRow = 1 to 4 by 1 Repeat for intCol = 1 To 2 by 1 Read name from file and store it in the current array element End repeat for intCol End repeat for intRow Close the file
Tutorial 1016 DataGrid Control When bound to an ADO data control, the DataGrid control displays the information from the recordset created by the ADO data control’s RecordSource property The information displays in a row and column format, with each database field appearing in a column, and each record appearing in a row The intersection of a column and a row is called a cell
Tutorial 1017 DataGrid Control Use the Components option on the Project menu to add the Microsoft DataGrid Control 6.0 (OLEDB) tool to the toolbox, then place a DataGrid control in the interface dgd is the three-character ID for a DataGrid control You bind a DataGrid control to the ADO data control by setting the DataGrid control’s DataSource property
Tutorial 1018 DataGrid’s Popup Menu
Tutorial 1019 Sizing Columns
Tutorial 1020 General Tab
Tutorial 1021 General Tab Check Boxes (Properties) AllowAddNew - allows the user to add records while the application is running AllowDelete - allows the user to delete records while the application is running AllowUpdate - allows the user to edit existing records while the application is running ColumnHeaders - controls whether a header appears above each column Enabled - controls whether the control responds to a user-generated event
Tutorial 1022 Columns Tab
Tutorial 1023 Format Tab
Tutorial 1024 Layout Tab
Tutorial 1025 Five DataGrid Events BeforeColUpdate AfterColUpdate AfterColEdit BeforeUpdate AfterUpdate
Tutorial 1026 Referring to a Field Within the Fields Collection Syntax for referring to a field within a Recordset object’s Fields collection: [form.]adocontrol.Recordset.Fields(field) field is the name of the field enclosed in quotation marks
Tutorial 1027 Creating an Object Variable Dim objectVariableName As datatype Dim objFlds As Object Dim objRst As Recordset datatype can be a specific object type (such as Sheet, Range, or Recordset) or the keyword Object, which can be used to represent a reference to any object
Tutorial 1028 Assigning an Object Reference to an Object Variable Set objectVariableName = object Set objFlds = adoPay.Recordset.Fields Set objRst = adoPay.Recordset After assigning the reference to the object variable, you can use the shorter object variable’s name in the code
Tutorial 1029 Round Function Round(expression [,numdecimalplaces]) Returns a number rounded to a specific number of decimal places expression is a numeric expression numdecimalplaces is a number indicating how many places to the right of the decimal are included in the rounding
Tutorial 1030 Function Procedure Referred to simply as a function Like a sub procedure, a function is a block of code that performs a specific task Unlike a sub procedure, a function returns a value Some functions are intrinsic (Val, Round) You also can create your own functions, referred to as user-defined functions
Tutorial 1031 Function Syntax Private Function functionname [(arglist)] As datatype [statements] functionname = expression End Function The functionname = expression statement assigns the return value to the function To invoke a function you need simply to include the function’s name in a statement
Tutorial 1032 Debugging Technique You can use the Debug menu’s Toggle Breakpoint and Add Watch commands to help you debug an application
Tutorial 1033 Breakpoint Instruction
Tutorial 1034 Add Watch Dialog Box
Tutorial 1035 Watches Window
Tutorial 1036 Watches Window Showing Current Values of Watch Expressions