Professional Documents
Culture Documents
CHAPTER 6 - List, Loops and Printing
CHAPTER 6 - List, Loops and Printing
Do
(Code to execute)
Loop
Dim number As Long
number = 0
Do
number = number + 1
Print number
Loop
Results: continually generate and print out the next number
infinitely
Exit Do
So we clearly need some way to escape from the Do-
Loop. You could, of course, simply End the program once
you have calculated enough values, but what if you still
need to perform tasks after you're done calculating? The
answer is to use the Exit Do statement. Whenever your
program reaches an Exit Do statement within a loop, it
will exit the current loop.
Dim number As Long
number = 0
cnt = 1 Results:
Do 1
number = number + 1 2
3
Print number 4
5
If cnt >= 8 Then 6
7
Exit Do
8
Else
cnt = cnt + 1
End If
Loop
Do-Until/Loop
Do Until
(Code to execute)
Loop
Do While
(Code to execute)
Loop
For x = 1 To 5 Results:
Print x ; 12345
Next
Results:
1
For x = 1 To 5 Step 2 3
Print x 5
Next
Results:
For x = 5 To 1 Step -2 5
Print x 3
1
Next
Exit For
For intLoopIndex = 1 To 10
If Text1.Text = “” Then
MsgBox “You must enter something”
Exit For
End If
Next intLoopIndex
MsgBox Function
Display a dialog box with more than one button
The functions returns a value to indicate which button was
pressed.
Dim intResponse As Integer
intResponse = MsgBox("Clear the coffee list?", vbYesNo +
vbQuestion, "Title")
If intResponse = vbYes Then
Combo1.Clear
End If
Important button and icon values:
Button to Display Intrinsic Constant
OK vbOKOnly
OK and Cancel vbOKCancel
Abort, Retry, Ignore vbAbortRetryIgnore
Yes, No, Cancel vbYesNoCancel
Yes and No vbYesNo
Retry and Cancel vbRetryCancel
Important message icons and their intrinsic constants
Icons to DisplayIntrinsic Constant
Critical Message vbCritical
Warning Query vbQuestion
Warning Message vbExclamation
Information Message vbInformation
http://www.vb6.us/tutorials/vb6-string-functions
Left(txtName.Text, 3) is Hel
(returns first 3 characters)
Right(txtName.Text, 3) is rld
Hello World
Print Method
To print text in a form, on the printer object or the debug
window
http://www.informit.com/library/content.aspx?b=STY_V
B6_24hours&seqNum=169
Printer.print – output added to the printer object
Methhod: EndDoc, NewPage – job terminates, send
content in printer object to printer.
Formatting lines
The output page has preset tab settings with 5 columns per
Line (print zone) Use a comma to advance the output to
the next print zone. Eg: Printer.Print 1, 2, 3, 4, 5
Output:
1 2 3 4 5
Use a semicolons to separate items without advancing to
the next print zone. Eg: Printer.Print “Name: “; Text1.Text
Output: Name: Hatie
Trailing commas and semicolons – continue on the same
line without advancing the line
Eg: Printer.Print “ Harimau”,
Printer.print “Malaya”
Output: Harimau Malaya
Printer.Print – print a blank lines
Tab function – to specify the column position where you
want the output to appear. Eg: Printer.Print Tab (20);
“Hello Guys”
Output: Hello Guys
Spc function – to specify the number of spaces on the line
that you want to advance from the last item printed. Eg:
Printer.Print Tab(20); “Name”; Spc(5); “Phone”; Spc(5);
“Address”
Output: Name Phone Address
Aligning String and Numeric data
String values: print with no extra spacing between lines