You are on page 1of 27

Program to count digits of a given number

Private Sub cmdDigits_Click()


'variables declaration
Dim lngNum As Long
Dim intCounter As Integer
'Fetch the number from text box (txtNum) into lngNum
lngNum = Val(txtNum.Text)
'obtaining the individual digits from number and count
While lngNum > 0
lngNum = lngNum / 10
intCounter = intCounter + 1
Wend
'Print the output
lblOutput.Caption = "The total digits in the given number is " & intCounter
End Sub

Program to find whether the give character is vowel, special character,


Capital, small or digit

Private Sub cmdOk_Click()


'variable declaration
Dim strInput As String, bytAsc As Byte
'fetch character from text box (txtInput) into strInput
strInput = txtInput.Text
bytAsc = Asc(strInput)
'check if given input is small character
If bytAsc >= 65 And bytAsc <= 91 Then
Label2.Caption = "You have inputed a capital alphabet character"
'check if given input is capital character
ElseIf bytAsc >= 97 And bytAsc <= 123 Then
Label2.Caption = "You have inputed a small alphabet character"
'check if given input is numeric
ElseIf bytAsc >= 48 And bytAsc <= 58 Then
Label2.Caption = "You have inputed a numeric value "
'otherwise special character
Else
Label2.Caption = "You have inputted a special character"
End If
End Sub

Program to find perfect number in between 1 to 1000

Option Explicit
Private Sub Form_Load()
'Variable declaration
Dim intNum As Integer, intFactSum As Integer, intCounter As Integer
For intNum = 1 To 1000
'Calculate the sum of the factors of intTemp
intFactSum = 0
For intCounter = 1 To intNum - 1
If (intNum Mod intCounter) = 0 Then
intFactSum = intFactSum + intCounter
End If
Next intCounter
If intNum = intFactSum Then
lstOutput.AddItem (Str(intNum))
End If
Next intNum
End Sub

Program to print given string in the triangle String

Option Explicit
Private Sub cmdOk_Click()
'Variable Declaration
Dim strInput As String, strTemp As String
Dim intTotChar As Integer, intLength As Integer
'Fetch the string from text box (txtInput) into strInput
strInput = txtInput.Text
'Get the length of string
intLength = Len(strInput)
'Clear the form contents if any
Form1.Cls
For intTotChar = 1 To intLength
strTemp = Left(strInput, intTotChar)
Form1.Print strTemp
Next intTotChar
For intTotChar = intLength - 1 To 1 Step -1
strTemp = Left(strInput, intTotChar)
Form1.Print strTemp
Next intTotChar
End Sub

Program to make a login form

Option Explicit
Dim strMyusername As String, strMyPassword As String
Private Sub cmdLogin_Click()
'Variable declaration
Dim strUserName As String, strPassword As String
Dim intResponse As Integer
Static intAttempt As Integer
'Check if user attempted 3 times then exit the application
If intAttempt = 2 Then
MsgBox "You have made 3 wrong attempt" _
& "so now application will have to close immediately"
End
End If
'Fetch the username and password in from txtUserName and txtPassword respectively
strUserName = txtUserName.Text
strPassword = txtPassword.Text
'check wheather given username and password is match or not
If strUserName = strMyusername And strPassword = strMyPassword Then
MsgBox "Welcome to your account", , "Welcome"
End
Else

intAttempt = intAttempt + 1
intResponse = MsgBox("Invalid username or password" & vbCrLf _
& "Would you like to retry ?", vbRetryCancel, "Login Error")
If intResponse = vbRetry Then
txtUserName.SetFocus
Else
End
End If
End If
End Sub
Private Sub Form_Load()
'Set the username and password
strMyusername = "Brajmohan"
strMyPassword = "Gamer"
End Sub
Private Sub txtPassword_GotFocus()
txtPassword.SelStart = 0
txtPassword.SelLength = Len(txtPassword.Text)
End Sub
Private Sub txtUserName_GotFocus()
txtUserName.SelStart = 0
txtUserName.SelLength = Len(txtUserName.Text)
End Sub

Program for database handling with student database

Private Sub cmdAdd_Click()


'Add new Record
Data1.Recordset.AddNew
For Each ctrl In Form1.Controls
If TypeOf ctrl Is TextBox Then
With ctrl
.Locked = False
.Text = ""
End With
End If
Next
txtName.SetFocus

