Visual Basic

51
Page | 1 Message Box Private Sub Command1_Click() Unload Me End Sub Private Sub Command2_Click() Dim textmsg As Integer textmsg = MsgBox("CLICK TO TEST", 1, "TEXT MESSAGE") If textmsg = 1 Then Label1.Caption = "TESTING SUCCESSFUL" Else Label1.Caption = "TESTING FAIL" End If End Sub Private Sub Command3_Click() Dim textmsg2 As Integer textmsg2 = MsgBox("CLICK TO TEST", vbYesNoCancel + vbExclamation, "TEXT MESSAGE") If textmsg2 = 6 Then Label1.Caption = "TESTING SUCCESSFUL" ElseIf textmsg2 = 7 Then

description

Visual Basic Basic Programs

Transcript of Visual Basic

Page 1: 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

Page 2: Visual Basic

P a g e | 2

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

Page 3: Visual Basic

P a g e | 3

Page 4: Visual Basic

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

Page 5: Visual Basic

P a g e | 5

Page 6: Visual Basic

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)

Page 7: Visual Basic

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

Page 8: Visual Basic

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

Page 9: Visual Basic

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

Page 10: Visual Basic

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

Page 11: Visual Basic

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

Page 12: Visual Basic

P a g e | 12

Page 13: Visual Basic

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

Page 14: Visual Basic

P a g e | 14

Page 15: Visual Basic

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

Page 16: Visual Basic

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

Page 17: Visual Basic

P a g e | 17

Page 18: Visual Basic

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())

Page 19: Visual Basic

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

Page 20: Visual Basic

P a g e | 20

Page 21: Visual Basic

P a g e | 21

MDI (Multiple Document Interface) Form

Page 22: Visual Basic

P a g e | 22

Adding Menu Editor

Page 23: Visual Basic

P a g e | 23

Page 24: Visual Basic

P a g e | 24

Database in SQL Using ADO connection

Page 25: Visual Basic

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

Page 26: Visual Basic

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.

Page 27: Visual Basic

P a g e | 27

Page 28: Visual Basic

P a g e | 28

Data Grid

Page 29: Visual Basic

P a g e | 29

Creating MS Access Database

Page 30: Visual Basic

P a g e | 30

Page 31: Visual Basic

P a g e | 31

Enter the fields

Page 32: Visual Basic

P a g e | 32

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

Page 33: Visual Basic

P a g e | 33

Creating the connectivity of MS Access Database with ADODC

Adodc1 (ActiveX Data Object Data Control)

Next Recordprevious

Page 34: Visual Basic

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

Page 35: Visual Basic

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

Page 36: Visual Basic

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

Page 37: Visual Basic

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

Page 38: Visual Basic

P a g e | 38

Data environment and Data Report

Page 39: Visual Basic

P a g e | 39

Page 40: Visual Basic

P a g e | 40

Page 41: Visual Basic

P a g e | 41

Select Table From List

Click on it

This dialog box will appear

Select The Database

Page 42: Visual Basic

P a g e | 42

Select The Required Table

Page 43: Visual Basic

P a g e | 43

The list of fields will appear

Page 44: Visual Basic

P a g e | 44

Drag n Drop

Page 45: Visual Basic

P a g e | 45

Page 46: Visual Basic

P a g e | 46

Page 47: Visual Basic

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,,

Page 48: Visual Basic

P a g e | 48

Page 49: Visual Basic

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

Page 50: Visual Basic

P a g e | 50

Page 51: Visual Basic

P a g e | 51