Input Dialog Box
An input dialog box can be used to obtain a single item of input from the user
Presents a window (dialog box) requesting input
Syntax: stringVar = InputBox(prompt, title)
Example of an Input Dialog BoxPrivate Sub cmdDisplay_Click()
Dim fileName As String, prompt As String, title As String
Dim houseNumber As Single, street As String
prompt = "Enter the name of the file containing the information."
title = "Name of File"
fileName = InputBox(prompt, title)
Open fileName For Input As #1
Input #1, houseNumber
Input #1, street
picAddress.Print "The White House is at"; houseNumber; street
Close #1
End Sub
After executing, an inputdialog box will pop up
Using Message Dialog Box for Output The message dialog box is used to
present a pop-up window containing information for the user
Syntax: MsgBox prompt, , title
Example of a Message Dialog Box
MsgBox “Nice try, but no cigar”, , “Consolation”
Stays on thescreen until the user presses OK
Formatting the Output:
Create easily readable output In the Print method, the spacing of the
output is controlled by the following devices:semicoloncommaTab function
Semicolons
The next value output is placed in the next column position.
Example:
picOutput.Print “Patrick”; ”Jon”
Output:
PatrickJon
Example of Semicolon
picOutput.Print “Patrick”; “ Jon”
Output Screen:
Patrick Jon
Space here
Space here
Example of Semicolon
picOutput.Print 100; -200; 300
Output Screen:
100 -200 300
One space
Two spaces
Commas
A comma in a Print method causes the next value output to be placed in the next available print zone.
Each print zone is 14 positions wide.
Using Commas
Example:
picOutput.Print “SEE”, ”YOU”, ”SOON”
Output Screen:
SEE YOU SOON
Column 1
Column 15
Column 29
Using Commas
A print zone can be skipped by typing consecutive commas
Example:
picOutput.Print “HOURLY”, , “PAY”
Output Screen:
HOURLY PAYColumn 29
Tab Function
Specifies the column where output will start
Use only semicolons with the Tab function Can only be used to advance the print
position (cannot move backwards)
Example of Tab Function
Example: picOutput.Print Tab(3); “Hi there!” ; Tab(25) ;“Bye!”
Output Screen:
Hi there! Bye!
Column 3
Column 25
Built-In Functions
Take one or more input values and return an output value
A means provided by Visual Basic for carrying out small, common tasks
Types of Built-In functionsNumeric functions (manipulate numbers)String functions (manipulate strings)
Numeric Functions
Rnd Returns a number between 0 and 1 (excluding 1)
Sqr(n) Returns the square root of the number n
Round(n,r) Returns the number n rounded to r decimal places
Int(n) Returns the greatest integer less than or equal to the number n
Example of Numeric Functions
Private Sub cmdEvaluate_Click() Dim n As Single, root As Single
n = 6.76 root = Sqr(n) picResults.Print root; Int(n); Round(n,1)End Sub
Output: 2.6 6 6.8
Commonly-Used String Functions
Function: Left(“Penguin”, 4)
Purpose: Returns the number of characters specified, starting at the beginning of the string
Commonly-Used String Functions
Function: Right(“Cork City”, 4)
Purpose: Returns the number of characters specified from the end of the string
Commonly-Used String Functions
Function: Mid(“Commissioner”, 4, 3)
Purpose: Returns the substring starting at the position indicated by the first number and continuing for the length specified by the second number
Commonly-Used String Functions
Function: UCase(“Yes”)
Purpose: Converts any lowercase letters in a string to uppercase
String-Related Numeric Functions
Function: InStr(“John Smith”, “m”)
Purpose: Searches for the first occurrence of one string in another and gives the position at which the string is found
String-Related Numeric Function
Function: Len(“John Smith”)
Purpose: Returns the number of characters in the string.
Format Functions
The format functions provide detailed control of how numbers, dates, and strings are displayed.
Examples FormatNumber (12345.678, 1) 12,345.6 FormatCurrency (12345.678, 2) $12,345.68 FormatPercent (.185, 2) 18.50% FormatNumber (1 + Sqr(2), 3) 2.414
Format Function
Format (expr, “@……..@”)
Purpose: The value of this function is the value of expr right justified in a field of n spaces, where n is the number of @ symbols.
Format Examples
Format(12345, “@@@@@”) 12345
Format(123, “@@@@@”) 123
Format(“123.4”, “@@@@@”) 123.4
FormatDateTime Example
FormatDateTime (“9-15-04”, vbLongDate)
Output: Monday, September 15, 2004
Rnd Function
Returns a random number from 0 to 1.
(excluding 1).
Example:
picBox.Print Int(6 * Rnd) + 1
Output: Displays a random integer from 1 through 6.
Top Related