End Sub
Private Sub cmdFirst_Click()
'Move to the first record
Data1.Recordset.MoveFirst
End Sub
Private Sub cmdLast_Click()
'Move to the last Record
Data1.Recordset.MoveLast
End Sub
Private Sub cmdModify_Click()
'Unlock all the text boxes on form
For Each ctrl In Form1.Controls
If TypeOf ctrl Is TextBox Then
ctrl.Locked = False
End If
Next
Data1.Recordset.Edit
End Sub
Private Sub cmdNext_Click()
'Move to the next record
Data1.Recordset.MoveNext
'Check if end of file then move to the last record
If Data1.Recordset.EOF Then
Data1.Recordset.MoveLast
End If
End Sub
Private Sub cmdPrevious_Click()
'Move to the previous record
Data1.Recordset.MovePrevious
'Check if begining of file then move to the first record
If Data1.Recordset.BOF Then
Data1.Recordset.MoveFirst
End If
End Sub
Private Sub cmdRem_Click()
'Delete the current record
Data1.Recordset.Delete
Data1.Recordset.MoveNext
'Check if end of file then move to the last record

If Data1.Recordset.EOF Then
Data1.Recordset.MoveLast
End If
End Sub
Private Sub cmdSearch_Click()
Dim intRoll As Integer
'Ask for the roll number of student
intRoll = Val(InputBox("Enter the roll number of student do you want to search", "Roll number",
"0"))
'Move to the first record
Data1.Recordset.MoveFirst
'Loop through all the records of database
Do While Data1.Recordset.EOF = False
'Check if desired record is found then exit loop
If Data1.Recordset.Fields("rollno") = intRoll Then
Exit Do
End If
Data1.Recordset.MoveNext
Loop
'if end of file then record is not found and move to the last record
If Data1.Recordset.EOF Then
MsgBox "Cannot find the specified record ", vbInformation, "No such record"
Data1.Recordset.MoveLast
End If
End Sub
Private Sub cmdUpdate_Click()
'Update the record and lock all the text boxes of form
Data1.Recordset.Update
For Each ctrl In Form1.Controls
If TypeOf ctrl Is TextBox Then
ctrl.Locked = True
End If
Next ctrl
End Sub
Private Sub Form_Load()
'Connect to the database and bound all textboxes to that data control(data1)
Data1.DatabaseName = App.Path & "\student.mdb"
Data1.RecordSource = "student"

'Bound the controls


txtName.DataField = "name"
txtClass.DataField = "class"
txtAddress.DataField = "address"
txtAge.DataField = "age"
txtRoll.DataField = "rollno"
End Sub

Program to find whether both numbers are amicable or not

Option Explicit
Private Sub cmdOk_Click()
'Variable Declaration
Dim intFirstNo As Integer, intSecondNo As Integer, intSumFact1 As Integer
Dim intSumFact2 As Integer, intCounter As Integer
'Fetch the numbers from textboxes (txtFirstDig,txtSecNo)
intFirstNo = Val(txtFirstDig.Text)
intSecondNo = Val(txtSecDig.Text)
'Obtain the sum of factors of first number
For intCounter = 1 To intFirstNo - 1
If (intFirstNo Mod intCounter) = 0 Then
intSumFact1 = intSumFact1 + intCounter
End If
Next intCounter
'Obtain the sum of factors of second number
For intCounter = 1 To intSecondNo - 1
If (intSecondNo Mod intCounter) = 0 Then
intSumFact2 = intSumFact2 + intCounter
End If
Next intCounter

'Check if both sum of factors are equal to each other then numbers are amicable
If intSumFact1 = intSecondNo And intSumFact2 = intFirstNo Then
Label3.Caption = "Numbers are amicable"
Else
Label3.Caption = "Numbers are not amicable"
End If
End Sub

Program to Parsing the given string

Option Explicit
Private Sub cmdOk_Click()
'Variable Declaration
Dim intVowel As Integer, intWord As Integer
Dim intDigit As Integer, intSpace As Integer
Dim intSpChar As Integer
Dim strTemp As String, strInput As String
Dim intCounter As Integer, intAsc As Integer
Dim blnNextChar As Boolean
'Fetch the string from text box(txtInput) into strInput
strInput = txtInput.Text
blnNextChar = True
'Parse the string
For intCounter = 1 To Len(strInput)
strTemp = Mid(strInput, intCounter, 1)
intAsc = Asc(UCase(strTemp))
If strTemp = " " Then
intSpace = intSpace + 1

blnNextChar = True
Else
intAsc = Asc(UCase(strTemp))
If blnNextChar Then
intWord = intWord + 1
blnNextChar = False
End If
Select Case intAsc
Case 48 To 58
intDigit = intDigit + 1
Case 65 To 91
Select Case UCase(strTemp)
Case "A"
intVowel = intVowel + 1
Case "E"
intVowel = intVowel + 1
Case "O"
intVowel = intVowel + 1
Case "U"
intVowel = intVowel + 1
Case "I"
intVowel = intVowel + 1
End Select
Case Else
intSpChar = intSpChar + 1
End Select
End If
Next intCounter
'Print the output
lblVowel.Caption = "Total Vowels : " & intVowel
lblSpChar.Caption = "Total special characters : " & intSpChar
lblSpace.Caption = "Total Spaces : " & intSpace
lblWord.Caption = "Total Words : " & intWord
lblDigit.Caption = "Total Digits : " & intDigit
End Sub

