Appendix B. Answers

Hour 1 Quiz

1:

What is the purpose of Visual Basic?

A1:

To develop Windows applications

2:

How have programming languages changed over the years?

A2:

Programming languages are now visual to handle Windows environments.

3:

What programming language is Visual Basic based on?

A3:

BASIC (stands for Beginner's All-Purpose Symbolic Instruction Code)

4:

Which Visual Basic development environment window forms the background for the applications you develop?

A4:

Form window

5:

What is the difference between the Form window and the Form Layout window?

A5:

The Form window holds the application's form background and all its user controls, such as command buttons. The Form Layout window lets you adjust the Form window's location on the user's screen when the user first executes the application.

6:

True or false: All the tools you find on the Toolbox window when you start Visual Basic are the intrinsic controls.

A6:

True

Hour 1 Exercise

Q1:

Start Visual Basic and select various options from the View window. You will see several new windows open. Look through the menu options and click any scrollbars you see. Don't save anything when prompted. Double-click over tools on the toolbox to see different tools appear on the Form window. Move the tools away from the center of the window to see more of them at one time. As you click different tools in the Form window, watch the Properties window change to reflect the current tool's properties.

A1:

No answer is necessary.

Hour 2 Quiz

1:

How do windowed programs differ from programs running in text-based environments?

A1:

Windows programs are visual and include graphic elements the user interacts with to control the program. In addition, Windows programs respond to events, whereas text-based programs guide the user more rigidly and the programs control the user's next move.

2:

What are events?

A2:

Events occur during your application's execution and usually, but not always, occur in response to the user's action, such as a mouse button click or a keystroke.

3:

Why are project component filenames not usually the same as their internal VB names?

A3:

The Project window's component filenames use common filename extensions such as .vbp and .bas, but Visual Basic doesn't use filename extensions when referring to those components.

4:

What is usually the last step a VB programmer takes before distributing an application to users?

A4:

Compilation

5:

How does Visual Basic know which procedure to execute for a particular control's event?

A5:

The control's event procedures determine the code that executes when an event occurs.

6:

True or false: All controls support one and only one event.

A6:

False

7:

Which usually respond at design time: control property changes or event procedures?

A7:

Control property changes show themselves at design time and at runtime, whereas event procedures execute only at runtime.

Hour 2 Exercise

Q1:

Scroll through the interest rate project's form module again, looking at the various event procedures coded there. Determine which events are handled and which are not. An event procedure whose name begins with Form is an event procedure for the form itself. For example, you can respond to the user's mouse click over the form differently from a mouse click over a command button. Look for the events associated with the various command buttons on the form. Most often, a command button's event procedure is a …Click() or …DblClick() event procedure because most users either click or double-click command buttons and the click and double-click events are the ones you often need to respond to.

A1:

No answer is necessary.

Hour 3 Quiz

1:

What is the fastest way to place a control on the form?

A1:

Double-click the control on the Toolbox window to place the control on the form quickly.

2:

What are a control's sizing handles for?

A2:

The sizing handles let you move the control and change its size.

3:

How can you select multiple controls?

A3:

Hold down the Ctrl key when you click each control, or lasso multiple controls by dragging a square around them with the mouse.

4:

True or false: Some properties change automatically as you move and resize controls.

A4:

True (such as the Top and Left properties)

5:

Which form property sets the title that appears in the form's title bar?

A5:

The Caption property sets the form title.

6:

What is the difference between an object and a control?

A6:

A control is a type of object; objects also can include the form, menus, and other application components.

7:

When is the best time to add a ToolTip to a control?

A7:

Add ToolTips to controls when you place the controls on the form.

8:

Why do some controls display an ellipsis when you click certain property values?

A8:

A dialog box appears when you click a control property's ellipsis to let you fill in multiple values related to the control property.

Hour 3 Exercises

1:

Create another application from scratch. Add two command buttons and one label between them. Make the label's Caption property blank when you place the label on the form. When the user clicks the first command button, a caption should appear on the label that reads Clicked!. You'll need to place the following Visual Basic statement inside one of the application's event procedures to do this:

lblClick.Caption = "Clicked!"

Save the project and form module so you can modify the application later if you want.

A1:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 3 Exer 1.

2:

Load the project you created in the previous exercise and add ToolTips to the two command buttons and to the label button. Run the application and test the ToolTips to see if they work.

A2:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 3 Exer 2.

Hour 4 Quiz

1:

True or false: A selected control (the control with its sizing handles showing) is the control with the focus.

A1:

False; the focus appears at runtime, not at design time.

2:

True or false: When the user clicks the mouse over a control in a window that doesn't have the focus, the clicked control still gets the focus.

A2:

True

3:

Which control works better for titles: labels or text boxes?

A3:

Labels

4:

What can you do to close a Form window when the user presses Esc?

A4:

Set the control's Cancel property to True.

5:

Which property disables a text box from triggering events when the user types or clicks the text box?

A5:

The Enabled property

6:

Why do you think labels fail to support a GetFocus event?

A6:

Labels don't directly interact with users.

7:

What happens if you set a label's AutoSize property to True before setting the WordWrap property to True if the label holds a long caption value?

A7:

The label expands horizontally before you have a chance to set the WordWrap property. WordWrap does you no good when the label is so long no wrap is necessary.

8:

Why should you avoid adding too many autosizing labels to the form at one time?

A8:

The form might not be large enough to hold multiple autosizing labels that hold a large amount of text.

Hour 4 Exercises

1:

Write a Visual Basic application that displays an appropriate form title and asks the user for his first and last names in two separate text boxes. Add a command button that terminates the program when the user clicks the command button, presses the command button's hotkey, or presses Esc.

A1:

The code can be retrieved from the CD accompanying this book. After you retrieve the source code, look for a project named Lesson 4 Exer 1.

2:

Create an application with five command buttons. Reverse the focus order so that when you run the application and press the Tab key several times, the focus order flows upward through the command buttons.

A2:

The code can be retrieved from the CD-ROM that accompanies this book. After you retrieve the source code, look for a project named Lesson 4 Exer 2.

Hour 5 Quiz

1:

What is a datatype?

A1:

A datatype describes the kind of data a variable can hold.

2:

What is the difference between a String and a Boolean datatype?

A2:

A string holds zero or more characters, whereas a Boolean datatype holds only one of two values, True or False.

3:

What are two controls that behave as if they conform to the Boolean datatype?

A3:

Option buttons and check boxes

4:

What is the difference between a literal and a variable?

A4:

A literal's value never changes.

5:

Which of the following are invalid variable names?

12Months
a
85
"curSalesForecast"
Acctg98
A5:

12Months, 85

6:

Which operator performs two operations?

A6:

The plus sign performs addition and concatenation. In addition, the equal sign assigns values as well as tests for equality.

7:

What is the difference between a fixed-length string and a variable-length string?

A7:

Fixed-length strings set a limit on the number of characters they can hold.

8:

What value would Visual Basic store in the following ans variables?

a. ans = 1 + 2 + 3 + 4 / 2
b. ans = 1 + 2 + 3 + (4 / 2)
c. ans = 2 ^ 5
d. ans = 25 - 8 / 2 ^ 2 + 1
A8:

  1. 8
  2. 8
  3. 32
  4. 24

Hour 5 Exercises

1:

Write code that declares these variables: your first name, your last name, your age, your tax rate, and whether you are married.

A1:

Dim strFirst As String
Dim strLast As String
Dim intAge As Integer
Dim sngTaxRate as Single
Dim blnMarried As Boolean
2:

Write an application that accepts your age in a text box and then displays, when you click a command button, your age in dog years (your age divided by 7). Don't worry about rounding that might take place.

A2:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 5 Exer 2.

Hour 6 Quiz

1:

What is the difference between a message box and a text box?

A1:

A message box is not a control, but a dialog box that appears when needed to get information from the user. A text box is a control that resides on the form.

2:

Which stays on the user's screen during the majority of a program's execution: a text box or an input box?

A2:

A text box stays on the screen longer, in most cases, than an input box.

3:

Why do the named literals provide for better program maintenance?

A3:

When you use a named literal instead of placing literal values throughout an application, it's easier to make modifications to the code. Named literals are more intuitive to people who maintain your code than values of named literals.

4:

What are the two kinds of remark statements?

A4:

The apostrophe and the Rem statement are both remarks.

5:

Whom are remarks for?

A5:

Remarks are for people who look at your program code.

6:

What does modal mean?

A6:

Modality determines how the dialog box reacts to the user's input.

7:

How many icons can you display with message boxes?

A7:

You can display one of four icons in a message box.

8:

True or false: You can pass multiple arguments and receive multiple return values from functions.

A8:

False; you can return a maximum of one value from a function.

9:

What role do default values play in input boxes?

A9:

The input box returns the default value if the user does not change the default value.

10:

True or false: The MsgBox() function can return one of seven values.

A10:

True

Hour 6 Exercises

1:

Write three remarks for the top of a program that calculates sales tax. The first remark should hold your name, the second should hold the date that you write the remark, and the third should span at least two lines and should describe the purpose of the program.

A1:

'Programmer name: Julie Russell
'Date: November 27, 1999
'This program calculates sales taxes
'based on the customer sales values
2:

Write an input box function that asks users for their ages. Display a default value of 25.

A2:

varAge = InputBox("How old are you?", "Age Request", "25")

Hour 7 Quiz

1:

How do comparison operators differ from mathematical operators?

A1:

Comparison operators return Boolean results and perform no math.

2:

What role does the ASCII table play in comparison logic?

A2:

The ASCII table determines the order of character comparisons.

3:

Do the following statements produce true or false results?

  1. 25 <= 25
  2. "a" >= "B"
  3. 0 < -1
  4. 234.32 > 234.321
A3:

  1. True
  2. True
  3. False
  4. False
4:

When do you code the Else portion of an If statement?

A4:

Code the Else portion if you want to specify execution for the If's false comparison result.

5:

True or false: The End If statement isn't needed for one-line If statements.

A5:

True

6:

Which statement replaces nested If...Else logic?

A6:

Select Case

7:

Which Case option checks for a range of values?

A7:

Case expr1 To expr2

8:

What happens if every Case fails and there is no Case Else option?

A8:

The statement following the End Select executes.

9:

What role do code blocks play in Select Case statements?

A9:

Every statement in the code block executes if that leg of the Select Case executes.

10:

What is wrong with this If statement?

If (intA < 1) And (intC >= 8) Then
  lblDraft.Caption = "Overdrawn"
Else
  lblDraft.Caption = "Underdrawn"
End Else
A10:

The End Else should be End If.

Hour 7 Exercises

1:

Rewrite the following nested If statement using a single If with a logical operator:

If (A > 3) Then
  If (B > 10) Then
    lblAns.Caption = "Yes"
  End If
End If
A1:

If (A > 3) And (B > 10) Then
    lblAns.Caption = "Yes"
End If
2:

Rewrite the following If to eliminate the Not and to clarify the code:

If Not(X < 10) Or Not(Y >= 20) Then
A2:

If (X >= 10) And (Y < 20) Then

Hour 8 Quiz

1:

Why might your program need a loop?

A1:

Your program might need to repeat one or more statements.

2:

How many forms of Do are there?

A2:

Four

3:

True or false: The Do...Loop While and the Do While are identical in every respect.

A3:

False; they check for the condition at different locations in the loop.

4:

What is the difference between Do...Loop While and Do...Loop Until?

A4:

The Do...Loop While loop continues as long as the condition is True and the Do...Loop Until loop continues as long as the condition is False.

5:

Which loop checks its condition at the top of the loop: Do Until or Do...Loop Until?

A5:

The Do Until checks its condition at the top of the loop.

6:

Why do programmers often use the Val() function on control values and InputBox() returns?

A6:

Val() converts string values to numbers.

7:

True or false: A For loop can count down.

A7:

True, if you use a negative Step value.

8:

How many times does the following loop iterate?

intI = 10
do While intI >= 1
intI = intI - 1
Loop
A8:

10 times

9:

What default Step value does Visual Basic use if you don't supply a Step value?

A9:

1

10:

True or false: The Exit statement exits the current application.

A10:

False; Exit supports several forms, including an Exit that exits the current subroutine, an Exit that exits the current function, and an Exit that exits the current loop.

Hour 8 Exercises

1:

Write a program that contains a text box and a command button. Put a label above the text box that tells the user to type a number from 1 to 10 inside the text box. When the user clicks the command button, check the text box for a valid number and issue an error message box if the number isn't inside the expected range. If the user entered a valid number, use a For loop to issue a beeping sound equal to the number entered in the text box.

A1:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 8 Exer 1.

2:

Rewrite Listing 8.3 so that the error message box that you display tells the user that the age entered is too low if the age is less than 10 or that the age entered is too high if the age is more than 99.

A2:

Dim strAge As String
Dim intAge As Integer
Dim intPress As Integer
Do
 strAge = InputBox("How old are you?", "Age Ask")
 ' Check for the Cancel command button
 If (strAge = "") Then
   End   ' Terminate program
 End If
 intAge = Val(strAge)
 
 If (intAge < 10) Then
   ' The user's age is too low
   intPress = MsgBox("Your age is too low!" _
          vbExclamation, "Error!")
 ElseIf (intAge > 99) Then
   ' The user's age is too high
   intPress = MsgBox("Your age is too high!", _
          vbExclamation, "Error!")
 End If
Loop While ((intAge < 10) Or (intAge > 99))

Hour 9 Quiz

1:

What does the form's StartUpPosition value do?

A1:

The StartUpPosition determines the form's initial location on the screen.

2:

Why do label captions often show hotkey letter combinations?

A2:

So the user can quickly move to the text box next to the label

3:

When is the best time to enter ToolTipText properties and why?

A3:

Enter ToolTipText properties when you add controls because you are more familiar with the controls' purpose at that time.

4:

What is a control array?

A4:

A control array is a collection of controls that have the same name and many similar properties.

5:

Why does Visual Basic like to offer a control array when you paste a control on a form that has the source of the paste already on the form?

A5:

Visual Basic assumes that you want to add a similar control, and control arrays often hold similar controls.

6:

Why might you want to lock a Text Box control?

A6:

Lock the text in a text box when you don't want the user to be able to change the text.

7:

What is the proper way to terminate an application?

A7:

Unload the form and then issue the End statement.

8:

What happens when you attempt to divide by zero?

A8:

A runtime error occurs because you cannot divide by zero.

9:

True or false: The following two statements are identical:

lblTitle = "The Sound of Multimedia"

lblTitle.Caption = "The Sound of Multimedia"
A9:

True; Caption is a label's default property. Of course the wording differs, but the statements perform the same action.

10:

What is the difference between a function procedure and a subroutine procedure?

A10:

A function procedure returns a value to the calling procedure, whereas a subroutine procedure never returns a value.

Hour 9 Exercises

1:

Change this lesson's application's form properties so the user cannot resize the form. Search through the form properties until you find the property that will do this.

A1:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 9 Exer 1.

2:

Add input box routines to the application so that the user sees the error message in the input box and can enter the replacement values in the input box. The code then places the input box's value into the form. Once the form gets a replacement value, check the controls again to make sure that the replacement value is correct. You might want to add a Do...While loop around the input box routines so that the user keeps seeing the input box warnings until the user enters a value greater than zero.

A2:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 9 Exer 2.

Hour 10 Quiz

1:

When do you normally initialize a list box?

A1:

At runtime

2:

What method adds new items to a list box?

A2:

The AddItem method

3:

Which method determines the number of items in a list box?

A3:

The ListCount method

4:

True or false: Visual Basic will automatically keep list box items sorted if you set a certain property to True.

A4:

True

5:

How many combo boxes are there?

A5:

3

6:

How do you specify the type of combo box you want to add to an application?

A6:

Specify the Style property to change a combo box style.

7:

True or false: The drop-down list box is one of the Combo Box controls, so users can enter new values in the drop-down list box just as they can other Combo Box controls.

A7:

False; users cannot enter items into a drop-down list box.

8:

What is an array?

A8:

A collection of variables with the same name

9:

What is the highest subscript in a 10-element array if you don't use Option Base 1 and you use element 0?

A9:

9

10:

True or false: A control array exists when two or more controls have the same Name property.

A10:

True

Hour 10 Exercises

1:

Write an application that builds a list as the user enters new values. (Hint: Use a Combo Box control for the list.) The list should hold the user's favorite book titles. As the user enters more and more titles, the list should grow. Add a command button to the form with the caption Add To; when the user clicks the command button, the title just entered goes to the list. Keep the list sorted at all times.

A1:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 10 Exer 1.

2:

Write an application that contains four command buttons. The command buttons should be blue and have boldfaced, italicized captions that read Change Color, Change Bold, Change Height, and Change Width. When the user clicks one of the command buttons, the appropriate property should change inside the Click() event procedure. Use a Select Case statement to determine which property should change based on the event procedure's Index argument.

A2:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 10 Exer 2.

Hour 11 Quiz

1:

True or false: Option button captions always appear to the right of the buttons.

A1:

False

2:

What happens if the user clicks an option button that isn't currently selected?

A2:

The clicked option becomes the selected option button, and the one that was selected no longer is selected.

3:

Why would you gray out a Check Box control?

A3:

The item might be temporarily unavailable.

4:

What happens if the user clicks a check box that isn't currently selected?

A4:

The check box becomes selected, and any others that were already selected are still selected.

5:

True or false: An application can begin with none of its option buttons or check boxes selected.

A5:

True

6:

What kind of control can you place on a frame?

A6:

Any control can go on a frame.

7:

What is the difference between a scrollbar's SmallChange and LargeChange properties?

A7:

The SmallChange property determines the amount of scrolling that takes place when the user clicks the scroll bar arrows; the LargeChange property determines the amount of scrolling that takes place when the user clicks the scroll bar on either side of the scroll thumb.

8:

Which property changes when the user clicks one of the scrollbar's arrows?

A8:

The Value property changes when the user clicks the scrollbar.

9:

True or false: The Timer control works like an alarm clock ready to go off at a preset time of day.

A9:

False; the Timer control triggers events every time a fixed number of milliseconds pass.

10:

True or false: If you need a timer interval greater than approximately 10 seconds, you must use multiple Timer controls.

A10:

False; through programming you can make a single Timer control any timed interval.

Hour 11 Exercises

1:

Create an application that mimics the frame application used with Listing 11.1. Instead of using separate option buttons, use an option button array for each frame's option button, making a total of three option button arrays. Change Listing 11.1 to reduce the number of event procedures in the application. Use a Select Case statement based on the event procedure Index argument to set the appropriate label property.

A1:

The Index value that you use in the Select Case will be the argument passed to the procedure.

2:

Change the application you wrote in exercise 1 so that no frames appear on the form. Remove the Underline and Italic option buttons (keep the Framed Color option buttons) and add these check box controls in their place: Underline and Italic. Change the code so that the text will appear underlined if the user clicks the Underline check box, and the text will be italicized if the user clicks the Italic check box. Both or only one might be checked at any one time.

A2:

The form requires no frame because the user can select multiple check boxes at once, unlike option buttons.

3:

Duplicate this lesson's scrollbar application that lets the user set the label's text size with the scrollbar. Completely remove the scrollbar, however, and add a Timer property. Every second, add 5 to the label's font size. When the font size grows to 70 or more points, send the size back down to 8 and start increasing the size once again.

A3:

Set the Timer control's Interval property to 1000. Use an If at the start of the timer's event procedure so that the label's size grows no larger than 70 points.

Hour 12 Quiz

1:

How many different dialog boxes can the Common Dialog Box control produce?

A1:

6

2:

What are two advantages to using the Common Dialog Box control over your own dialog boxes that you create?

A2:

Programming is easier and your applications are more consistent with each other and with other Windows applications your users already know.

3:

True or false: You can add an ActiveX control to your toolbox.

A3:

True

4:

True or false: Visual Basic includes these five controls that you can add to your toolbox: File Open, File Save, Colors, Font, and Help.

A4:

False; these actions are available all from one control, the Common Dialog Box control.

5:

What property limits the files displayed to a particular set of extensions?

A5:

The Filter property controls the filename extensions that appear.

6:

Name the methods that display all six common dialog boxes.

A6:

ShowColor, ShowFont, ShowHelp, ShowOpen, ShowPrinter, and ShowSave

7:

Why does the following code not display a Font dialog box?

cbdDialog.DialogTitle = "Font"
cbdDialog.ShowFont
A7:

The Flags property is not set.

8:

How do you know which file was selected by the user after a File Save dialog box closes?

A8:

Check the FileName property.

9:

True or false: The Color dialog box limits the user to a few limited color values.

A9:

False

10:

Why does a file not begin printing as soon as the user selects a printer and closes the Printer dialog box?

A10:

The Printer dialog box does no printing but only sets up printing.

Hour 12 Exercises

1:

Press Ctrl+T to display the Components dialog box once again. Search through the controls you can add to your toolbox. Add a few interesting controls and study their properties. You can add as many as you want. You also can delete one of these extra controls from the Toolbox window by pressing Ctrl+T and deselecting it.

A1:

No answer is necessary.

2:

Write a simple Color dialog box application that lets the user display a Color dialog box simply by clicking a command button. When the user selects a color and closes the Color dialog box, set the command button's BackColor property to the user's selected color.

A2:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 12 Exer 2.

3:

Run the application you created in exercise 2 and click the Define Custom Colors button to see how the Color dialog box changes. Aren't common dialog boxes simple to use?

A3:

No answer is necessary.

4:

Display a File Open dialog box (attach the display to a command button so you can display the dialog box when you are ready) that displays, by default, all files in the selected folder that end with .txt and .bat extensions. Set the default folder to your computer's root directory (C:\) and make Autoexec.bat the default filename selected when the user first sees the dialog box.

A4:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, please look for a project named Lesson 12 Exer 4.

Hour 13 Quiz

1:

What are two reasons for writing structured programs?

A1:

Your programs are easier to write and easier to maintain when you write structured code.

2:

True or false: Structured code is useful for getting to code details as fast as possible.

A2:

False; structured programming puts off details.

3:

True or false: You can write your own functions.

A3:

True

4:

What is wrong with the following subroutine declaration?

Public Subroutine DoItSub ()
A4:

The keyword should be Sub.

5:

When is the Call keyword optional in subroutine calling?

A5:

If the procedure uses no arguments, Call is optional.

6:

The following code appears in a form module's general section. Is X a local, module-global, or project-global variable? What about Y? Would your answers be different if this appeared in an external module as opposed to a form module?

Dim X As Integer
Public Y As Integer
A6:

X is module global and Y is project global. The variables would be the same if they appeared in an external module.

7:

What is wrong with the following function declaration?

Public Function DoCalc(intAge As Integer, strCoNames(45) As String)
A7:

Don't include the array subscript.

8:

Why does the called procedure need to know the datatypes for passed values?

A8:

The arguments are local to the calling procedure, so the called procedure needs to know the datatypes being passed.

9:

How does one procedure get local data from a calling procedure?

A9:

By the passed argument list

10:

Which keyword is optional: ByRef or ByVal?

A10:

ByRef is the default argument passing process, so if you want to pass by reference, you don't need to specify the ByRef keyword. If, however, you want to pass by value, you must specify ByVal.

Hour 13 Exercises

1:

Write a general-purpose standard function procedure that accepts a numeric integer argument and returns that argument multiplied by 10.

A1:

Public Function By10 (ByRef intVal As Integer) As Integer
 By10 = intVal * 10
End Function
2:

Write a standard subroutine procedure that accepts three single-precision arguments and displays those three values in labels named lblSng1, lblSng2, and lblSng3.

A2:

Public Sub LblSng (ByVal a1 As Single, _
           ByVal a2 As Single, ByVal a3 As single)
 lblSng1.Caption = a1
 lblSng2.Caption = a2
 lblSng3.Caption = a3
End Sub

Hour 14 Quiz

1:

What preparation must you do before the built-in functions are available to you?

A1:

None; the built-in functions are part of the Visual Basic language.

2:

What is the value stored in each of the following assignment statements?

  1. strA = Left("abcdefg", 3)
  2. strB = Right("abcdefg", 3)
  3. strC = Mid("abcdefg", 2, 3)
  4. strD = Mid("abcdefg", 2)
A2:

  1. abc
  2. efg
  3. bcd
  4. bcdefg
3:

Is the following Mid() a function or a statement?

Mid(strTest, 2, 4) = "abcd"
A3:

A statement

4:

Is the following Mid() a function or a statement?

strAns = Mid(strTest, 2, 4)
A4:

A function

5:

What is the value stored in each of the following assignment statements?

  1. intA = Int(20.34)
  2. intB = CInt(20.34)
  3. intC = Fix(-2.8)
  4. intD = Int(-2.8)
A5:

  1. 20
  2. 20
  3. -2
  4. -3
6:

What value appears in varAns after the following assignment?

varAns = Val(LTrim(Str("10")))
A6:

10

7:

What is the 24-hour time for 12:56 p.m.?

A7:

12:56

8:

What is a thousands separator?

A8:

A thousands separator is used at each thousand's place in a number. For example, a comma appears as this number's thousand's separator: 45,419.12.

9:

True or false: You must use Format() to properly format date and time values because the built-in date and time functions cannot interpret your International settings.

A9:

False; use Format() to display your dates in the format you want them to appear.

10:

True or false: Now returns information for both the current date and the current time.

A10:

True

Hour 14 Exercises

1:

Write a program that stores the 256 ASCII characters (from ASCII 0 to ASCII 255) in a string array that's defined to hold 256 characters.

A1:

Dim strASCII As String(256)
For intCtr = 0 To 255
 StrASCII(intCtr) = Chr(intCtr)
Next intCtr
2:

Write a subroutine procedure that asks the user for the time that he clocked into work and then for the time he clocked out. Display, in three labels, the total number of seconds worked, the total number of minutes worked, and the total number of hours worked.

A2:

Public Sub getTime()
 Dim dteTimeIn As Date
 Dim dteTimeOut As Date
 Dim lngSec As Long
 Dim intMin As Integer
 Dim intHours As Integer
 dteTimeIn = InputBox("What time did you check in?", "Check In")
 dteTimeOut = InputBox("What time did you check out?", "Check Out")
 lngSec = DateDiff("s", dteTimeIn, dteTimeOut)
 intMin = DateDiff("n", dteTimeIn, dteTimeOut)
 intHours = DateDiff("h", dteTimeIn, dteTimeOut)
 lblSecWorked.Caption = lngSec
 lblMinWorked.Caption = intMin
 lblHoursWorked.Caption = intMin/60
End Sub
3:

Write a subroutine procedure that asks the user for her birthday. If the user entered a valid date (check to make sure and keep asking if the user did not enter a date), display a message box telling the user how many years until she reaches the retirement age of 65. If the user is older than 65, congratulate her on a long life!

A3:

'You may want to add input validation to ensure
'that the user enters a reasonable age.
Public Sub getTime()
 Dim dteBirth As Date
 Dim intYrsRetire As Integer
 Dim intAge As Integer
 Dim Msg As Single
 Do
    dteBirth = InputBox("When were you born?", "Birthday")
 Loop Until IsDate(dteBirth)
 intAge = DateDiff("yyyy", dteBirth, Now)
 If intAge >= 65 Then
    Msg = MsgBox("Congratulations on a long life!" _
          , vbExclamation, "It's great!")
 Else
    intYrsRetire = 65 - intAge
    Msg = MsgBox("You have only " & intYrsRetire & _
          " years to retire!", vbExclamation, "Soon...!")
 End If
End Sub

Hour 15 Quiz

1:

What is the difference between a file and a database?

A1:

A database is an organized collection of one or more tables of records.

2:

What is the difference between a record and a field?

A2:

A record is a row, and a field is a column from a file.

3:

What is the difference between a table and a file?

A3:

A table is a file in a relational database.

4:

What happens if you open an existing file in Output mode?

A4:

The file is overwritten.

5:

What happens when you write to a file in Append mode?

A5:

The file is added to.

6:

Which files does the following statement close?

Close
A6:

All open files

7:

True or false: Your form must contain a different Data control for every table in the database.

A7:

False; only for the tables you want to work with

8:

What advantage does a bound text box provide for the programmer who wants to write an application that lets the user update a database field?

A8:

If the user changes the bound control, the underlying database value changes also.

9:

A database field is a Yes/No Access data field that can only take one of two values. Which Visual Basic control would best serve to represent that field?

A9:

An option button or a check box

10:

What's the simplest way to create a form based on a database?

A10:

Use the VB Data Form Wizard

Hour 15 Exercises

1:

Write an application that stores the names of your five favorite friends, their ages, and their phone numbers in five records in a disk file. Use Write # to write each three-value record and Input # to read each record. Call the data-entry and file-writing procedures from one procedure and the file-reading and display procedure from another procedure.

A1:

Public Sub WriteValues()
 Open "c:\friends.dat" For Output As #1
 Write #1, "George", 35, "912-3344"
 Write #1, "Elaine", 31, "649-1999"
 Write #1, "Jerry", 34, "912-5712"
 Write #1, "Kramer", 38, "747-1123"
 Write #1, "Newman", 32, "648-2900"
 Close #1
End Sub
Public Sub ReadValues()
 Dim strName(6) As String
 Dim intAge(6) As Integer
 Dim strPhone(6) As String
 Open "c:\friends.dat" For Input As #1
 For intCtr = 1 to 5
    Input #1, strName(intCtr), intAge(intCtr),_
          StrPhone(intCtr)
 Next intCtr
 Close #1
 ' Code goes here that processes data
End Sub
2:

Write a database application that displays the Biblio.mdb's book title table's first three fields. Don't let the user update the fields; you must be careful what kind of controls you use to display the field data.

A2:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, look for a project named Lesson 15 Exer 2.

3:

Use the Data Form Wizard to generate a table based on the entire Biblio.mdb's book title table. Connect the generated form to a command button's Click() event on the main form so that the data form appears for the user when the user clicks the command button.

A3:

No answer is available.

Hour 16 Quiz

1:

Why does Visual Basic printer output not go immediately to a printer?

A1:

The output goes to the Windows print manager.

2:

What happens if the printer isn't online when the user prints something?

A2:

An error message box appears.

3:

What is the difference between the Printer object and the Print method?

A3:

The Printer object receives data sent by the Print method.

4:

How can you specify the number of output copies to print?

A4:

Specify the Copies property.

5:

True or false: You can add the Printer object to the toolbox.

A5:

False

6:

How many spaces does a print zone contain?

A6:

14

7:

Why do you sometimes need to use the ASCII-based Chr() function when printing?

A7:

Use Chr() to print special characters on the printer.

8:

What's the output from the following code?

Printer.Print "1";
Printer.Print "2"
A8:

12

9:

True or false: Using Tab(14) after each variable does the same thing as putting a comma after each variable printed.

A9:

False

10:

True or false: You can apply the Print method to a form.

A10:

True

Hour 16 Exercises

1:

Write the Print method that prints a Spanish Ñ (with a tilde) on the printer.

A1:

Printer.Print Chr(165)

2:

Write a program that prints ASCII values 32 through 255 on paper when the user clicks a command button.

A2:

For intCtr = 32 to 255
 Printer.Print Chr(intCtr)
Next intCtr
3:

Modify the book publisher application from Hour 15, "Visual Basic Database Basics," to print on paper the current book's title and year when the user clicks a Print command button.

A3:

See the project named Lesson 16 Exer 3 on the CD-ROM.

Hour 17 Quiz

1:

True or false: Menu items are controls that have properties.

A1:

True

2:

True or false: More than one form can have a menu within a single application.

A2:

True

3:

What is the most common menu-naming prefix?

A3:

mnu

4:

What would be a good name for an Edit | Select | All menu option?

A4:

mnuEditSelectAll

5:

How does the Menu Editor know that a submenu option is part of a menu bar option?

A5:

The indentation determines the menu level.

6:

True or false: You can add a menu shortcut keystroke to a menu option that produces a submenu.

A6:

False

7:

What menu option should all applications use?

A7:

File|Exit

8:

Why should programmers shy away from using unconventional menu options such as File | Quit?

A8:

The user will not adapt as quickly to your application.

9:

What is the event property used in menu processing?

A9:

Click

10:

Which property must your application change in order to change the check mark setting on a menu option?

A10:

The Checked property

Hour 17 Exercises

1:

Create a new project with the following menu bar items: Write, Read, and Listen. Create a Write submenu with these options: Keyboard, Pencil, and Pen. Create a Read submenu with these options: Screen, Book, and Magazine. Create a Listen submenu with these options: Radio and Television.

A1:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, look for a project named Lesson 17 Exer 1.

2:

Add menus to the Atm.vbp project that appears in VB's samples folder. On the opening form, add a File | Exit option as well as a Language menu bar option with these pull-down checked choices: English, Italiano, Espanol, Francais, and Deutsch. Don't use special foreign characters unless you can access them easily from your keyboard, and you are used to using them. When the user first starts the application, put the check mark next to the English option but move the check mark (or let the user select a different option) when the user selects an option or clicks the corresponding command button. Add one more menu to the Welcome form that includes a File | Exit option. Unlike the Welcome form's OK button, make sure the menu's File | Exit command on that form completely terminates the application.

A2:

The answer is not available, so that copyright information can be maintained in Visual Basic's prepackaged Atm example.

Hour 18 Quiz

1:

Which two controls display graphic images?

A1:

The Picture Box and Image controls display graphic images.

2:

Which control is more efficient?

A2:

The Image control is more efficient than the Picture Box control.

3:

What happens if you load a picture into an Image control and the Image control is too small to hold the entire picture (assume default property values)?

A3:

The Image control will shrink to measure the same size as the loaded picture.

4:

What happens if you load a picture into a Picture Box control and the Picture Box control is too small to hold the entire picture (assume default property values)?

A4:

The Picture Box control doesn't resize to measure the same size as the loaded graphic image.

5:

What happens if you load a picture into an Image control and the Image control is larger than the picture (assume default property values)?

A5:

The Image control enlarges to display the entire image.

6:

What happens if you load a picture into a Picture Box control and the Picture Box control is larger than the picture (assume default property values)?

A6:

The Picture Box control doesn't enlarge to display the full image, but rather, clips the image.

7:

What is wrong with this assignment (assume that the filename and pathname are correct)?

imgFace.Picture = "C:\DataPics\Flower.Ico"
A7:

The assignment is missing the LoadPicture() function.

8:

Which control helps control animation effects?

A8:

The Timer control helps you control animation effects.

9:

True or false: A static variable is a global variable because its value doesn't change from a procedure's termination to the same procedure's next execution.

A9:

False; only local variables can be static.

10:

When does a static variable first get initialized?

A10:

Visual Basic initializes static variables at compile time, so the first time through the procedure, the variable has an initial value.

Hour 18 Exercises

1:

Add a command button to the animation application so that the animation doesn't begin until you click the button. The solution to this exercise might not be obvious at first. (Hint: Consider activating the Timer control in the command button's event procedure.) Put a happy face on the command button and hide the command button so it disappears when the application starts animating the happy face.

