Visual Basic Sample Code

Visual Basic Sample Code is a page for beginners. If you have just started learning programming, these sample codes will help you take a first look at different variations of visual basic programming.

VB can be used in many different ways to create automation and applications to accomplish a desired task. Let's take a look at sample code for 5 important variations.

An Excel Visual Basic Sample Code

The Task: Write a Macro in Excel to rename all the sheets using the value in cell A1 from each sheet.Sub RenameSheets()
Dim WS As Worksheet
For Each WS In ThisWorkbook.Sheets
    WS.Select
    ActiveSheet.Name = Range("A1").Value
Next
End Sub

An Access Visual Basic Sample Code

The Task: Create sub-orders from Original_Orders table based on a percentage value provided by the user through a Form. The sub-orders are created in the Sub_Orders table.

Sub CreateSubOrders()
Dim iPerc As Integer, sInsertSQL As String
iPerc = Val(Nz(txt_Perc, ""))
If iPerc = 0 Or iPerc > 100 Then
    MsgBox "Invalid percentage value", vbCritical
    Exit Sub
End If
sInsertSQL = "Insert Into Sub_Orders(Order_ID, Item, Sub_Order_Qty) Select Order_ID, Item, Original_Qty * " & iPerc / 100 & " From Original_Orders"
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * from Sub_Orders"
DoCmd.RunSQL sInsertSQL
DoCmd.SetWarnings True
MsgBox "Sub-orders created successfully", vbInformation
End Sub

A Visual Basic 6.0 Sample Code

The Task: Create a file merge program. The user provides location of 2 text files. The code should create a single merged file in the folder from where the application is running.Sub MergeFiles()
Dim Txt As String, File1 As String, File2 As String, WriteFile As String
File1 = Trim(txt_SourceFile1)
File2 = Trim(txt_SourceFile2)
WriteFile = App.Path & "\Merged_File.txt"

On Error GoTo ErrHandler
Open File1 For Input As #1
Open WriteFile For Output As #2

Do Until EOF(1)
    Line Input #1, Txt
    Txt = Trim(Txt)
    Print #2, Txt
Loop
Close #1

Open File2 For Input As #1
Do Until EOF(1)
    Line Input #1, Txt
    Txt = Trim(Txt)
    Print #2, Txt
Loop
Close #1

Close #2
MsgBox "Merged File Created: " & vbCr & WriteFile, vbInformation

Exit Sub
ErrHandler:
MsgBox Err.Description, vbCritical
Reset
Err.Clear
End Sub

A Visual Basic 2008 Sample Code

The Task: Create the same file merge program using Visual Basic 2008. The user provides location of 2 text files. The code should create a single merged file in the folder from where the application is running.

Sub MergeFiles()
Dim sTxt As String, sReadFile1 As String, sReadFile2 As String, sWriteFile As String
Dim oRead As System.IO.StreamReader, oWrite As System.IO.StreamWriter

sReadFile1 = Trim(txt_SourceFile1.Text)
sReadFile2 = Trim(txt_SourceFile2.Text)

sWriteFile = Application.StartupPath & "\Merged_File.txt"
oWrite = IO.File.CreateText(sWriteFile)

On Error GoTo ErrHandler

oRead = IO.File.OpenText(sReadFile1)
Do Until oRead.EndOfStream
    sTxt = oRead.ReadLine
    sTxt = Trim(sTxt)
    oWrite.WriteLine(sTxt)
Loop
oRead.Close()

oRead = IO.File.OpenText(sReadFile2)
Do Until oRead.EndOfStream
    sTxt = oRead.ReadLine
    sTxt = Trim(sTxt)
    oWrite.WriteLine(sTxt)
Loop
oRead.Close()
oWrite.Close()

MsgBox("Merged File Created: " & vbCr & sWriteFile, MsgBoxStyle.Information)
Exit Sub

ErrHandler:
MsgBox(Err.Description, MsgBoxStyle.Critical)
Err.Clear()
End Sub

A Script - Visual Basic Sample Code

The Task: Create a VB Script file to automatically login to a FTP site and download a file. sLocalFolder = "c:\temp"
sFTPServer = "70.80.90.100"
sLoginID = "max"
sPassword = "max"
sFTPFolder = "www"
sFile2Get = "index.htm"
sFTPScriptFile = sLocalFolder & "\FTPBatch.txt"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oMyFile = oFSO.CreateTextFile(sFTPScriptFile, True)

oMyFile.WriteLine ("open " & sFTPServer)
oMyFile.WriteLine (sLoginID)
oMyFile.WriteLine (sPassword)
oMyFile.WriteLine ("cd " & sFTPFolder)
oMyFile.WriteLine ("lcd " & sLocalFolder)
oMyFile.WriteLine ("get " & sFile2Get)
oMyFile.WriteLine ("bye")
oMyFile.Close

Set oShell = CreateObject("WScript.Shell")
oShell.Run "ftp -s:" & sFTPScriptFile & " >>c:\temp\FTPLog.txt", 1, True
Set oFSO = Nothing
Set oMyFile = Nothing
Set oShell = Nothing
So these are the 5 type of visual basic sample code. If you are interested to learn more you can download the full sample programs.

Back to Visual Basic for Beginners