Program to repeat the given string upto the length of string

Option Explicit
Private Sub cmdOk_Click()
'Variable Declaration
Dim intcounter As Integer
Dim strInput As String
'Fetch the string from textbox(txtinput) into strInput
strInput = txtInput.Text
'Clear the list contents if any
lstOutput.Clear
For intcounter = 1 To Len(strInput)
'Add the given string to list box(lstOutput)
lstOutput.AddItem (strInput)
Next intcounter
End Sub

Program to reverse the given number through function

Option Explicit
Private Sub cmdReverse_Click()
'Variable Declaration
Dim lngInput As Long
Dim lngOutput As Long
'Fetch the number from textbox(txtInput) into lngInput
lngInput = Val(txtInput.Text)
'Call the function Reverse for reverse the inputed number and
'Store the reversed number into lngOutput
lngOutput = Reverse(lngInput)
'Print the Reverse number
lblOutput.Caption = "The Reverse Number is " & Str(lngOutput)
End Sub
'User Defined Function to reverse the give number
Public Function Reverse(ByVal lngTemp As Long) As Long
Dim intRem As Integer
Dim lngResult As Long
While lngTemp > 0
intRem = lngTemp Mod 10
lngResult = (lngResult * 10) + intRem
lngTemp = lngTemp / 10
Wend
Reverse = lngResult
End Function

Program to sort the each row of given 3X3 matrix

Option Explicit
Private Sub Form_Activate()
'Variable Declaration
Dim intArray(2, 2) As Integer
Dim intRow As Integer, intCol As Integer
Dim intTemp As Integer, intNext As Integer
'Take input from user into matrix
For intRow = 0 To 2
For intCol = 0 To 2
intArray(intRow, intCol) = Val(InputBox("Enter the Element" _
& intRow & "Row " & intCol & "Col", "Enter Matrix", "0"))
Next intCol
Next intRow
Print "Matrix Before Sorting : ": Print: Print
For intRow = 0 To 2
For intCol = 0 To 2
Print Space(8) & intArray(intRow, intCol);
Next intCol
Print: Print
Next intRow

Print: Print: Print "Matrix After Sorting : ": Print: Print


'Sort the matrix
For intRow = 0 To 2
For intCol = 0 To 1
For intNext = intCol + 1 To 2
If intArray(intRow, intCol) > intArray(intRow, intNext) Then
intTemp = intArray(intRow, intCol)
intArray(intRow, intCol) = intArray(intRow, intNext)
intArray(intRow, intNext) = intTemp
End If
Next intNext
Next intCol
Next intRow
'Print The Output
For intRow = 0 To 2
For intCol = 0 To 2
Print Space(8) & intArray(intRow, intCol);
Next intCol
Print: Print
Next intRow
End Sub

Program to perform all common task of common dialog box

Private Sub Form_Resize()


rt1.Width = Form1.ScaleWidth
rt1.Height = Form1.ScaleHeight
End Sub
Private Sub mnuEditCopy_Click()
Clipboard.SetText (rt1.SelRTF)
End Sub
Private Sub mnuEditCut_Click()
Clipboard.SetText (rt1.SelRTF)
rt1.SelRTF = ""
End Sub
Private Sub mnuEditPaste_Click()
rt1.SelRTF = Clipboard.GetText
End Sub
Private Sub mnuFileExit_Click()
Unload Me
End Sub

Private Sub mnuFileNew_Click()


rt1.TextRTF = ""
End Sub
Private Sub mnuFileOpen_Click()
On Error Resume Next
cd1.ShowOpen
rt1.LoadFile (cd1.FileName)
End Sub
Private Sub mnuFileSave_Click()
On Error Resume Next
cd1.ShowSave
rt1.SaveFile (cd1.FileName)
End Sub
Private Sub mnuFormatFont_Click()
On Error Resume Next
cd1.Flags = cdlCFEffects Or cdlCFBoth
cd1.ShowFont
rt1.SelFontName = cd1.FontName
rt1.SelColor = cd1.Color
rt1.SelBold = cd1.FontBold
rt1.SelItalic = cd1.FontItalic
rt1.SelFontSize = cd1.FontSize
rt1.SelStrikeThru = cd1.FontStrikethru
rt1.SelUnderline = cd1.FontUnderline
End Sub

Program to rotate 4 images

Option Explicit
Dim strPath1 As String, strPath2 As String, strPath3 As String, strPath4 As String
Private Sub Image1_Click()
End Sub
Private Sub cmdStartStop_Click()
Static blnState As Boolean
blnState = Not blnState
Timer1.Enabled = blnState
If blnState Then
cmdStartStop.Caption = "Stop Rotation"
Else

