You are on page 1of 37

BUBBLE SORT Coding Option Explicit Dim marray(10) As Integer Private Sub cmdcreate_Click() Dim x As Integer Call Randomize

Call lstoriginal.Clear Erase marray For x = LBound(marray) To UBound(marray) marray(x) = 1 + Int(100 * Rnd()) Call lstoriginal.AddItem(marray(x)) Next x Call lstsorted.Clear cmdsort.Enabled = True End Sub Private Sub cmdexit_Click() End End Sub Private Sub cmdsort_Click() Dim x As Integer Call lstsorted.Clear Call bubblesort(marray) For x = 1 To UBound(marray) Call lstsorted.AddItem(marray(x)) Next x cmdsort.Enabled = False End Sub Public Sub bubblesort(a() As Integer) Dim pass As Integer, compare As Integer Dim hold As Integer For pass = 1 To (UBound(a) - 1) For compare = 1 To (UBound(a) - 1) If a(compare) > a(compare + 1) Then hold = a(compare) a(compare) = a(compare + 1) a(compare + 1) = hold End If Next compare Next pass End Sub

FORM DESIGN

OUTPUT

Result

LINEAR SEARCH Coding: Option Explicit Dim arr(10) As Integer Private Sub cmdsearch_Click() Dim searchkey As Integer Dim element As Integer lblresult.Caption = "" searchkey = txtkey.Text element = linearsearch(arr(), searchkey) If element <> -1 Then lblresult.Caption = "Value was found" Else lblresult.Caption = "Value not found" End If End Sub Private Sub Form_Load() Call lstdata_Click End Sub Private Sub lstdata_Click() Dim x As Integer Call Randomize Call lstdata.Clear lblresult.Caption = "" For x = LBound(arr) To UBound(arr) arr(x) = 1 + Int(100 * Rnd()) Call lstdata.AddItem(arr(x)) Next x End Sub Private Sub cmdexit_Click() End End Sub Function linearsearch(a() As Integer, key As Integer) As Integer Dim x As Integer For x = LBound(a) To UBound(a) If a(x) = key Then linearsearch = x Exit Function End If Next x linearsearch = -1 End Function

FORM DESIGN AND OUTPUT

RESULT

CONTROL ARRAY Coding Public curval As Double Public preval As Double Public choice As String Public result As Double Private Sub ac_Click(Index As Integer) curval = preval = 0 Text1.Text = "" End Sub Private Sub add_Click(Index As Integer) Text1.Text = " " preval = curval curval = 0 choice = "+" End Sub Private Sub Command1_Click(Index As Integer) Text1.Text = Text1.Text & Command1(Index).Caption curval = Val(Text1.Text) End Sub Private Sub div_Click(Index As Integer) Text1.Text = " " preval = curval curval = 0 choice = "/" End Sub Private Sub eq_Click(Index As Integer) Select Case choice Case "+" result = preval + curval Text1.Text = Str(result) Case "-" result = preval - curval Text1.Text = Str(result) Case "*" result = preval * curval Text1.Text = Str(result) Case "/" result = preval / curval Text1.Text = Str(result) End Select curval = result

End Sub Private Sub mul_Click(Index As Integer) Text1.Text = " " preval = curval curval = 0 choice = "*" End Sub Private Sub sub_Click(Index As Integer) Text1.Text = " " preval = curval curval = 0 choice = "-" End Sub Private Sub EXIT_Click() End End Sub

FORM DESIGN:

OUTPUT:

Result:

DYNAMIC ARRAY

Coding: Dim mdynamic() As Integer Private Sub cmderase_Click() Erase mdynamic Call lstval.Clear cmderase.Enabled = False chkpreserve.Value = vbUnchecked End Sub

Private Sub cmdprint_Click() Dim length As Integer, x As Integer Call lstval.Clear length = txtinput.Text If chkpreserve.Value = vbUnchecked Then ReDim mdynamic(length) Call initarray ElseIf chkpreserve.Value = vbChecked Then ReDim Preserve mdynamic(length) End If For x = LBound(mdynamic) To UBound(mdynamic) Call lstval.AddItem("index:" & x & vbTab & vbTab & "value:" & mdynamic(x)) Next x cmderase.Enabled = True End Sub Private Sub initarray() Dim j As Integer For j = LBound(mdynamic) To UBound(mdynamic) mdynamic(j) = Int(50 * Rnd()) Next j End Sub

FormDesign

OUTPUT

Result

KEYBOARD PREVIEW PROPERTY Coding


Option Explicit Private Sub Form_KeyPress(KeyAscii As Integer) If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then Text1.Text = Text1.Text & Chr$(KeyAscii) End If End Sub Private Sub Form_Load() Call Randomize KeyPreview = True End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) KeyAscii = 0 End Sub Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) Select Case Int(Rnd() * 3) Case 0 Text1.BackColor = vbYellow Case 1 Text1.BackColor = vbCyan Case 2 Text1.BackColor = vbRed End Select End Sub

