Contents
Chapter |
Description |
Page number |
1 |
Introduction to Visual Basic |
2 |
2 2.1 2.2 |
Background of Visual Basic Visual Basic history Features of Visual Basic |
4 4 |
3 |
Working with Visual Basic window |
5 |
4 4.1 4.2 4.3 |
Analyzing Visual Basic data Declaring variables Declaring strings Visual Basic’s operation |
7 7 7 |
5 5.1 5.2 5.3 |
Control flow statement If…Then If…Then…Else SelectCase |
9 10 10 |
6 6.1 6.2 6.3 6.4 |
Loop structures Do…loop statement While…wend For….Next statement Nesting For loop |
12 13 14 14 |
7 7.1 7.2 7.3 |
Advance keyboard and screen support The MgsBox () function The InputBox () function About check boxes |
16 16 17 |
8 |
Database in visual basic |
18 |
9 |
Package and deployment wizard |
19 |
10 |
Programming for spur gear design |
20 |
11 |
References |
31 |
CHAPTER-1
INTRODUCTION TO VISUAL BASIC
Several years ago, when Visual Basic was a "Small" language, a mastering book would have covered every aspects of it. Since it is first release, however, Visual Basic has evolved into a major development environment that covers every aspect of programming, from educational applications to database, and from financial application to interne components.
Visual Basic is an enjoyable language due to its visual environment. Much of building a Windows program in Visual Basic requires dragging and dropping graphic object onto the screen from a toolbox, which house those objects. Your Windows program appears before your eyes as you add the objects. Visual Basic is one of the first programming languages to incorporate a true WYSIWYG (what you see is what you get) environment.
Visual basic has evolved from the original BASIC language, which is very widely used language. As the names suggest, programming with visual basic is accomplished visually. The method to create the graphical user interface (GUI) is the visual part. Existing objects are dragged and dropped into place instead of writing numerous line of code. While writing the program, you are able to see how the program will look during run time; this is a great advantage over other programming languages.
The goal for teaching Visual Basic to newcomers at times is challenging due to today's broad range of computing skill. Some move to Visual Basic after programming in more advanced programming languages such as C++, other comes to Visual Basic with only a QBasic background. QBasic is a language supplied with PCs for year, but offers only slow; text- based MS-DOS programming environment.
In today’s fast – changing world, program maintenance is more critical than ever before. Company’s change, industries consolidate, spin-offs happen. The computer programs of today must be fluid and maintainable so that programmer can quickly change the program to meet the need of a changing environment in which the programs are used. A program is written ones but updates many times.
Visual Basic provides an environment conducive to rapid application development (RAD), which is object-base3d and also has a set of debugging tools. This makes it a powerful development platform that can be used to create application for the Windows98 and Windows NT operating system quickly and easily.
CHAPTER-2
BACKGROUND OF VISUAL BASIC
2.1 VISUAL BASIC HISTORY:
Programming languages is a set of command and command option, called arguments that you use to give instruction to the computer. Computers con not understands human languages because people deal well with ambiguous command and computer cannot understand such ambiguity. A programming language must be more precise than a spoken language.
By understanding the background of Visual Basic, you will gain insight into Visual Basic and you will be better equipped to use Visual Basic. Microsoft based Visual Basic on programming languages written for beginners called BASIC. BASIC has been around for more than 35 years in from or another. The original languages could use. With BASIC, new programming could become proficient right away. Other programming languages of the day, such as COBOL, FORTRAN, and Assembler, required much study than BASIC before one could use them effectively.
The programming languages are often comprised of several that interact with one another, so you will often see the term application used as a synonym for all of a file of a program.
2.2 FEATURES OF VISUAL BASIC:
CHAPTER-3
WORKING WITH VISUAL BASIC WINDOW
3.1 LAYOUT OF VISUAL BASIC WINDOWS:
3.2 DIAGRAM OF LAYOUT OF VISUAL BASIC:
CHPTER-4
ANALYZING VISUAL BASIC DATA
WORKING WITH VARIABLES:
A variable is temporary named storage area inside our program memory that holds data. Variables hold intermediate calculation and values that you assign to load from controls on the form. Before using a variable is to hold variable by telling Visual Basic the name and data type that the variable is to hold.
4.1 DECLARING VARIABLES:
Before we can use a variable, we must first declare that variable with a Dim statement.
Example: Dim VarName As DataType
VarName is the name we assign to the variable, and DataType is one of the data types listed below table no.1.
CARE MUST TAKE WHEN DECLARING VARIABLES:
4.2 DECLARING STRINGS:
Strings pose an extra problem when we declare them because of the string data type works for two kinds of strings. Variable and fixed length strings.
EXAMPLE: Dim strCityName As String (variable string)
Dim strStateName As String*50 (fixed string)
4.3 VISUAL BASIC’S OPERATORS:
Visual Basic supports numerous mathematical and string operator. Table no.2 listed the most common operators. We use these operators in expression when calculating and working with data.
Table no-1
VISUAL BASIC SUPPORTS SEVEN NUMERIC DATA TYPE
TYPE |
STORAGE |
RANGE |
Byte |
1 byte |
0 to 255 |
Integer |
2 bytes |
-32,768 to 32,767 |
Long |
4 bytes |
-2,147,483,648 to 2,147,483,647 |
Single |
4 bytes |
-3.402823E+38 to –1.401298E-45 for –ve values. |
Double |
8 bytes |
-1.79769313486232E+308 to –4.94065645841247E-324 for negative values. |
Currency |
8 bytes |
-922,337,203,685,477.5808 to 922,337,203,685,477.5807 |
Decimal |
12 bytes |
+/-79,228,162,514,264,337,593,543,950,335 if we use no decimal; with up 28 decimal places +/-79228162514264337593543950335 |
Table no-2
OPERATORS PERFORM CALCULATIONS AND MANIPULATE DATA.
OPERATOR |
MEANING |
EXAMPLE |
RESULT |
^ |
Exponentiation |
2^3 |
8 |
* |
Multiplication |
2*3 |
6 |
/ |
Division |
6/2 |
3 |
+ |
Addition |
2+3 |
6 |
- |
Subtraction |
6-3 |
3 |
CHAPTER-5
CONTROLLING PROGRAMS
What makes programming languages flexible and capable of handling every situation and programming challenge with a relatively small set of commands is the capability to examine external conditions and act accordingly. Programs aren’t monolithic sets of commands that carry out the same calculations every time they are executed.
An application needs a built-in capability to test a different course of action depending on the outcome of the test. Visual Basic provides three control flow, or decision, structures.
5.1 If…Then
The If…..Then structure tests the condition specified, and if it’s True, executed the statement(s) that follows.
General form of If….Then
If condition Then Statement
Visual basic evaluates the condition, and if it’s True, executes the statement that follows.
We can also executed multiple statement by separating them with a colon :
If condition Then Statement: Statement: Statement: Statement:
Example:
If Month(date) = 1 Then
Year = Year + 1
End If
5.2 If……...Then……Else
A variation of the If……Then statements the If …Then…..Else statement, which executes one block of statements if the condition is True and another if the condition is False. The syntax of the If……Then…..Else statement is as follows.
If condition Then
Statementblock-1
Else
Statementblock-2
End If
5.3 Select Case
The select case structure tests a single expression, which is evaluated once at the top of the structure. The result of the tests is then compared with several values, and if it matches one of them, the corresponding block of statements is executed. Here’s the syntax of the select Case statement:
General form of Select Case
Select Case expression
Case value1
Statementblock-1
Case value2
Statementblock-2
……
……
……
Case Else
Statementblock
End Select
A practical example based on the Select Case statement is:
Select Case WeekDay (Date)
Case 1
DayName = "Monday"
Message = "Have a Nice week"
Case 6
DayName = "Saturday"
Message = "Have a Nice week"
Case 2
DayName = "Sunday"
Message = "Have a Nice week"
Case Else
Message = "Wel-come back!!!!"
End Select
The expression variable, which is evaluated at the beginning of the statement, is the number of the weekday, as reported by the WeekDay () function (a value in the range 1 t0 7). The value of the expression is then compared with the values that follow each case keyword. If they match, the block of statement up to the next Case keyword is executed, and the program skips to the statement following the End Select statement. The block of the Case Else statement ids optional and is executed if none of the previous Case value match the expression.
CHAPTER-6
LOOP STRUCTURE
Looping structure is used when a group of statement have to be executed repeatedly, based on a condition. Visual basic supports following loop construction:
6.1 DO….LOOP
A do…loop construct is used to repeat the execution of a block of statement, based on a condition. This loop construct evaluates a numeric condition to determine whether or not to continue the execution. The condition must be a value or expression that evaluate to a false (zero) or to a true (nonzero).
The variation of do….loop statement are listed in below table.
Do……Loop statement and Action
Statement
DoWhile <condition>
<statements>
loop
action :
Statement
Do
<statements>
loop While <condition>
action :
<condition>
Statement
Do Until <condition>
<statements>
loop
action :
Statement
Do
<statements>
loop Until <condition>
action :
1. Executes the <statement> first and then tests the <condition>
2. loops at least once, then tests the condition.
6.2 WHILE….WEND
The while…..wend loop structure allows you to perform a series ofsteps based on a condition. The syntax of the While…..Wend structure is:
While <condition>
<statement>
Wend
The condition following the while clause is evaluated. If it is True, the statements in the block are executed. When the Wend clause is encountered, the condition is tested again.
6.3 FOR….NEXT LOOP
Do and while loop are used when it is not known exactly how many number of times the statements in the loop will be executed. If you know the specific number of times a lop needs to be executed, it is always better to use a For…Next loop. A For..Next loop uses a counter that increases in value each time the loop gets executed.
The syntax of the For…. Next loop is:
For <counter> = <start> to <end> [step value]
[Statements]
next <counter>
counter- is the integer value that is incremented
start – is the initial value of the counter.
end – is the stop value of the counter.
Step value- is the increment or decrement value of counter.
6.4 NESTING For LOOPS
As with all other Visual Basic statements, you can nest two or more For loops inside one another. Anytime your program needs to repeat a loop more than once, use a nested loop. Figure shows an outline of a nested For loop. Think of the inside loop as looping "faster" than the outside loop. The inside loop iterates because the variable In goes from 1 to 10 in the inside loop before the outside loop’s first iteration has completed. Because the outside loop does not repeat until the Next Out statement, the inside For loop has a chance to finish in its entirety. When the outside loop finally does iteate a second time, the inside loop start all over again.
Figure
The outside loop determines For Out = 1 to 4
How many times the inside For In = 1 to 10
Loop executes. Inner loop ‘block of code goes here’
Outer loop Next In
Next Out
Program for cotinues when outer loop end.
Figure inner loop executes a total of 40 times. The outside loop iterates four times, and the inside loop executes 10 times for each of the outer loop’s iterations.
CHAPTER-7
ADVANCE KEYBOARD AND SCREEN SUPPORT.
This gives you how to get the user’s input and send information to the screen as output.
We learn the following.
7.1 The MgsBox() function :-
A massage box is a small dialog box used for output during a program’s execution. The user can close the message box by clicking a command button and can move the message box but the user cannot resize the message box.
The following is the format of the MsgBox() function:
IntResponse = MsgBox ( strPrompt [, intStyle ] [, strTitle ] )
IntResponse hold the function’s integer return data type. The first argument is a string (or variable control that hold the string) that displays as the message box’s message. The third argument determines the style of the button that appears. The last argument determines the style of the button that appears. The last argument determines the title that appears in the title bar of the message box.
All message boxes display a command button. Your executing program must known when your user is finished reading the message box. The program’s execution temporarily halts until the user click one of the message box’s buttons. As soon as user clicks the command button, the program continues executing at the statement that follows the command button.
7.2 The InputBox() function :-
Getting input with InputBox()
An input box is a message box with a field in which the user can type a value, such as word or phrase that might answer a question you ask in the input box’s title. As with a message box, the user can move or close an input box but not resize it. Unlike message boxes, you cannot control which command button appears in input boxes. The OK and Cancel command button is the only button that input boxes display.
The format for the InputBox (strPrompt [, strTitle ] [, strDefault ] [, intXpos ] [, intYpos ] )
strPrompt: the massage, such as the question you ask, that appear in the input box.
strtitle: the text that appear in the input box window’s title bar.
strdefault: a default valve that appear in the input box’s input field.
intXops, intYops: the twip coordinate where you want the input box to appear in the form window.
The following statement generated the previous input box:
strAnswer= InputBox ( "What is the customer’s name?", "Get name", "pankaj lamture", 500 , 700 )
7.3 About check boxes: -
A check box is an option on a from that is checked when selected and underchecked when not selected. Use CheckBox controls when you want to offer two value choices, such as whether something is true or false or possibly on or off.
Figure a: The MsgBox() function
displays a message and
lets the user respond.
Figure b: the input box provides
a title and a field for
data entry.
Figure c: you user can select
Various option with
Check box.
CHAPTER-8
DATABASE IN VISUAL BASIC
Nearly all business applications need to store large volume of data, organized in a format that simplifies retrieval. This is accomplished with a database management system (DBMS), a mechanism for manipulating tabular data with high-level commands. The database management system hides low-level details, such as how data are store in a database, and frees the programmer to concentrate on managing information, rather than on the specifics of manipulating files or maintaining or maintaining links among them.
Visual basic provides a wealth of tools for creating and accessing databases on both individual machines and networks.
The two major tools are:
The data control gives you access to database without any programming. You can set a few properties of the control use regular controls such s textboxes to display the values of the fields in the database. This is the no-code approach to database programming, which is implemented quite nicely in visual basic. But as you can guess, this approach can’t take you far. Sooner or later, you will have to write code.
The data access object is a structure of objects for accessing databases through your code. All the functionality of the data control is also available to your code, through the data access object (DAO).
Just what is a database?
In it’s basic sense’ a database is simply a grouping of related information organized for easy processing and retrieval. the actual data in a database is stored in tables, which are similar to random access files. Data in table is made up columns and rows contain identically structured pieces of information, which are equivalent to the records of random access files. A record is collection of values (called fields).
CHAPTER-9
THE PACKAGE AND DEPLOYMENT WIZARD:
The packing and deployment wizard does a lot of work for you, including the following task:
The package and deployment wizard generated a list of several files need for the setup. A single setup.exe file is not the only thing to come out of the setup routine. Often, a Visual Basic application requires DLL and OCX files, and those files must reside in the targeted setup area with the compiled program and the setup.exe program.
CHAPTER-10
PROGRAM FOR SPUR GEAR DESIGN
MODUL FORM
IT IS A ONLY DECLEARATION FORM
Option Explicit
Public req_power As Double
Public center_dist As Double
Public service_factor As Double
Public Safty_factor As Double
Public tens_stress As Double
Public np As Double
Public ng As Double
Public sb As Double
Public mt As Double
Public pt As Double
Public cv As Double
Public peff As Double
Public v As Double
Public dg As Double
Public dp As Double
Public res1 As Double
Public m As Double
FIRST PAGE
(MID FORM)
CODE AS WELL AS DIAGRAM OF FIRST PAGE
mid form
Option Explicit
Private Sub mnu_Click ()
Form1.Show
End Sub
Private Sub mnu_Click ()
MAINPAGE.Show
End Sub
SECOND PAGE
(MAINPAGE FORM)
CODE AS WELL AS DIAGRAM OF SECOND PAGE
Option Explicit
Private Sub Command1_Click()
example. Show
End Sub
THIRD PAGE
(EXAMPLE FORM)
CODE AS WELL AS DIAGRAM OF THIRD PAGE
Private Sub Command1_Click ()
Form1.Show
End Sub
FOURTH PAGE
(INPUT FORM)
CODE AS WELL AS DIAGRAM OF FOURTH PAGE
Input form
Private Sub Form_Load()
End Sub
Private Sub txt_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii = 13 Then
Select Case Index
Case 0
ng = CDbl(txt(Index).Text)
txt(Index + 1).SetFocus
Case 1
np = Val(txt(Index).Text)
txt(Index + 1).SetFocus
Case 2
req_power = Val(txt(Index).Text)
kw = req_power
txt(Index + 1).SetFocus
Case 3
center_dist = Val(txt(Index).Text)
txt(Index + 1).SetFocus
Case 4
service_factor = Val(txt(Index).Text)
cs = service_factor
txt(Index + 1).SetFocus
Case 5
Safty_factor = Val(txt(Index).Text)
fs = Safty_factor
txt(Index + 1).SetFocus
Case 6
tens_stress = Val(txt(Index).Text)
If (Trim(txt(0).Text) = "") Then
MsgBox "Speed of gear can not be null"
Else
If (Trim(txt(1).Text) = "") Then
MsgBox "Speed of pinion can not be null"
Else
If (Trim(txt(2).Text) = "") Then
MsgBox "Power required can not be null"
Else
If (Trim(txt(3).Text) = "") Then
MsgBox "Centre distance can not be null"
Else
If (Trim(txt(4).Text) = "") Then
MsgBox "Service factor can not be null"
Else
If (Trim(txt(5).Text) = "") Then
MsgBox "Factor of safety can not be null"
Else
If (Trim(txt(6).Text) = "") Then
MsgBox "Ultimate tensile stress can not be null"
Else
Form2.Show
End If
End If
End If
End If
End If
End If
End If
End Select
End If
End Sub
FOURTH PAGE
FIFTH PAGE
(CALCULATION NO-1 FORM)
CODE AS WELL AS DIAGRAM OF FIFTH PAGE
Option Explicit
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
Dim res As Double
res = np / ng
Text1(0).Text = res
res1 = Text1(0).Text
Case 1
Text1(1).Text = "100"
dp = Text1(1).Text
Case 2
Text1(2).Text = 400
dg = Text1(2).Text
Case 3
res = (3.14 * 100 * np) ' / (60 * 1000)
Text1(3).Text = Round(res / 60000, 4)
v = Text1(3).Text
Case 4
res = 3 / (3 + CDbl(Text1(3).Text))
Text1(4).Text = Round(res, 4)
cv = Text1(4).Text
Case 5
res = Round(10 * (700 / 3) * (0.34), 4)
Text1(5).Text = res
sb = Text1(5).Text
Case 6
res = Round((60 * (10 ^ 6) * kw) / (2 * 3.14 * np), 5)
Text1(6).Text = res
mt = Text1(6).Text
Case 7
pt = (2 * Text1(6).Text) / dp
Text1(7).Text = Round(pt, 4)
pt = Text1(7).Text
Case 8
peff = Round((cs * pt) / cv, 4)
Text1(8).Text = peff
Text1(8).SetFocus
FIFTH PAGE
SIXTH PAGE
(CALCULATION NO-2 FORM)
CODE AS WELL AS DIAGRAM OF SIXTH PAGE
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
m = Round(Sqr((peff * fs) / sb), 4)
Text1(0).Text = m
Case 1
b = Round(10 * m, 4)
Text1(1).Text = b
Case 2
zp = Round(dp / m, 4)
Text1(2).Text = zp
Case 3
zg = Round(dg / m, 4)
Text1(3).Text = zg
Case 4
sb = Round((m * b * (700 / 3) * 0.34), 4)
Text1(4).Text = sb
Case 5
peff1 = Round((cs * pt + 1970), 4)
Text1(5).Text = peff1
Case 6
sb = CDbl(Text1(4).Text)
fs = Round((sb / peff1), 4)
Text1(6).Text = fs
Case 7
Text1(7).Text = 4
m = 4
End Select
End Sub
DIAGRAM OF SIXTH PAGE
CHAPTER-11
REFERENCES
BY- EVANGELOS PETROUTSOS
BY- A BOOK OF ‘NIIT’ INSTITUTE.