A1:

Set the Timer control's Enabled property to False in the Properties window. In the Click event procedure, set the Enabled property to True to begin the animation. Be sure to set the command button's Style property to Graphical before putting the happy face on the button.

2:

Change the animation application so that the happy face bounces off all four sides of the Form window.

A2:

No answer is available.

Hour 19 Quiz

1:

Why must you open the project's Components dialog box before using the Toolbar control?

A1:

The Toolbar control doesn't appear as an intrinsic control on the Toolbox window.

2:

Which control works with the toolbar to produce icon images on the buttons?

A2:

The Image List control

3:

What does the Image control's Key method do?

A3:

The Key property determines which string returns when a toolbar button is clicked.

4:

Why does the toolbar's event procedure use an argument?

A4:

The argument value determines which toolbar button is clicked.

5:

How many shapes can the Shape control produce?

A5:

7

6:

Which property determines the pattern of drawn lines?

A6:

BorderStyle

7:

True or false: You should use the LoadPicture() function to initialize or change the value displayed with the Shape control.

A7:

False; LoadPicture() is for the Picture Box and Image controls.

8:

What is the FillStyle property used for?

A8:

The FillStyle property determines the interior pattern of shapes.

9:

True or false: You can change a shape's interior and exterior color.

A9:

True. You can set the shape's border and interior colors.