Form Design:

Output:

Result:

KEYBOARD EVENTS

Coding:
Option Explicit Dim MTS As String Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case Shift Case vbShiftMask ForeColor = vbYellow Case vbAltMask ForeColor = vbRed Case vbCtrlMask ForeColor = vbGreen Case vbShiftMask + vbAltMask ForeColor = vbBlue Case vbShiftMask + vbCtrlMask ForeColor = vbMagenta Case vbAltMask + vbCtrlMask ForeColor = vbCyan Case vbShiftMask + vbAltMask + vbCtrlMask Call Cls End Select If KeyCode >= vbKeyA And KeyCode <= vbKeyZ Then Print Chr$(KeyCode) ElseIf KeyCode = vbKeyReturn Then Print End If End Sub Private Sub Form_KeyPress(KeyAscii As Integer) Caption = MTS & "(" & Chr$(KeyAscii) & ")" End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) ForeColor = vbBlack End Sub Private Sub Form_Load() MTS = Caption & Space$(5) End Sub

OUTPUT

Result:

Mouse drag and drop


Coding: Option Explicit Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) Dim w As Integer, h As Integer w = X - Source.Width / 2 h = Y - Source.Height / 2 Call Source.Move(w, h) End Sub Private Sub Form_Load() Dim a As Integer For a = Command1.LBound To Command1.UBound Command1(a).DragMode = 1 Next a End Sub

Form Design:

Output:

Result

MOUSE EVENTS
Coding:

Private Sub Command1_Click() Form2.Cls Option1.Value = False End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Option1 = True Then img1.Move X, Y Else img2.Move X, Y End If End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Circle (X, Y), 70 End Sub

FORM DESIGN:

OUTPUT

RESULT:

BASIC DATATYPES

Coding: Private Type employee name As String designation As String salary As Currency End Type Dim allow As Integer Dim n As Variant Dim sal As employee Private Sub compute_Click() If Not IsNumeric(txtno.Text) Then MsgBox " please enter numeric values alone in Number field" ElseIf IsNumeric(txtname.Text) Then MsgBox " Numerals not allowed in name field" ElseIf IsNumeric(txtdesig.Text) Then MsgBox " Numerals not allowed in designation field" ElseIf Not IsNumeric(txtbpay.Text) Then MsgBox " please enter numeric values alone in basic pay field" ElseIf Not IsNumeric(txthra.Text) Then MsgBox " please enter numeric values alone in HRA field" ElseIf Not IsNumeric(txtma.Text) Then MsgBox " please enter numeric values alone in MA field" ElseIf Not IsNumeric(txttax.Text) Then MsgBox " please enter numeric values alone in TAX field" Else allow = Val(txtbpay.Text) + Val(txthra.Text) + Val(txtma.Text) n = allow - Val(txttax.Text) sal.salary = n sal.name = txtname.Text lblname = sal.name lblnpay = sal.salary End If End Sub

FORM DESIGN:

Output:

Result

STANDARD DIALOG BOX Coding:


Option Explicit Private Sub disp_Click() Dim message As String Dim dialogtype As Integer Dim title As String message = "GOOD MORNING" dialogtype = vbOK + vbInformation title = "Pre-defined demo" MsgBox message, dialogtype, title End Sub Private Sub getdate_Click() Dim i, day, msg i = InputBox("enter a date", "date demo") If Not IsDate(i) Then MsgBox "invalid date" Exit Sub End If day = Format(i, "dddd") msg = "day of this date is:" + day MsgBox msg End Sub Private Sub exit_Click() Dim message As String Dim dialogtype As Integer Dim title As String Dim response As Integer message = "Thank you" dialogtype = vbYesNo + vbInformation title = "good bye" response = MsgBox(message, dialogtype, title) If response = vbYes Then End End If End Sub

Form Design

Result

CUSTOM DIALOG CONTROL Coding: Option Explicit Private Sub disp_Click() Dim message As String Dim dialogtype As Integer Dim title As String message = "Have a gala shopping" dialogtype = vbOK + vbInformation title = "welcome to the supermarket" MsgBox message, dialotype, title End Sub Private Sub getday_Click() Form2.Show 1 If Form2.Tag = "" Then MsgBox "DIALOG BOX IS CANCELLED" Else MsgBox "The selected day is:" + Form2.Tag End If End Sub

FORM2: Private Sub cmdok_Click() If monday = True Then Form2.Tag = "MONDAY" If tuesday = True Then Form2.Tag = "TUESDAY" If wednesday = True Then Form2.Tag = "WEDNESDAY" If thursday = True Then Form2.Tag = "THURSDAY" If friday = True Then Form2.Tag = "FRIDAY" If saturday = True Then Form2.Tag = "SATURDAY" If sunday = True Then Form2.Tag = "SUNDAY,SO NO TRANSACTION" Form2.Hide End Sub Private Sub Command2_Click() Form2.Hide Form2.Tag = "" End Sub

