Visual Basic

Post on 19-Jun-2015

333 views 1 download

Tags:

description

Visual Basic Basic Programs

Transcript of Visual Basic

P a g e | 1

Message Box

Private Sub Command1_Click()Unload MeEnd Sub

Private Sub Command2_Click()Dim textmsg As Integertextmsg = MsgBox("CLICK TO TEST", 1, "TEXT MESSAGE")If textmsg = 1 ThenLabel1.Caption = "TESTING SUCCESSFUL"ElseLabel1.Caption = "TESTING FAIL"End IfEnd Sub

Private Sub Command3_Click()Dim textmsg2 As Integertextmsg2 = MsgBox("CLICK TO TEST", vbYesNoCancel + vbExclamation, "TEXT MESSAGE")If textmsg2 = 6 ThenLabel1.Caption = "TESTING SUCCESSFUL"ElseIf textmsg2 = 7 Then

P a g e | 2

Label1.Caption = "ARE U SURE"ElseLabel1.Caption = "TESTING FAIL"End IfEnd Sub

P a g e | 3

P a g e | 4

Input Box

Private Sub Command1_Click()Dim usermsg As Stringusermsg = InputBox("WHAT IS YOUR MESSAGE?", "MESSAGE ENTRY FORM", "500", "700")If usermsg <> "" ThenText1.Text = usermsgElseText1.Text = "NO MESSAGE"End IfEnd Sub

P a g e | 5

P a g e | 6

Calculator

Dim OP As StringDim FIRST As Double

Private Sub Command1_Click(INDEX As Integer)Text1.Text = Text1.Text + Command1(INDEX).CaptionEnd Sub

Private Sub Command2_Click(INDEX As Integer)FIRST = Val(Text1.Text)OP = Command2(INDEX).CaptionText1.Text = ""End Sub

Private Sub Command3_Click()Select Case (OP)Case "+"Text1.Text = FIRST + Val(Text1.Text)Case "-"Text1.Text = FIRST - Val(Text1.Text)Case "*"Text1.Text = FIRST * Val(Text1.Text)

P a g e | 7

Case "/"Text1.Text = FIRST / Val(Text1.Text)End SelectEnd Sub

Private Sub Command4_Click()Text1.Text = Sqr(Val(Text1.Text))End Sub

Private Sub Command5_Click()Text1.Text = ""OP = ""End Sub

P a g e | 8

Traffic Light using Timer

Private Sub command1_click() 'Timer1.Enabled = True End Sub Private Sub command2_click() 'If Timer1.Enabled = False Then 'Else 'Timer1.Enabled = True 'End If End Sub

Private Sub timer1_timer() If Image1.Visible = True Then Image1.Visible = flase Image2.Visible = True

P a g e | 9

ElseIf Image2.Visible = True Then Image2.Visible = False Image3.Visible = True ElseIf Image3.Visible = True Then Image3.Visible = False Image1.Visible = True End If End Sub

P a g e | 10

Application to Display Circle, Line, and Pset

Private Sub CIRCLE_Click()Dim I As IntegerFor I = 100 To 10000 Step 100Form1.Circle (I, I), I, RGB(255, 0, 0)Next IEnd Sub

Private Sub CLOSE_Click()EndEnd Sub

Private Sub CLS_Click()Form1.CLSEnd Sub

P a g e | 11

Private Sub HIDE_Click()Form1.HIDEEnd Sub

Private Sub LINE_Click()Dim I As IntegerScaleMode = 3For I = 10 To 250 Step 3Form1.Line (10, I)-(250, I), RGB(0, 0, 255)Form1.Line (I, 10)-(I, 250), RGB(255, 0, 255)DoEventsNext IEnd Sub

Private Sub MOVE_Click()Form1.Left = 1000Form1.Top = 2000End Sub

Private Sub PSET_Click()Dim I As Integer, J As IntegerDim RED As Integer, GREEN As Integer, BLUE As IntegerScaleMode = 3For I = 1 To 200For J = 1 To 200RED = CInt(Rnd * 255)BLUE = CInt(Rnd * 255)GREEN = CInt(Rnd * 255)Form1.PSet (I, J), RGB(RED, GREEN, BLUE)Next JNext IEnd Sub

Private Sub REFRESH_Click()Form1.REFRESHEnd Sub

Private Sub RESIZE_Click()Form1.Width = 5000End Sub

Private Sub SHOW_Click()Form1.SHOWEnd Sub

P a g e | 12

P a g e | 13

Notepad

Private Sub cmdpaste_Click()Textnpad.SelText = Clipboard.GetTextEnd Sub