Hour 19 Exercises

1:

Write an application that includes a large Shape control (originally placed as a square) in the middle of the form. Add a command button that reads Change Shape. Every time the user clicks the command button, change the shape to something different.

A1:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, look for a project named Lesson 19 Exer 1.

2:

Create a Form window that contains a rectangle with a blue border, red diagonal lines, and a green interior.

A2:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, look for a project named Lesson 19 Exer 2.

3:

Write an application that draws a yellow happy face in the center of the form. Don't use a graphic image file. Add two toolbar buttons, one with a happy face icon from an icon file and one with a sad face icon. When the user clicks the happy face toolbar button, wink the happy face's eye. When the user clicks the sad face toolbar button, draw a tear coming out of one eye.

A3:

You can retrieve the code from the CD-ROM that accompanies this book. After you retrieve the source code, look for a project named Lesson 19 Exer 3.

Hour 20 Quiz

1:

Which errors are the easiest to find?

A1:

Syntax errors are the easiest to find.

2:

Which errors are the hardest to find?

A2:

Logic errors are the hardest to find.

3:

If you write a program and, during execution, the program halts and displays an error message telling you that a disk drive doesn't exist, what kind of error just occurred?

A3:

A runtime error occurred.

4:

How can you tell the current program mode?

A4:

Visual Basic's title bar tells the current mode.

