Visual Basic Review - ICA

5,011
-1

Published on

This is provided as a review for the Fourth Quarter Periodical Test in Computer I.

Published in: Technology
1 Comment
21 Likes
Statistics
Notes
  • Why you don't allow to download the slideshow, it is more didactic and we can learn more easily.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
5,011
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
1
Likes
21
Embeds 0
No embeds

No notes for slide

Visual Basic Review - ICA

  1. 1. VISUAL BASIC 6 REVIEW
  2. 2. Dear First Year, This review is given to you from the Variables up to the Select Case lesson just in case some of you are still unclear on certain topics. Good luck on your test and study well! Ms. Emily Trajano Computer Coordinator February 18, 2009
  3. 3. Variables, Operators & Functions
  4. 4. A. Variables
  5. 5. Variable A sign or a name that stands for a particular value in a program. It may also store information while the program is running.
  6. 6. <ul><li>Variables are assigned a data type that indicates what kind of data it will store and the amount of memory it will use. </li></ul>
  7. 7. <ul><li>Conventions in Naming a Variable </li></ul><ul><li>A name should be descriptive of the data it holds. It should be unique. </li></ul><ul><li>A variable must begin with a letter. </li></ul><ul><li>Name can be up to 255 characters long. </li></ul>
  8. 8. Conventions in Naming a Variable 4. Must not contain a space or characters used to identify data type such as ! # % & @ 5. Do not use dash to avoid confusion with the minus sign (-). Instead, use underscore _
  9. 9. Data Types <ul><li>Byte </li></ul><ul><li>Boolean </li></ul><ul><li>IntegerLong (long integer) </li></ul><ul><li>Single (single-precision floating-point) </li></ul><ul><li>Double (double-precision floating-point) </li></ul><ul><li>Currency (scaled integer) </li></ul><ul><li>DecimalDate </li></ul><ul><li>ObjectString (variable-length) </li></ul><ul><li>String (fixed-length) </li></ul><ul><li>Variant (with numbers) </li></ul><ul><li>Variant (with characters) </li></ul><ul><li>User-defined (using Type) </li></ul>
  10. 12. <ul><li>Declaring Variables </li></ul><ul><li>Explicit Declaration - variable is declared at the beginning of the procedure in the Declaration Section </li></ul>
  11. 13. Examples: Dim Num_1 as Integer Dim Age as String Dim Reg_DOB as Date Dim Name as String, Address as String, Num2 as Integer (3 variable declarations in one statement) Dim Price1, Price2, Price3 as Single (only Price 3 is declared as Single; Price1 and Price2 are considered Variants because they are not explicitly declared)
  12. 14. Declaring Variables 2. Implicit Declaration - variable is not formally declared. It is a result form other variable declarations
  13. 15. Examples: Dim Num1 as Integer Explicit Declaration Dim Num2 as Integer Explicit Declaration Num3 = Num1 * Num2 Implicit Declaration
  14. 16. In implicit declarations, the readability and clearness of the code is sometimes sacrificed. For VB to consider implicit declarations as error, you can use the Option Explicit clause. Option Explicit Dim Num1 as Integer, Gender as String
  15. 17. Scope of Variables 1. Public Statements – variable can be used in several forms of the projects
  16. 18. <ul><li>Scope of Variables </li></ul><ul><li>2. Private Statements – variable can only be used in the form where it is declared. </li></ul>
  17. 19. <ul><ul><li>a. Module-level – variables declared in the declarations section of the module. It is available to every control in the form. </li></ul></ul><ul><ul><li>b. Procedure-level – variables declared inside a procedure. It is only available when the procedure is called. It will go back to its original value once it is called. </li></ul></ul>
  18. 20. LET’S ANALYZE THE CODE!!! Declarations Section Procedure for Command button 1 Procedure for Command button 2
  19. 21. Option Explicit – tells that no implicit declaration is allowed Dim Sum as Integer – declared the variable Sum as Integer - Sum can be used by any control in the form
  20. 22. Private Sub Command1_Click() Dim Num1 As Integer – Num1 is only declared for this procedure Static Product As Integer – Instead of Dim, Static is used to declare a variable that can be used by other controls in the form. (another way of declaring a module-level variable, only it is inside a procedure and not the declarations section)
  21. 23. Num1 = Num1 * 10 – The value of num1 is to be multiplied by 10 - once the variable is called, it will go back to its original value Product = Num1 * 5 – the value of Product is multiplied by 5 and will retain its value End Sub
  22. 24. Private Sub Command2_Click() Dim Num1 As Integer – Num 1 is declared as Integer Num1 = Num1 + 1 – the value of Num1 is incremented by 1 End Sub
  23. 25. B. Constants
  24. 26. Constant A constant stores value that does not change during the execution of the procedure.
  25. 27. 2 Types of Constants 1. Intrinsic Constants – defined by VB examples: vbTrue, vbBlack 2. User-defined Constants – defined by programmers who writes the code examples: Const Pi = 3.1416 Const Max_Num = 100
  26. 28. Object Browser The Object Browser displays all intrinsic constants that are related to the properties of the controls you created, including the procedures and modules you define for the project.
  27. 29. To open: Click the Object Browser button on the Standard Toolbar or Press F2 function key on the keyboard Description of intrinsic constant is found below the window.
  28. 30. C. Operators
  29. 31. 1. Mathematical & Text Operators – performs mathematical operations on values and variables Types of Operators: Operators – are symbols that represent a specific action
  30. 32. The order of evaluation: Parenthesis ( ), ^, *, /, , Mod, +, - Hi User! “ Hi ” & “User!” String concatenation &  1 25 / 4 Integer division 3 15 Mod 4 Remainder of division Mod 10 15 - 5 Subtraction -  19 15 + 4 Addition +  3 15 / 5 Division /  15 5 * 3 Multiplication *  25 5 ^ 2 Exponent (power of) ^ RESULT EXAMPLE DEFINITION OPERATOR
  31. 33. 2. Relational Operators – compares two values and returns a logical result of true or false Types of Operators:
  32. 34. True (10=5) OR (10>2) Logical OR  OR  True (10=10) AND (5>4) Logical AND  AND True 10<>5 Not equal to <> True 10<=10 Less than or equal to <=  True 10 >= 8 Greater than or equal to >=  False 10 < 8 Less than  <  True 10 > 8 Greater than  >  False 10 = 8 Equal to  =  RESULT EXAMPLE DEFINITION OPERATOR
  33. 35. 3. Logical Operators – applies a logical expression to one or more logical values Types of Operators:
  34. 36. True (10=5) OR (10>2) Logical OR  OR  True (10=10) AND (5>4) Logical AND  AND TRUE 10 = NOT 5 Negation NOT RESULT EXAMPLE DEFINITION OPERATOR
  35. 37. HANDS-ON ACTIVITY 3 (p. 61 of the module) <ul><li>1. Create a currency converter program. </li></ul><ul><li>2. The user must input the following: </li></ul><ul><li>- the exchange rate per P1.00 </li></ul><ul><ul><li> - the amount that will be converted </li></ul></ul><ul><li>3. The program should display how much the </li></ul><ul><li>converted money will be. </li></ul><ul><li>4. Design your user interface. </li></ul><ul><li>Folder: Currency Converter </li></ul><ul><li>Project: prjCurr_Converter Form: frmCurr_Converter </li></ul>Time Allotment: 15 minutes
  36. 38. HANDS-ON ACTIVITY 2 (p. 62 of the module) 1. Create a weight converter program. 2. The user must input the weight in kg. 3. The program should display the weight in pounds when the Convert button is clicked. Formula: 1kg = 2.2 lbs 4. Design your user interface. Folder: Weight Converter Project: prjWeight_Converter Form: frmWeight_Converter Time Allotment: 15 minutes
  37. 39. D. Functions
  38. 40. <ul><li>Functions – are procedures that return a result when called. There are many functions by type of statements. We will only cover the commonly used ones. </li></ul>
  39. 41. <ul><li>Functions </li></ul><ul><ul><li>Most functions are by a pair of parenthesis </li></ul></ul><ul><ul><li>They often appear on the right side of an equal sign </li></ul></ul>
  40. 42. Examples: <ul><ul><li>Num1 = SQR(16) </li></ul></ul><ul><ul><li>Cur_Date = Date </li></ul></ul>
  41. 43. Commonly Used Functions: 1. String Functions Results: Share Share your Example: Dim Message as String, Result as String Message = “Share your blessings.” Result = Left(Message,5) Result = Left(Message,10) Left(string,length) Returns a specified number of characters from the left side of the string Left Result: 6 Example: Dim Name as String, Result as String Name = “Monica” Result = Len(Name) Len(string) Returns the number of characters of a string Len
  42. 44. Results: Patience To Virtue Example: Dim Quote as String, Result as String Quote = “Patience is a Virtue” Result = Mid(Quote,1,8) Result = Mid(Quote,10,2) Result = Mid(Quote,15,6) Mid(string,starting position,length) Returns a number of characters of a string starting from at a position indicated Mid Results: blessings sings INGS Example: Dim Message as String, Result as String Message = “Share your blessings.” Result = RightMessage,9) Result = Right(Message,5) Result = UCASE(Right(Message,3)) Right(string,length) Returns a specified number of characters from the right side of the string Right
  43. 45. Result: slippery when wet Example: Dim Warning as String, Result as String Warning = “Slippery When Wet” Result = Lcase(Warning) LCase(String) Changes all uppercase letters into lowercase letters LCase Result: WET FLOOR Example: Dim Warning as String, Result as String Warning = “Wet Floor” Result = Ucase(Warning) UCase(String) Changes all lowercase letters into uppercase letters UCase Results: 4 Example: Dim Result as String, Result = InStr(1002, 2) InStr(string1,string1) Returns the position of string2 in string1 InStr
  44. 46. Result: 5 (as a number) Example: Dim Num as String, Result as String Num = 5 Result = Val(Num) Val(String) Converts values to numbers Val
  45. 47. Commonly Used Functions: 2. Date Functions Result: 10/15/2000 Example: Dim Cur_Date as String Cur_Date = Date Date() Returns the current date of the computer being used Date 10/16/2005 6:30:23 PM Example: Lbl1.Caption=Now() Now() Returns both the current date and time from the system clock Now
  46. 48. 2006-10-16 Example: Dim Cur_Date as Date Lbl1.caption= Format(Cur_Date, “yyyy-mm-dd”) Format(string, “specified format”) Returns a formatted string according to user-defined format Format Result: 2:08:34 PM Example: Dim Cur_Time as String Cur_Time = Time Time() Returns the current time of the computer being used Time
  47. 49. Commonly Used Functions: Formatting Characters for Date: 8 Hour without zero h 11 Number of month m Friday Name of the day of the week dddd 2015 Year with century yyyy 09 Day of the month with zero dd 06 Year without century yy Example Description Formatting Characters Example Description Formatting Characters
  48. 50. 26 Seconds ss 9 Day of the month without zero d 30 Minutes mm November Long name of month mmmm 08 Hour with zero hh nov Short name of month mmm
  49. 51. Commonly Used Functions: 3. Number Functions 0245.60 $245.60 Example: Dim Result as Single Result = 245.6 Lbl1.caption= Format(Result, “0000.00”) Lbl1.caption= Format(Result, “$##00.00”) Format(string, “specified format”) Returns a formatted string according to user-defined format Format
  50. 52. Formatting Characters for Numbers: Literal character; displayed as typed $+-() space Thousands separator , Decimal placeholder . Represents a digit, without non-significant leading and trailing zeros # Represents a digit, with non-significant leading and trailing zeros 0 Description Formatting Characters
  51. 53. HANDS-ON ACTIVITY 1 <ul><li>Encode the following codes. </li></ul><ul><li>Run the program. </li></ul><ul><li>Save: Folder: Functions </li></ul><ul><li>Form: frmFunctions </li></ul><ul><li>Project: prj function </li></ul>Time Allotment: 20 minutes
  52. 55. <ul><li>Create a button and a label </li></ul><ul><li>to get the following: </li></ul><ul><li>First 4 letters. </li></ul><ul><li>Last 4 letters </li></ul><ul><li>All letters in capital </li></ul><ul><li>All letters in small letters </li></ul><ul><li>Resulting will be the letters ECHNO </li></ul><ul><li>The date </li></ul><ul><li>The time </li></ul>
  53. 56. QUIZ 3 <ul><li>Follow the interface given. </li></ul><ul><li>Run the program. </li></ul><ul><li>Save: Folder: Name-Function </li></ul><ul><li>Form: frmName-Function </li></ul><ul><li>Project: prjName-Function </li></ul>Time Allotment: 20 minutes
  54. 57. QUIZ 3 Time Allotment: 40 minutes
  55. 58. QUIZ 4 <ul><li>Design your own interface. </li></ul><ul><li>Run the program. </li></ul><ul><li>Save: Folder: Date-Function </li></ul><ul><li>Form: frmDate-Function </li></ul><ul><li>Project: prjDate-Function </li></ul>Time Allotment: 20 minutes
  56. 59. <ul><li>Create a label with your first, </li></ul><ul><li>middle and last name. </li></ul><ul><li>2. Get the following: </li></ul><ul><li>a. First name </li></ul><ul><li>b. Middle name </li></ul><ul><li>c. Last name </li></ul><ul><li>d. All letters in capital </li></ul><ul><li>e. All letters in lowercase </li></ul><ul><li>f. Number of characters </li></ul><ul><li>3. Create a button to clear and close. </li></ul><ul><li>4. Design the user interface. </li></ul>Time Allotment: 30 minutes
  57. 60. QUIZ 4 Time Allotment: 40 minutes
  58. 61. Commonly Used Functions: 3. Input/Output Functions Example: MsgBox (&quot;Welcome to Visual Basic!!!&quot;) MsgBox(“prompt”) Displays a pop-up box with a message and waits the user to click a button. MsgBox Example: InputBox (&quot;Type your name:&quot;) InputBox(“prompt”) Displays a dialog box with a prompt and a textbox with the OK and CANCEL button InputBox
  59. 62. InputBox() FUNCTION <ul><ul><li>InputBox() function displays a message box where user can input data. </li></ul></ul>
  60. 63. <ul><ul><li>Syntax: </li></ul></ul><ul><ul><li>Prompt  - The message displayed prompting what is to be entered </li></ul></ul><ul><ul><li>Title - The title of the Input Box. </li></ul></ul><ul><ul><li>default-text   - The default text that appears inside the input box </li></ul></ul><ul><ul><li>x-position and y-position - the position or the coordinates of the input box </li></ul></ul><ul><ul><li>variable=InputBox(Prompt, Title, default_text, x-position, y-position) </li></ul></ul>
  61. 64. <ul><ul><li>Example: </li></ul></ul><ul><ul><li>Code </li></ul></ul><ul><ul><li>At Run-time </li></ul></ul>
  62. 65. QUIZ # 1 <ul><li>Save: Folder: InputBox_Average </li></ul><ul><li>Form: frmAverage </li></ul><ul><li>Project: prjAverage </li></ul>Time Allotment: 20 minutes
  63. 66. QUIZ # 1 <ul><li>Create a program that will ask the </li></ul><ul><li>user to input his/her grades in Math, </li></ul><ul><li>Science and English. </li></ul><ul><li>2. After entering the English grade, </li></ul><ul><li>the Form will display the average. </li></ul><ul><li>3. If she failed, the message “Study </li></ul><ul><li>Harder should appear”, if she passed, </li></ul><ul><li>message will be “Congartulations! </li></ul><ul><li>You got a passing mark!”. </li></ul><ul><li>4. Design your user interface. </li></ul>Time Allotment: 30 minutes
  64. 67. MsgBox() FUNCTION <ul><ul><li>MsgBox function produces a pop-up message box and prompts the user to click on a command button before going to the next instruction. </li></ul></ul>
  65. 68. <ul><ul><li>Syntax: </li></ul></ul><ul><ul><li>Prompt  - The message displayed prompting what is to be entered </li></ul></ul><ul><ul><li>Title - The title of the Input Box. </li></ul></ul><ul><ul><li>default-text   - The default text that appears inside the input box </li></ul></ul><ul><ul><li>x-position and y-position - the position or the coordinates of the input box </li></ul></ul><ul><ul><li>variable=MsgBox(Prompt, Style Value, Title) </li></ul></ul>
  66. 69. <ul><ul><li>Style Values </li></ul></ul>Retry and Cancel buttons vbRetryCancel 5 Yes and No buttons vbYesNo 4 Yes, No and Cancel buttons vbYesNoCancel 3 Abort, Retry and Ignore buttons. vbAbortRetryIgnore 2 Ok and Cancel buttons vbOkCancel 1 Ok button vbOkOnly 0 Displayed Button/s Named Constant Style VAlue
  67. 70. Examples: Smenu=MsgBox( &quot;Click OK to Proceed&quot;, 1, &quot;Startup Menu&quot;)   Smenu=MsgBox(&quot;Click OK to Proceed“, vbOkCancel,&quot;Startup Menu&quot;) <ul><ul><li>Style Values </li></ul></ul>The style value can be used instead of the named constant like in the example below.
  68. 71. The variable holds values that are returned by the MsgBox ( ) function. The values are determined by the type of buttons being clicked by the user. It has to be declared as Integer data type in the procedure or in the general declaration section. Return Values and Command Buttons No button vbNo 7 Yes button vbYes 6 Ignore button vbIgnore 5 Retry button vbRetry 4 Abort button vbAbort 3 Cancel button vbCancel 2 Ok button vbOk 1 Clicked Button Named Constant Value
  69. 72. HANDS-ON ACTIVITY 1 <ul><li>Encode the following codes. </li></ul><ul><li>Run the program. </li></ul><ul><li>Save: Folder: Validation </li></ul><ul><li>Form: frmValidation </li></ul><ul><li>Project: prjValidation </li></ul>Time Allotment: 20 minutes
  70. 73. Code
  71. 74. At run-time When VALIDATE button is clicked At Run-Time When OK button is clicked When CANCEL button is clicked
  72. 75. HANDS-ON ACTIVITY 2 <ul><li>Encode the following codes. </li></ul><ul><li>Run the program. </li></ul><ul><li>Save: Folder: Message Box </li></ul><ul><li>Form: frmMsgBox </li></ul><ul><li>Project: prjMsgBox </li></ul>Time Allotment: 20 minutes
  73. 76. ICONS Icons can be placed inside the message box to add more distinction to the message it wants to convey. vbInformation 64 vbExclamation 48 vbQuestion 32 vbCritical 16 Icon Named Constant Value
  74. 77. Code
  75. 78. Message Box with Icon
  76. 79. HANDS-ON ACTIVITY 3 <ul><li>Encode the following codes. </li></ul><ul><li>Run the program. </li></ul><ul><li>Save: Folder: Testing </li></ul><ul><li>Form: frmTest </li></ul><ul><li>Project: prjTest </li></ul>Time Allotment: 20 minutes
  77. 83. Conditional Statements
  78. 84. <ul><li>IF..THEN </li></ul><ul><li>Structure </li></ul>
  79. 85. If…Then Structure The If… Then structure presents a condition and if the condition is True , the statement or series of statements (contains event) in between the If and End If will be executed.
  80. 86. Syntax: If condition Then Statement/s End If
  81. 87. Example 1: If Score = 100 Then Lbl1.caption = “Perfect” End If
  82. 88. Example 2: If Price >= 100 Then Price = Price * .1 End If
  83. 89. B. IF..THEN…Else Structure
  84. 90. If…Then…Else Structure Like the If… Then structure, a condition will be presented and if the condition is True , the statement or series of statements (contains event) after the If statement will be executed, and if False , it will execute the statement after Else .
  85. 91. Syntax: If condition Then Statement/s Else Statement/s End If
  86. 92. Example 1: If score = 100 Then Lbl1.caption = “Perfect” Else Lbl1.caption = “With mistakes” End If
  87. 93. Example 2: If PName = “Anna” Then Lbl1.caption = “ANNA” MsgBox (&quot;Hello, Anna!!!&quot;) Else Lbl1.caption = “UNIDENTIFIED” MsgBox (&quot;Unauthorized Person!!!&quot;) End If
  88. 94. Example 3:
  89. 95. Output of Example 3: Output when Anna is entered Output when Rosie is entered
  90. 96. C. Nested IF Structure
  91. 97. Nested IF Structure The Nested If structure uses several conditions. If the condition is True , the statement/s after the If condition will be executed. If the condition is False , each condition in the ElseIF is evaluated until it gives a True result then the statement/s following it will be immediately executed. If no condition was met, the statement/s after Else will be executed.
  92. 98. Syntax: If condition1 Then Statement/s 1 ElseIf condition2 Then Statement/s 2 ElseIf condition3 Then Statement/s 3 Else Statement/s 4 End If
  93. 99. Example 1: If Num <=100 Then Num= Num + 1 ElseIf Num <= 200 Then Num = Num + 2 ElseIf Num<= 300 Then Num = Num + 3 Else Num = Num + 0 End If
  94. 100. Example 2:
  95. 101. Output Example 2: Input Box Message Box (Output when MARK is entered)
  96. 102. HANDS-ON ACTIVITY <ul><li>1. Create a program that will ask for a username </li></ul><ul><li>and password. </li></ul><ul><ul><li>Username: ICA </li></ul></ul><ul><ul><li>Password: Greenhills </li></ul></ul><ul><li>2. If the username and password are both correct, </li></ul><ul><li>a message box will appear saying “YOU MAY </li></ul><ul><li>NOW ENTER”, otherwise the message will be </li></ul><ul><li>“ TRY AGAIN!!!” </li></ul><ul><li>3.Design your user interface. </li></ul><ul><li>Form: prjPassword Project: prjPassword </li></ul><ul><li>Folder: Password </li></ul>Time Allotment: 20 minutes
  97. 103. D. SELECT Case
  98. 104. SELECT Case The Select Case presents several conditions. It is used instead of the Nested If because it is clearer and easier to use. It evaluates an expression once. The result is compared with several values, if they match, the statement in the statement block is executed.
  99. 105. Select Case expression Case Value1 Statement block 1 Case Value 2 Statement block 2 Case Value 3 Statement block 3 Case Value 4 Statement block 4 Case Else Statement block End Select Syntax:
  100. 106. Example 1: Select Case Subject Case 1 LblSubject.Caption = “Mathematics” Case 2 LblSubject.Caption = “Science” Case 3 LblSubject.Caption = “English” Case 4 LblSubject.Caption = “Filipino” Case 5 LblSubject.Caption = “Religion” Case 6 LblSubject.Caption = “Physical Education” Case 7 LblSubject.Caption = “Computer” Case 8 LblSubject.Caption = “Music” End Select
  101. 107. Example 2:
  102. 108. Output of Example 2: Output when Enter button is clicked after user input
  103. 109. HANDS-ON ACTIVITY <ul><li>List down 5 products found in an </li></ul><ul><li>appliance store. Give a two-letter code for </li></ul><ul><li>each product (show the codes on the form </li></ul><ul><li>for reference). </li></ul><ul><li>2. Create a program that will ask the user to </li></ul><ul><li>enter the code of the product. Once the </li></ul><ul><li>code is entered, the image, description, </li></ul><ul><li>brand and price will appear in different </li></ul><ul><li>controls. </li></ul>Time Allotment: 40 minutes
  104. 110. HANDS-ON ACTIVITY 3. Add controls that will clear the entries and end the program. 4.Save under Folder: Product Code Form: frmProduct_Code Project: prjProduct_Code Time Allotment: 40 minutes
  105. 111. POINTERS <ul><li>Know and understand the flow of the program before designing and coding. </li></ul><ul><li>Always remember to put the code in the control indicated in the instruction. </li></ul><ul><li>Common mistakes are syntax and typographical errors. </li></ul>
  106. 112. POINTERS <ul><li>You normally declare variables if you ask a user to enter data and that data is used in any part of the program. </li></ul><ul><li>Copy and Paste Command will be very useful to save time. </li></ul><ul><li>Always take note of the errors you encounter in writing the codes and how you solve the error. </li></ul>
  107. 113. <ul><li>AUTOMATED QUIZ PROGRAM </li></ul><ul><li>Choose the type of quiz you want (e.g. multiple choice, True or False, Matching Type, etc.) </li></ul><ul><li>Create a program that will show 10 questions. The program should provide the user ways on how to answer (ex. Option buttons for multiple choice). </li></ul>CHALLENGE!
  108. 114. <ul><li>AUTOMATED QUIZ PROGRAM </li></ul><ul><li>After the user answers all the questions. A control can be used to get the total correct answers. </li></ul><ul><li>Optional. It can even show the correct answer using another control. </li></ul>CHALLENGE!
  109. 115. The End

×