Private Sub cmdselectall_Click()Text$ = Textnpad.TextTextnpad.SelStart = 0Textnpad.SelLength = Len(Text$)Textnpad.SetFocusEnd Sub

Private Sub cmdcopy_Click()Clipboard.SetText Textnpad.SelTextcmdpaste.Enabled = TrueEnd Sub

Private Sub cmdcut_Click()Clipboard.SetText Textnpad.SelTextTextnpad.SelText = " "cmdpaste.Enabled = TrueEnd Sub

P a g e | 14

P a g e | 15

Stimulate Dice Rolling

Private Sub command1_click()Dim dice

Randomizedice = Int((6 * Rnd) + 1)

Select Case dice

Case 1Shape2(0).FillStyle = 1Shape2(1).FillStyle = 1Shape2(2).FillStyle = 1Shape2(3).FillStyle = 1Shape2(4).FillStyle = 1Shape2(5).FillStyle = 1Shape2(6).FillStyle = 0

Case 2Shape2(0).FillStyle = 1Shape2(1).FillStyle = 1Shape2(2).FillStyle = 0Shape2(3).FillStyle = 0Shape2(4).FillStyle = 1Shape2(5).FillStyle = 1

P a g e | 16

Shape2(6).FillStyle = 1

Case 3Shape2(0).FillStyle = 0Shape2(1).FillStyle = 1Shape2(2).FillStyle = 1Shape2(3).FillStyle = 1Shape2(4).FillStyle = 1Shape2(5).FillStyle = 0Shape2(6).FillStyle = 0

Case 4Shape2(0).FillStyle = 0Shape2(1).FillStyle = 0Shape2(2).FillStyle = 1Shape2(3).FillStyle = 1Shape2(4).FillStyle = 0Shape2(5).FillStyle = 0Shape2(6).FillStyle = 1

Case 5Shape2(0).FillStyle = 0Shape2(1).FillStyle = 0Shape2(2).FillStyle = 1Shape2(3).FillStyle = 1Shape2(4).FillStyle = 0Shape2(5).FillStyle = 0Shape2(6).FillStyle = 0

Case 6Shape2(0).FillStyle = 0Shape2(1).FillStyle = 0Shape2(2).FillStyle = 0Shape2(3).FillStyle = 0Shape2(4).FillStyle = 0Shape2(5).FillStyle = 0Shape2(6).FillStyle = 1End SelectEnd Sub

P a g e | 17

P a g e | 18

Generate All Library Functions

Private Sub ASCII_Click()Dim A As StringA = InputBox("ENTER A STRING")MsgBox ("ASCII CODE IS:" & Asc(A))End Sub

Private Sub CASE_Click()Dim A As StringA = InputBox("ENTER STRING IN LOWER CASE:")MsgBox ("STRING IN UPPER CASE IS:" & UCase(A))Dim B As StringB = InputBox("ENTER STRING IN UPPER CASE:")MsgBox ("STRING IN LOWER CASE IS:" & LCase(B))End Sub

Private Sub CHARACTER_Click()Dim A As StringA = InputBox("ENTER ANY VALUE")MsgBox ("CHARACTER CODE IS:" & Chr(A))End Sub

Private Sub DATE_TIME_Click()MsgBox ("DATE IS:" & Date)MsgBox ("TIME IS:" & Time())

P a g e | 19

End Sub

Private Sub INSTRING_Click()Dim A As StringA = InputBox("ENTER A STRING")Dim B As StringB = InputBox("ENTER A CHARACTER YOU WANT TO SEARCH FROM THE STRING")MsgBox (InStr(A, B))End Sub

Private Sub LEFT_RIGHT_Click()Dim A As StringA = InputBox("ENTER A STRING TO EXTRACT CHARACTERS")Dim I As IntegerI = InputBox("ENTER NO.OF CHARACTERS YOU WANT TO EXTRACT")MsgBox ("STRING USING LEFT FUNCTION:" & Left(A, I))MsgBox ("STRING USING RIGHT FUNCTION:" & Right(A, I))End Sub

Private Sub LENGTH_Click()Dim A As StringA = InputBox("ENTER A STRING TO CALCULATE ITS LENGTH")Dim R As IntegerR = Len(A)MsgBox ("LENGTH OF STRING IS:" & Val(R))End Sub

Private Sub MID1_Click()Dim A As StringDim I As Integer, J As IntegerA = InputBox("ENTER A STRING")I = InputBox("ENTER THE STARTING POSITION")J = InputBox("ENTER THE NO.OF CHARACTERS")MsgBox ("USE OF MID:" & MID(A, I, J))End Sub

Private Sub REVERSE_Click()Dim A As StringA = InputBox("ENTER ANY STRING")MsgBox ("REVERSE OF TRING IS:" & StrReverse(A))End Sub

