Showing posts with label Visual Basic. Show all posts
Showing posts with label Visual Basic. Show all posts

Thursday, 30 August 2012

VB Program - To Create a Notepad(.txt) File and Save the data


VB Program - To Create a Notepad(.txt) File and Save the data

Steps:

1.For Creating a text file in a Vb program we use File System Object.
2.CreateObject ("scripting.filesystemobject") - This is  used to create the object of the file.
3.After creating the file object we open the file using opentextfile(location) function.
4.Once the file is opened then writeline is used to write text in the file that has been created above.
5.After writing in to the file ,the file object has to be closed using Close.

Here is a demo to create and then write something in a text file :
The below code will create the Text file(Ia2Tech.txt) and then write the data("Its all about Technology") in the file which will be saved at "D:\Ia2Tech.txt" and then will close it.

Program :
Sub Notepad()

Set objFS = CreateObject("scripting.filesystemobject")
Set txtFile= objFS.opentextfile("D:\Ia2Tech.txt", 8, True)
txtFile.writeline "Its all about Technology"
txtFile.Close

End Sub

Note : If the File is already there at a specified location then the data will be appended to the data already present in the file.

VB Program - To Reverse the n Digit Number entered by a user


VB Program - To Reverse the n Digit Number entered by a user

Program :

Here n= 5,this can be modified in the below code.

Sub ReverseNumber()

Dim varNum
Dim varRev
varNum = InputBox("Please Enter a number")
If Len(varNum) <> 5 Then
MsgBox varNum + " is not a 5 digit number"
Else
varRev = varRev * 10 + varNum Mod 10
varNum = varNum / 10
varNum = Left(varNum, 4)
varRev = varRev * 10 + varNum Mod 10
varNum = varNum / 10
varNum = Left(varNum, 3)
varRev = varRev * 10 + varNum Mod 10
varNum = varNum / 10
varNum = Left(varNum, 2)
varRev = varRev * 10 + varNum Mod 10
varNum = varNum / 10
varNum = Left(varNum, 1)
varRev = varRev * 10 + varNum Mod 10
MsgBox "Reverse Order of the entered number is " & varRev
End If

End Sub

VB Program - To Calculate the Simple Interest


VB Program - To Calculate the Simple Interest

Program :

Sub SimpleInterest()

Dim varPA
Dim varT
Dim varROI
Dim varSI
varPA = InputBox("Enter Principle Amount")
varT = InputBox("Enter Time(Yrs)")
varROI = InputBox("Enter Rate of Interest")
varSI = (varPA * varT * varROI) / 100
MsgBox "Simple Interest is " + CStr(varSI)
'Cstr()- Function converts the value to String

End Sub

VB Program - To find out whether the number is Even number or Odd number?


VB Program - To find out whether the  number is Even number or Odd number?

Program: 

Sub EvenOdd()

Dim varNum
varNum = InputBox("Enter a number")
If varNum Mod 2 = 0 Then
MsgBox varNum + "  is a even number"
Else
MsgBox varNum + "  is a odd number"

End If

VB Program - To find out whether the given year is a leap year or not?

VB Program - To find out whether the given year is a leap year or not?

Program : 

Sub LeapYear()

Dim varYear
varYear = InputBox("Enter the Year")
If varYear Mod 4 = 0 Then
MsgBox varYear + " is a Leap year"
Else
MsgBox varYear + " is not a Leap year"
End If


End Sub

Saturday, 11 August 2012

VB Program - Palindrome

Visual Basic (VB) program to check whether a string is Palindrome or not.

- Use of StrReverse() Function

Program :

Sub Palindrome()

Dim s As String
s = InputBox("Enter a string")
If s = StrReverse(s) Then
MsgBox s & (" is a palindrome")
Else
MsgBox s & (" is not a palindrome")
End If

End Sub

Note: Copy the code and run in Microsoft Visual Basic Editor.(Microsoft Excel)