5:

What is a breakpoint?

A5:

A stopping point during a program's execution where all variables are kept active.

6:

What are three things you can do at a breakpoint?

A6:

Look at the variables, add variables to the Watches window, single-step through the rest of the program, add other breakpoints, remove breakpoints, or terminate the execution.

7:

How do you single-step through a program?

A7:

Click the Debug toolbar's Step Through button at any breakpoint. You can also press F8 to step into the next line or press Shift+F8 to step over the line.

8:

True or false: While using the debugger, you have no access to your program's Output window.

A8:

False

9:

What's the quickest way to see a variable's value at a breakpoint?

A9:

Click the Quick Watch button or rest the mouse pointer over the variable to see its value.

10:

What method displays values in the Immediate window?

A10:

The Print method displays values in the Immediate window.

Hour 20 Exercises

1:

Larry the Visual Basic programmer wants to send values to the Immediate window right before he reads a disk file. Here are some statements Larry uses to print to the Immediate window:

Print lblFileName.Caption
Print intNumRecs, intNumFields
Print strCompName

Larry isn't having success. Instead of the Immediate window, these values all seem to appear on the form itself! Help determine what Larry is doing wrong so he can view the values in the Immediate window.

A1:

Larry is not printing to the Debug object.

2:

Load the Controls.vbp sample project. Single-step through the project beginning at a breakpoint that you set in Form_Load()'s last statement. The program uses data in a different way than normal because the program uses a resource file to hold its strings. By moving all its data out to a resource file, the strings can be easily translated to other languages, and the program only needs to be recompiled. Without a resource file, a programmer would have to search the Code window for all strings and possibly miss some. Use the single-step mode to learn how the resource file and its related built-in functions operate.