Private Sub TRIMMING_Click()Dim A As StringA = InputBox("ENTER A STRING WITH LEADING ANT TRAILING SPACES")MsgBox ("STRING USING LTRIM:" & LTrim(A))MsgBox ("STRING USING TRIM:" & Trim(A))MsgBox ("STRING USING RTRIM:" & RTrim(A))End Sub

P a g e | 20

P a g e | 21

MDI (Multiple Document Interface) Form

P a g e | 22

Adding Menu Editor

P a g e | 23

P a g e | 24

Database in SQL Using ADO connection

P a g e | 25

create table student(sname char(20),roll number(4),sid number(4),gender char(3),dob date,marks number(5))table created.

SQL>insert into student values(‘&sname’,&roll,&sid,‘&gender’,‘&dob’,&marks);Enter value for sname: richardEnter value for roll: 01Enter value for sid: 2012Enter value for gender: mEnter value for dob: 12-jun-1989Enter value for marks: 76old 1: insert into student values(‘&name’,&roll,&sid,‘&gender’,‘&dob’,&marks);new 1: insert into student values(‘richard’,01,2012,‘m’,‘12-jun-1989’,76)

1 row created.

SQL>/ Enter value for sname: bentleyEnter value for roll: 02Enter value for sid: 2607Enter value for gender: mEnter value for dob: 28-jul-1989Enter value for marks: 70old 1: insert into student values(‘&name’,&roll,&sid,‘&gender’,‘&dob’,&marks);new 1: insert into student values(‘bentley’,02,2607,‘m’,‘28-jul-1989’,70)

1 row created.

SQL>/ Enter value for sname: anaEnter value for roll: 03Enter value for sid: 2609Enter value for gender: fEnter value for dob: 12-dec-1989Enter value for marks: 80old 1: insert into student values(‘&name’,&roll,&sid,‘&gender’,‘&dob’,&marks);new 1: insert into student values(‘ana’,03,2609,‘m’,‘12-dec-1989’,80)

1 row created.SQL>/ Enter value for sname: julieEnter value for roll: 04Enter value for sid: 2621

P a g e | 26

Enter value for gender: fEnter value for dob: 25-jan-1989Enter value for marks: 88old 1: insert into student values(‘&name’,&roll,&sid,‘&gender’,‘&dob’,&marks);new 1: insert into student values(‘julie’,04,2621,‘f’,‘25-jan-1989’,88)

1 row created.

SQL>/ Enter value for sname: maryEnter value for roll: 05Enter value for sid: 2633Enter value for gender: fEnter value for dob: 19-aug-1989Enter value for marks: 64old 1: insert into student values(‘&name’,&roll,&sid,‘&gender’,‘&dob’,&marks);new 1: insert into student values(‘mary’,05,2633,‘f’,‘19-aug-1989’,64)

1 row created.

P a g e | 27

P a g e | 28

Data Grid

P a g e | 29

Creating MS Access Database

P a g e | 30

P a g e | 31

Enter the fields

P a g e | 32

Click on close button in add field dialog box and click on the Build The table button

P a g e | 33

Creating the connectivity of MS Access Database with ADODC

Adodc1 (ActiveX Data Object Data Control)

Next Recordprevious

P a g e | 34

Dim comm As New ADODB.ConnectionDim Rs As New ADODB.Recordset

Private Sub cmdadd_Click()Rs.AddNewText1.Text = ""Text2.Text = ""Text3.Text = ""Text4.Text = ""cmdadd.Visible = Falsecmdupdate.Enabled = Falsecmdexit.Enabled = Falsecmdnext.Enabled = Falsecmdlast.Enabled = Falsecmdprevious.Enabled = Falsecmddelete.Enabled = Falsecmdfirst.Enabled = FalseEnd Sub

Private Sub cmddelete_Click()cmdadd.Visible = Falsecmdupdate.Enabled = Falsecmdexit.Enabled = Falsecmdnext.Enabled = Falsecmdlast.Enabled = Falsecmdprevious.Enabled = Falsecmddelete.Enabled = Falsecmdfirst.Enabled = FalseDim ans As String, str As Stringans = MsgBox("do you really want to delete the current record ? ", vbExclamation + vbYesNo)If ans = vbYes Thencomm.Execute ("delete from lib where book_id=" & Text1.Text)MsgBox ("record has been deleted successfully")Set Rs = Nothingstr = "select * from lib"Rs.Open str, comm, adOpenDynamic, adLockPessimisticRs.MoveFirstText1.Text = Rs(0)Text2.Text = Rs(1)Text3.Text = Rs(2)Text4.Text = Rs(3)End Ifcmdadd.Enabled = Truecmdupdate.Enabled = Truecmdexit.Enabled = Truecmdprevious.Enabled = Truecmdnext.Enabled = Truecmdlast.Enabled = Truecmdfirst.Enabled = True