Form Design Form 1:

Form 2:

OUTPUT:

Result:

COMMON DIALOG BOX


Coding: Option Explicit Private Sub Form_Load() Label1.Enabled = True Label2.Visible = False Label3.Visible = False End Sub Private Sub save_Click() CommonDialog1.DefaultExt = "txt" CommonDialog1.ShowSave MsgBox "file saved successfully" End Sub Private Sub color_Click() CommonDialog1.Action = 3 Label1.ForeColor = CommonDialog1.color Exit Sub End Sub Private Sub font_Click() Label2.Visible = True Label3.Visible = True CommonDialog1.Flags = cdlCFBoth + cdlCFForceFontExist + cdlCFEffects CommonDialog1.ShowFont Label3.Caption = CommonDialog1.FontName Label3.FontBold = CommonDialog1.FontBold Exit Sub End Sub Private Sub exit_Click() End End Sub

Form Design

OUTPUT

Result:

BANK ACCOUNT DATABASE Form1-Customer Details Coding Private Sub addnew_Click() Adodc1.Recordset.addnew MsgBox "enter the data and click update" txtdate.Text = Date txtaccno.SetFocus End Sub Private Sub Command1_Click() Form2.Show End Sub Private Sub delete_Click() Adodc1.Recordset.delete MsgBox "RECORD DELETED!" End Sub Private Sub exit_Click() End End Sub Private Sub first_Click() Adodc1.Recordset.MoveFirst End Sub Private Sub last_Click() Adodc1.Recordset.MoveLast End Sub Private Sub next_Click() Adodc1.Recordset.MoveNext If Adodc1.Recordset.EOF Then Adodc1.Recordset.MoveFirst End If End Sub Private Sub previous_Click() Adodc1.Recordset.MovePrevious If Adodc1.Recordset.BOF Then Adodc1.Recordset.MoveLast End If End Sub Private Sub search_Click() Dim flag As Boolean

Dim i As Variant i = InputBox("enter the accno") flag = False Adodc1.Recordset.MoveFirst Do While Not Adodc1.Recordset.EOF If StrComp(i, Adodc1.Recordset.Fields("accno"), vbTextCompare) = 0 Then flag = True Exit Do Else Adodc1.Recordset.MoveNext End If Loop If flag = True Then MsgBox "record found" Else MsgBox "record not found" End If End Sub Private Sub update_Click() Adodc1.Recordset.update MsgBox "Record update" End Sub

Form2:ACCOUNT DETAILS

Option Explicit Private Sub addnew_Click() Adodc1.Recordset.addnew MsgBox "enter the data and click update" dcboaccno.Visible = True optdep.Visible = True optwdraw.Visible = True txtdate.Text = Date txtaccno.SetFocus End Sub Private Sub clear_Click() Adodc1.Recordset.delete MsgBox "YOUR RECORD HAS BEEN DELETED" End Sub Private Sub dcboaccno_Click() txtaccno.Visible = True txtaccno.Text = dcboaccno.Text dcboaccno.Visible = True End Sub Private Sub exit_Click() Form1.Show End Sub Private Sub Form_Activate() MsgBox "Click ADD NEW to add a new record" End Sub Private Sub Form_Load() txtdate.Text = Date End Sub Private Sub genreport_Click() datareport1.Show End Sub

Private Sub optdep_Click() optdep.Visible = True txttot.Text = "deposit" End Sub Private Sub optwdraw_Click() optwdraw.Visible = True txttot.Text = "Withdraw" End Sub

Private Sub update_Click() Dim n Form1.Adodc1.Recordset.MoveFirst Do While Not Form1.Adodc1.Recordset.EOF If Trim(Val(txtaccno.Text)) = Trim(Val(Form1.Adodc1.Recordset.Fields("accno").Value)) Then If txttot.Text = "deposit" Then Form1.Adodc1.Recordset.Fields("txtbal").Value = (Form1.Adodc1.Recordset.Fields("txtbal").Value) + Val(txtamt.Text) Form1.Adodc1.Recordset.update MsgBox "RECORD UPDATED" Else n = Val(Form1.Adodc1.Recordset.Fields("balance").Value) - Val(txtamt.Text) If n <= 500 Then MsgBox "cannot withdraw" Else Form1.Adodc1.Recordset.Fields("balance").Value = Val(Form1.Adodc1.Recordset.Fields("balance").Value) - Val(txtamt.Text) Form1.Adodc1.Recordset.update MsgBox "Record Updated" End If End If Exit Do Else Form1.Adodc1.Recordset.MoveNext End If Loop End Sub

OUTPUT Form 1-Customer details

Form2-Account details

Data Report

Result:

You might also like