A2:

No answer is necessary.

Hour 21 Quiz

1:

Which came first: OCX controls, ActiveX controls, or VB custom controls?

A1:

VB custom controls

2:

How do you add ActiveX controls to your Toolbox window?

A2:

Use the Components dialog box.

3:

Where can you get additional ActiveX controls?

A3:

From the Internet and online services such as CompuServe, and you can also write your own

4:

True or false: Programmers in other languages, such as Visual C++, can use ActiveX controls created by VB 6.

A4:

True

5:

If you design an ActiveX control that works and looks somewhat like an existing control, what can you do to speed the development of the new control?

A5:

Use the existing similar control as a basis for the new ActiveX control.

6:

What is the difference between an inserted object and an embedded object?

A6:

Nothing

7:

How does the user of an application with an embedded OLE object activate that object for editing?

A7:

The user can double-click the embedded object or its icon.

8:

What is the difference between an ActiveX document and an ActiveX container?

A8:

An ActiveX document is an application that an ActiveX container is capable of displaying during execution as a child process.

9:

What is an example of an ActiveX container that many Visual Basic programmers use already?

A9:

Internet Explorer is an ActiveX container.

10:

What must you do to convert a form to an ActiveX document?

A10:

Run the VB 6 ActiveX Document Migration Wizard to convert a form to an ActiveX document.