cmdStartStop.Caption = "Start Rotation"


End If
End Sub
Private Sub Form_Load()
'Load all the image files from application path
strPath1 = App.Path & "\b1.jpg"
strPath2 = App.Path & "\b2.jpg"
strPath3 = App.Path & "\b3.jpg"
strPath4 = App.Path & "\b4.jpg"
'load the images
imgScr1.Picture = LoadPicture(strPath1)
imgScr2.Picture = LoadPicture(strPath2)
imgScr3.Picture = LoadPicture(strPath3)
imgScr4.Picture = LoadPicture(strPath4)
End Sub
Private Sub Timer1_Timer()
Static bytCounter As Byte
Select Case bytCounter
Case 0
imgScr1.Picture = LoadPicture(strPath4)
imgScr2.Picture = LoadPicture(strPath1)
imgScr3.Picture = LoadPicture(strPath2)
imgScr4.Picture = LoadPicture(strPath3)
bytCounter = 1
Case 1
imgScr1.Picture = LoadPicture(strPath3)
imgScr2.Picture = LoadPicture(strPath4)
imgScr3.Picture = LoadPicture(strPath1)
imgScr4.Picture = LoadPicture(strPath2)
bytCounter = 2
Case 2
imgScr1.Picture = LoadPicture(strPath2)
imgScr2.Picture = LoadPicture(strPath3)
imgScr3.Picture = LoadPicture(strPath4)
imgScr4.Picture = LoadPicture(strPath1)
bytCounter = 3
Case 3
imgScr1.Picture = LoadPicture(strPath1)
imgScr2.Picture = LoadPicture(strPath2)
imgScr3.Picture = LoadPicture(strPath3)
imgScr4.Picture = LoadPicture(strPath4)
bytCounter = 0
End Select
End Sub

Program to find whether a matrix is symmetric or not

Option Explicit
Dim intMatrix(2, 2) As Integer
Private Sub Form_Activate()
'Variable Declaration
Dim intRow As Integer, intCol As Integer
Dim blnIsSymetric As Boolean
blnIsSymetric = True
'Take input in 3X3 Matrix
For intRow = 0 To 2
For intCol = 0 To 2
intMatrix(intRow, intCol) = InputBox("Enter the " _
& "Matrix element " & intRow & " " & intCol, "Enter element", "0")
Next intCol
Next intRow
'Evaluate the square of matrix
For intRow = 0 To 2
For intCol = 0 To 2
If intMatrix(intRow, intCol) <> intMatrix(intCol, intRow) Then
blnIsSymetric = False
Exit For
End If
Next intCol
Next intRow

'Print the matrix of form


For intRow = 0 To 2
Print: Print
For intCol = 0 To 2
Print intMatrix(intRow, intCol) & Space(10);
Next intCol
Next intRow
Print: Print
If blnIsSymetric = True Then
Print "Matrix is symetric"
Else
Print "Matrix is not symetric"
End If
End Sub

Program To Make Digital Watch

Option Explicit
Private Sub Timer1_Timer()
Label2.Caption = Time
End Sub

Program To Make a Basic Calculator

Dim dblPrev As Double, dblNow As Double


Dim strOpt As String
Private Sub cmdAdd_Click()
dblPrev = Val(txtNum.Text)
strOpt = "+"
txtNum.Text = ""
End Sub
Private Sub cmdBS_Click()
txtNum.Text = Left(txtNum.Text, Len(txtNum.Text) - 1)
End Sub
Private Sub cmdClear_Click()
dblPrev = 0
dblNow = 0
txtNum.Text = ""
strOpt = "+"
End Sub
Private Sub cmdDigit_Click(Index As Integer)
txtNum.Text = txtNum.Text + cmdDigit(Index).Caption
End Sub

Private Sub cmdDivide_Click()


dblPrev = Val(txtNum.Text)
strOpt = "/"
txtNum.Text = ""
End Sub
Private Sub cmdEqual_Click()
dblNow = txtNum.Text
Select Case strOpt
Case "+"
txtNum.Text = dblPrev + dblNow
Case "-"
txtNum.Text = dblPrev - dblNow
Case "*"
txtNum.Text = dblPrev * dblNow
Case "/"
txtNum.Text = dblPrev / dblNow
Case Else
MsgBox "Invalid Operation"
End Select
dblPrev = dblNow
End Sub
Private Sub cmdMul_Click()
dblPrev = Val(txtNum.Text)
strOpt = "*"
txtNum.Text = ""
End Sub
Private Sub cmdSign_Click()
txtNum.Text = -(txtNum.Text)
End Sub
Private Sub cmdSqrt_Click()
txtNum.Text = Sqr(txtNum.Text)
End Sub
Private Sub cmdSub_Click()
dblPrev = Val(txtNum.Text)
strOpt = "-"
txtNum.Text = ""
End Sub

You might also like