Front-End And Back-End Form / Interface / Front-End Student Roll Number Name Address Phone 1 Raj c-65, hari nagar 25126633 2 Amit a-45, Ramesh ngr. 25165263 3 Rahul c-96, Janak puri 25545298 Table / Relation / Back-End DataTypes in VB Roll Number Name Address Phone 4 Himanshu WZ-45, Hari Nagar 9213222920 Save Dele te Searc h
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Front-End And Back-End
Form / Interface / Front-End
StudentRoll Number Name Address Phone1 Raj c-65, hari nagar 251266332 Amit a-45, Ramesh ngr. 251652633 Rahul c-96, Janak puri 25545298
Table / Relation / Back-End
DataTypes in VB
Datatype Storage size Range 1. Byte 1 byte 0-2552. Boolean 2 bytes True or False3. Integer 2 bytes -32768 to +327674. Long 4 bytes -2147483648 to +2147483647 5. Single 4 bytes
Roll Number
Name
Address
Phone
4
Himanshu
WZ-45, Hari Nagar
9213222920
Save Delete Search
6. Double 8 bytes7. Currency 8 bytes8. Decimal 14 bytes9. Date 8 bytes Jan 1, 100 to
Dec 31, 999910. Object 4 bytes Any object reference11. String (variable length) 10 bytes + string length 0 to approx. 2 billion12. String (fixed length) length of string 1 to approx. 65,400 13. Variant (numeric) 16 bytes 14. Variant (text) 22 bytes + string length
Declaring & initializing Variables
Dim Num as integerNum = 2557
Or Num = -235 Or Num = 32 + 7 - 3
Dim Age as byteAge = 73
Or Age = 12Dim Name as string …. variable length string Name = “raj”Dim Name as string * 15 …. fixed length stringName = “raj”Dim Address as stringAddress = “A-45, janak puri“Dim price as singlePrice = 75.80
Or Price = 75.25 - 1.85Dim x as Booleanx = true
Or x = false Or x= 5<2 …. false
dim doj as datedoj = #8-15-2005#dim x as datex = #12:45 PM#dim p as variant
Or dim p …. (by default variant)p = “raj”
Or p = 45 Or p = #8-15-2005# Or p = true Or p=45.7
Dim a, b, c as integer
Variant variables
Literals
String literals:“raj”, “a-45, hari nagar”, “845.73”, “8-15-2005”
Boolean literals:True, false
Date / Time literals:#8-12-2005#, #1:45 AM #
Single / Double literals:12.5#, -13.80 , +0.73, 85.2!
Dim x% declares x as integer variableDim p$ declares p as string variableDim n# declares n as double variable
CommentsThe word REM or symbol (‘) tells the visual basic that everything on that line
following REM or (‘) is not the code, it is a comment and should be ignored by the compiler. REM can appear only in the beginning of the line but apostrophe (‘) can appear after a statement also.
Example:Dim x% ‘declares x as integer variable REM example of a comment.
Truth TableA B A AND B A OR B A XOR B NOT A NOT BFalse False False False False True True False True False True True True FalseTrue False False True True False TrueTrue True True True False False False
Events Event refers to the occurrence of an activity.
Some useful events
1. Click:- When the user clicks the primary mouse button on an object.2. Change:- When the user modifies text in combo box or text box.3. Gotfocus:- When an object receives focus.4. Lostfocus:-When an object loses focus.5. Keypress:- The user presses and releases a keyboard key while an object has
focus.6. Keyup:- When the user releases a keyboard key when an object has focus.7. Dragdrop:- The use drags an object to another location.8. Dragover:- The user drags an object over another control.9. Form_load: - When the form loads for execution in memory.
Setting the Properties
The controls that you draw on your form, have some properties associated with them. The properties can be set by two different ways.
1. At design time2. At run time.
1. Properties can be set at design time using properties window.
2. Setting properties at run time.
Setting the Alignment property
Example:Label1.alignment = 0
OrText1.alignment = 1
Setting Appearance property
Example:Label1.appearance =1
OrCommand1.appearance = 1
OrOption1.appearance = 0
Setting Autosize PropertyExample:
Label1.autosize = true
OrLabel1.autosize = false
OrPicture1.autosize = true
Setting the backcolor propertyExample:
Label1.backcolor = RGB(100, 125, 190)
0-255 (red) 0-255(green) 0-255( blue)Or
text1.backcolor = RGB(50, 50, 100)Or
text1.backcolor = VbRed
Setting Backstyle PropertyExample:
Label1.Backstyle = 0Or
Label1.backstyle = 1
Setting Borderstyle PropertyExample:
Label1.Borderstyle = 0Or
Label1.Borderstyle = 1
Setting the Enabled PropertyExample
Text1.enabled = trueOr
Label1.enabled = false
Setting caption propertyExample:
Label1.caption = “this is my name”Or
Command1.caption = “click me”
Setting forecolor propertyExample:
Label1.forecolor = RGB( 100, 255, 70)Or
Label1.forecolor = VbBlue Setting Height Property
Example:Label1.height = 100
OrCommand1.height = 200
Setting the tooltiptext propertyExample:
Text1.tooltiptext = “This is a text box”
Setting the text propertyExample:
Text1.text = “Amit sharma”
Setting the visible propertyExample:
Label1.visible = trueOr
Text1.visible = false
Setting the width propertyExample:
Text1.width = 250Or
Command1.width = 100
Setting the style propertyExample:
Command1.style = 0Or
Option1.style = 1
Setting the picture propertyExample:
Check1.picture = loadpicture ( “c:\abc.gif”)Or
Command1.picture = loadpicture ( “c:\abc.gif”)Or
Option1.picture = loadpicture ( “c:\abc.gif”)Or
Picture1.picture = loadpicture ( “c:\abc.gif”)
Setting the disabledpicture and downpicture propertyExample:Command1.disabledpicture = loadpicture(“c:\abc.gif”)OrCommand1.downpicture = loadpicture(“c:\abc.gif”)
Setting the value property of checkboxExample:
Check1.value = 0Or
Check1.value = 1
Setting the value property of option buttonExample:
Option1.value = trueOr
Option1.value = false
Setting the stretch property of image controlExample
Image1.stretch = trueOr
Image1.stretch = false
Setting the max/min property of Hscrollbar / VscrollbarExample
Hscroll1.max = 100
Hscroll1.min = 1
Vscroll1.max = 100
Vscroll1.min = 1 Setting the fillstyle and fillcolor property
Example:Shape1.fillstyle = 0
Shape1.fillcolor = RGB(100, 150, 200) Setting the Shape Property of Shape control
Example:Shape1.shape = 1
OrShape1.shape = 3
Setting the windowstate property of formExample:
Form1.windowstate = 0Or
Form1.windowstate = 1
Some Examples:Example1:- Loading a picture in the picture box at run time.
Picture Box
Command Button (cmdload)
Private sub cmdload_click()Picture1.picture = loadpicture (“c:\abc.gif”)
End sub
Example2: – Loading a picture in the image control at run time.
Image control(image1)Command Button( cmdload )
Private sub cmdload_click()Image1.picture = loadpicture (“c:\abc.gif”)
End sub
Example3: List Box
ListBox (list1)
Load
Add
Delete
Clear
Listcount
Listindex
Display
Enter Your Name
Private sub cmdadd_click()List1.additem text1.text
End sub
Private sub cmddel_click()List1.removeitem list1.listindex
End sub
Private sub cmdclear_click()List1.clear
End sub
Private sub cmdcount_click()Msgbox List1.listcount
End sub
Private sub cmdindex_click()Msgbox list1.listindex
End sub
Private sub cmddisp_click()Msgbox list1.text
End sub Adding an item in the list box
List1.additem “Ravi”List1.additem text1.text
Removing an item from the list boxList1.removeitem 0List1.removeitem list1.listindex
Example 4: Hscrollbar / Vscrollbar application
Hscroll1
Command Button (cmdsum) vscroll1
Private sub hscroll1_change()Text1.text = hscroll1.value
End sub
Private sub Vscroll1_change()Text2.text = vscroll1.value
End sub
Private sub cmdsum_click( )Dim a as integer, b as integera = val (text1.text)b = val (text2.text)text3.text = a + b
End sub
Example 5 : Shape Control ApplicationShape Control (shape1)
Sum
Private sub cmdrect_click( )Shape1.shape = 0
End sub
Private sub cmdsquare_click( )Shape1.shape = 1
End sub
Private sub cmdoval_click( )Shape1.shape = 2
End sub
Private sub cmdcircle_click( )Shape1.shape = 3
End sub
Private sub cmdroundrect_click( )Shape1.shape = 4
End sub
Private sub cmdroundsquare_click( )Shape1.shape = 5
End sub
Example 6 : Drive, Directory and file list box Drive1 directory1 file1
Rectangle Square Oval
Circle Rounded Rectangle
Rounded square
Private sub drive1_change( )Dir1.path = drive1.drive
End sub
Private sub dir1_change( )File1.path = dir1.path
End sub
Private sub text1_change( )File1.pattern = text1.text
End sub
Example 7: Timer application. Label1
Timer1 cmdreset
Set the following properties:Timer1.interval = 1000Label1 - > font = MS sans serif, bold, 18
C:
C:\ Program Files ………. ………. ……….
Abc.dllRmn.dllPqr.exe………………………
Enter the pattern
Reset
Dim I as integerPrivate sub cmdreset_click( )
I = 0End sub Private sub timer1_timer ( )
Label1.caption = iI = I + 1
End sub
Example 7: Option Button Application.
lblgrade cmdgrade
Private sub cmdgrade_click( )If optone.value = true then
Lblgrade.caption = “ The grade is A”Else if opttwo.value = true then
Lblgrade.caption = “ The grade is B”Else if optthree.value = true then
Lblgrade.caption = “ The grade is C”Else if optfour.value = true then
Lblgrade.caption = “ The grade is D”End if
End sub
Private sub form_load( )Optone.value = falseOpttwo.value = false
90
70
80
60
Click
Percentage
Optthree.value = falseOptfour.value = false
End sub
Manipulating forms
Setting the startup form
To change the startup form1. Goto project menu - > project peoperties2. Select General Tab3. In startup object listbox select the form4. Click Ok
Loading and unloading the forms
Load form1Unload form1Unload me
Showing and Hiding forms
Form1.showForm2.showForm1.hideMe.hide
Assigning the Access keyCommand buttons can be used by clicking on them or by pressing the alt + <
access key >. To assign a keyboard access key to command button place ampersand( &) in front of the letter that is to be used as access key, while setting the caption property of the command button.
Control Structures in VB
Control Flow:
1. Sequence2. Selection3. Iteration
The if statement:Syntax:
if (condition) thenstatements
end if
Example:
If (a>10) thenPrint ”Raj”
End if
The if…. Then… else statementSyntax:
If (condition) thenstatements
elsestatements
End ifExample: Text1
cmdresult
Private sub cmdresult_click( )Dim m as integerm = val (text1.text)if (m >= 40) then
print ”you are passed”else
print ”you are failed”end if
End sub
Enter the marks
Result
If …then… else if statementSyntax:
If (condition) thenstatements
else if (condition) thenstatements
else if (condition) thenstatements
.
.
.else
statementsEnd if
Example:
Text1
cmdprintday
Private sub cmdprintday_click( )Dim x as integerX = val(text1.text)If(x = 1) then
Msgbox ”Sunday”else if (x = 2) then
Msgbox ”Monday”else if (x = 3) then
Msgbox ”Tuesday”else if (x = 4) then
Msgbox ”Wednesday”else if( x = 5) then
Msgbox ”Thursday”else if (x = 6) then
Enter a number
Print day
Msgbox ”Friday”else if (x = 7) then
Msgbox ”Saturday”Else
Msgbox ”Invalid choice”End if
End sub
Nested ifsExample:
If ( x > 7) thenIf( y <= 8) then
Print ”Ravi”Else
Print ”Amit”End if
ElseIf (P <> 71) then
Print ”vishvas”Else
Print ”Raj”End if
End if
Compound if:Example:If ( x > 7 AND y <> 75) then
Print ”Vicky”Else
Print ”Raj”End if
Example:If( x >= 91 OR p <> 83) then
Print ”Rishabh”Else
Print ”Amrita”End if
The iif( ) function:Syntax:
iif(condition, value1, value2)Example:
Dim num1 as integer, num2 as integer, big as integer
mypos = instr(1, searchstring, searchchar, 0) - 9mypos = instr(searchstring, searchchar) - 9mypos = instr(searchstring, ”w”) - (0) not found
7. Space Functiondim p as stringp = “raj” & space(10) & ”puri”
print p - raj puri
8. String Functiondim x as string x = string(10, ”a”)print x - “aaaaaaaaaa”x = string (5, ”ABC”)print x - “AAAAA”
9. Str Functiondim x as string, y as stringx = str(1205) - “1205”y = str(1305) - “1305”print 12 + 15 - 27print str(12) + str(15) - 1215print “12” + “15” - 1215
10.Asc Functiondim x as integerx = asc(“A”)print x - 65print asc(“Abc”) - 65x = asc(“a”)print x - 97ASCII ValuesAmerican Standard Codes For Information Interchange.“A” to “Z” - 65 to 90“a” to ”z” - 97 to 122“0” to “9” - 48 to 57
11.Chr Functiondim x as stringx = chr(65)print x - Aprint chr(66) - B