P a g e | 35

End SubPrivate Sub cmdexit_Click()Unload MeEnd Sub

Private Sub cmdfirst_Click()Rs.MoveFirstText1.Text = Rs(0)Text2.Text = Rs(1)Text3.Text = Rs(2)Text4.Text = Rs(3)End Sub

Private Sub cmdlast_Click()Rs.MoveLastText1.Text = Rs(0)Text2.Text = Rs(1)Text3.Text = Rs(2)Text4.Text = Rs(3)End Sub

Private Sub cmdnext_Click()Rs.MoveNextIf Rs.EOF = True ThenMsgBox " this is last record ", vbExclamationRs.MoveLastEnd IfText1.Text = Rs(0)Text2.Text = Rs(1)Text3.Text = Rs(2)Text4.Text = Rs(3)End Sub

Private Sub cmdprevious_Click()Rs.MovePreviousIf Rs.BOF = True ThenMsgBox "this is the first record", vbExclamationRs.MoveFirstEnd IfText1.Text = Rs(0)Text2.Text = Rs(1)Text3.Text = Rs(2)Text4.Text = Rs(3)End Sub

P a g e | 36

Private Sub cmdsave_click()Rs(0) = Text1.TextRs(1) = Text2.TextRs(2) = Text3.TextRs(3) = Text4.TextRs.UpdateMsgBox "the record has been saved successfully"cmdadd.Visible = Truecmdupdate.Enabled = Truecmdexit.Enabled = Truecmdprevious.Enabled = Truecmdnext.Enabled = Truecmdlast.Enabled = Truecmdfirst.Enabled = Truecmddelete.Enabled = TrueEnd Sub

Private Sub cmdupdate_Click()cmdupdate.Enabled = Falsecmdadd.Visible = Falsecmdexit.Enabled = Falsecmddelete.Enabled = FalseDim ans As String ans = MsgBox("do you really want to modify the current record ? ", vbExclamation + vbYesNo) If ans = vbtes Then Rs.Update Else cmdupdate.Enabled = True cmdadd.Enabled = True cmdexit.Enabled = True End IfEnd Sub

Private Sub Cmdsearch1_Click()If Text5.Text = vbNullString Then Exit SubfindStr = "select * from lib where book_id = " & Text5.TextSet RsSearch = New ADODB.RecordsetRsSearch.Open findStr, comm, adOpenDynamicIf RsSearch.EOF And RsSearch.BOF ThenMsgBox "Search Could not find any matching data", vbInformation, "Invalid Search Criteria"GoTo CloseRsSearch:End IfText1.Text = RsSearch!book_idText2.Text = RsSearch!booknameText3.Text = RsSearch!authorText4.Text = RsSearch!editionCloseRsSearch:RsSearch.Close: Set RsSearch = Nothing

P a g e | 37

End SubPrivate Sub Form_Load()Dim str As StringMe.Caption = "library system"Set comm = Nothingcomm.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=library.mdb;Persist Security Info=False"str = "select * from lib"Rs.Open str, comm, adOpenDynamic, adLockPessimisticRs.MoveFirstText1.Text = Rs(0)Text2.Text = Rs(1)Text3.Text = Rs(2)Text4.Text = Rs(3)End Sub

Private Sub Form_Unload(cancel As Integer)Rs.Close: Set Rs = Nothingcomm.Close: Set comm = NothingEnd Sub

P a g e | 38

Data environment and Data Report

P a g e | 39

P a g e | 40

P a g e | 41

Select Table From List

Click on it

This dialog box will appear

Select The Database

P a g e | 42

Select The Required Table

P a g e | 43

The list of fields will appear

P a g e | 44

Drag n Drop

P a g e | 45

P a g e | 46

P a g e | 47

Creating Help File

Save the file in RTF(Rich Text Format)

Open Help Workshop

Create a New Document

Select help project and click OK

Save the file with extinction .hpj

Click on the file and add the Rich Text Format File that You have prepared

Write IDH_FILE after file and Hide It from font dialog box,,

P a g e | 48

P a g e | 49

Open VB and Drag and drop the Microsoft Dialog Common Control 6.0

Create A command button

Write The Code on command button

Private Sub Command1_Click()cd.HelpFile = "E:\sem3\vb file\help1.hlp"cd.HelpCommand = cdlHelpContentscd.ShowHelpEnd Sub

P a g e | 50

P a g e | 51