Hour 21 Exercises

1:

Create a new project and add two OLE controls to the form. Place a linked Word document in one and place an embedded Word document in another. Run the application, double-click each object, and manage the objects from within the application to see the effects. Start Word and modify the object, and then rerun your application to see the change reflected in one of the objects. (If you don't use Word, you can use WordPad or another word processor as long as that word processor appears inside the Insert Object dialog box.)

A1:

No answer is available, so that copyright information can be maintained for Word and WordPad.

2:

In this lesson's final section you saw Hour 18's animation form converted to an ActiveX document. Run the VB ActiveX Document Migration Wizard to do the same for another single form application created in a previous chapter and display the created ActiveX document in Internet Explorer.

A2:

No answer is needed. Run the wizard to convert the application to an ActiveX document.

Hour 22 Quiz

1:

What are three system objects?

A1:

Form, Printer, and Screen

2:

What is the difference between a class and an object?

A2:

A class defines objects, and an object is a single instance of a class.

3:

What happens when you use the New keyword inside an object declaration?

A3:

Visual Basic creates a new object.

4:

True or false: TypeOf is both a statement and a function.

A4:

True

5:

True or false: You can pass objects such as controls and forms as arguments to procedures.

A5:

True

6:

What is the index value for a collection's first item?

A6:

One

7:

How can you insert a new item at the beginning of a collection?

A7:

Use the Before named argument to insert items at the beginning of a collection.

8:

Which OLE automation function should you use to initiate OLE automation when the OLE automation application is already running on the machine?

A8:

GetObject()

9:

What is the new term being used more frequently for OLE automation?

A9:

Active Automation

10:

True or false: As long as you know the OLE automation language, you don't need the OLE automation application installed on your machine to use OLE automation with that application.

A10:

False

Hour 22 Exercises

1:

Write a procedure that decreases the font size of all controls on all forms by 50%. Use a system object to accomplish the change.

A1:

Public Sub FontDecrease()
 For intCtr = 0 to Controls.Count - 1
  Controls(intCtr).Font.Size = Controls(intCtr).Font.Size _
                    /2
 Next intCtr
End Sub
2:

If you use Word, Excel, or any other OLE automation—compatible application (as all the Office 97 products are), start that application and search the online help for information on that application's properties, events, and methods used in OLE automation. The more you know about that application's internals, the more easily you can integrate that application and borrow its power for your own applications. If the application is an Office 97 application, search the online help for the Visual Basic help to see how to start Visual Basic. (Visual Basic is often called Visual Basic for Applications in application help files. Visual Basic for Applications [or VBA] is the same language as Visual Basic.) Start the application's Visual Basic editor to see a development environment that looks like Visual Basic's own development environment. Open the application's Object Browser to receive an Explorer-like view of that application's properties, events, and methods. Search the Object Browser's online help for extensive OLE automation help.

A2:

No answer is necessary.

Hour 23 Quiz

1:

What are three advantages to compiling an application?

A1:

The program loads and runs faster, is more secure, and doesn't require Visual Basic's Development Environment to run.

2:

True or false: You must run the File | Compile option to compile the program.

A2:

False; use the Make option.

3:

Why might you use version control?

A3:

To maintain records of multiple versions of an application

4:

How do you designate an icon for an application?

A4:

Use the form's icon located in the form's Icon property.

5:

True or false: You can create the installation routine from Visual Basic's development environment.

A5:

False; you must run the installation routine from the Start menu.

6:

What is a dependency file?

A6:

A dependency file is a file needed by an application's installation to install and execute properly. The dependency file specifies the names of all the files the application requires, such as certain fonts, controls, and ActiveX files.

7:

True or false: The Package And Deployment Wizard can create an installation routine for the Internet.

A7:

True

8:

Why does the Package And Deployment Wizard offer two ways to store the setup routine on your hard disk (in a single folder or multiple folders)?

A8:

In case you want to copy the installation to floppy disks

9:

What happens if the installation routine finds a newer version of a file it's about to install?

A9:

The installation routine gives the user a chance to save the newer version.

10:

How does the user uninstall the installed application?

A10:

The user can run the uninstall routine from the Control Panel.

Hour 23 Exercises

Q1:

Change the form icon from Hour 18, "The Graphic Image Controls," to one of the happy face icons used in the project. Compile the project and make sure that the compiled application uses the icon for the project icon. Create an installation routine for Hour 18's animation application. Install the application using the setup routine. Run the application (look in the Program Files folder for the application) to make sure everything works well. If you need to change something, re-run the Package And Deployment Wizard. After you create a successful installation, open the Control Panel and uninstall the application.

A1:

No answer is necessary; no source code changes from the original application.

Hour 24 Quiz

1:

Which has the broader scope: an intranet or the Internet?

A1:

The Internet

2:

Which online connection—an Internet, an intranet, or both—can the VB Application Wizard support?

A2:

The VB Application Wizard supports both.

3:

True or false: The computer on which you develop your Visual Basic application with Internet support must have Internet Explorer 3.0 or later.

A3:

True (Internet Explorer 4.0 or better is highly recommended)

4:

True or false: The computer on which your application executes must have Internet Explorer 3.0 or later.

A4:

True (Internet Explorer 4.0 or better is highly recommended)

5:

What is an ISP used for?

A5:

An ISP is used to gain Internet access.

6:

True or false: The computer on which you develop your Visual Basic application with Internet support must have an ISP.

A6:

Only if you want to test the Internet-based application do you need an ISP. In most cases, you should have an ISP.

7:

What makes a Java-enabled Web page different from one that has no Java code?

A7:

Java-enabled Web pages are more active than non-Java pages. For example, a Java-enabled Web page can contain a multimedia video clip that runs on the user's own computer. The Java application's runtime speed isn't dependent on download speed because a Java application executes on the end user's machine.

8:

Why are extra support tools needed when an application already uses the Web Browser tool from the Toolbox window?

A8:

The Web Browser tool requires a toolbar and an Image List.

9:

True or false: The VB Application Wizard generates a Web browser that you can use to log on to the Internet's Web pages. When you finish viewing the Web pages, you can click a button to log off the browser but remain inside the generated application's code.

A9:

False; you must add a logoff routine.

10:

What are three ways to display a Web page that you previously displayed from the Web browser in the same session?

A10:

Click the Back button, display the pull-down URL list box, type the URL address in the text box, or select the Open dialog box and type the URL of the page you just browsed.

Hour 24 Exercise

Q1:

Use File | Print to print all the Web browser form's code. You'll see that the wizard generated a lot of code and that some of the code is fairly tricky. By studying the code, you'll see that these Internet controls can be difficult to program and that the wizard takes much of that difficult task off your shoulders.

A1:

No answer is necessary as this exercise simply directs a process.

+ Share This