• Increase font size
  • Default font size
  • Decrease font size
Visual Basic / VBA

How to Export Data from ListView in Visual Basic 6 to Excel

E-mail Print PDF

Assume that we have a ListView contains data in four columns. We want to export all data in ListView to a new sheet of Microsoft Excel. Below is a sample code in Visual Basic 6 use to export data from ListView to Microsoft Excel.

    Dim ExcelObj As Object
    Dim ExcelBook As Object
    Dim ExcelSheet As Object
    Dim i As Integer

    Set ExcelObj = CreateObject("Excel.Application")
    Set ExcelBook = ExcelObj.WorkBooks.Add
    Set ExcelSheet = ExcelBook.WorkSheets(1)

    With ExcelSheet
        For i = 1 To lvwList.ListItems.Count
            .cells(i, 1) = lvwList.ListItems(i).Text
            .cells(i, 2) = lvwList.ListItems(i).SubItems(1)
            .cells(i, 3) = lvwList.ListItems(i).SubItems(2)
            .cells(i, 4) = lvwList.ListItems(i).SubItems(3)
    End With
    ExcelObj.Visible = True
    Set ExcelSheet = Nothing
    Set ExcelBook = Nothing
    Set ExcelObj = Nothing
Download Sample Code

How to Open Report in Microsoft Access from Visual Basic 6

E-mail Print PDF

This sample shows you how to open report in Microsoft Access from Microsoft Visual Basic 6 coding.

  • Required Reference: Microsoft Access x.x Object Library
    How to add reference: Project -> References -> (Tick) Microsoft Access x.x Object Library)
  • x.x is the version of Microsoft Access and can be 11.0, 12.0, etc.

Private db As New Access.Application

Private Sub Command1_Click()
    Dim rpt As New Report
    Dim ctl As Control
    db.OpenCurrentDatabase App.Path & "\db1.mdb", True
    db.Visible = True
    Dim strSource As String
    strSource = "SELECT * FROM tblStudent WHERE Sex='m'"
    db.DoCmd.OpenReport "rptStudent", acViewDesign, , , , "m"
    db.Reports("rptStudent").RecordSource = strSource
    db.DoCmd.OpenReport "rptStudent", acViewPreview
    Set rpt = Nothing
End Sub

Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
    Set db = Nothing
End Sub
Download Sample Code

VB Programming: MSHFlexGrid Input TextBox in VB6

E-mail Print PDF

This sample shows how to use input text box and check box in MHSFlexGrid Control in VB6. Normally, we cannot entry value directly into MHSFlexGrid’s cells, so we need to create our own textbox and place to each cell programmatically to entry value or change value of the MHSFlexGrid’s cells.

In this sample, please use to move cell, to enter value or to enter value. You also can on each cell to change text color and background color of the current cell.

Download Full Source Code

VB Programming: Drive Show and Folder Show in VB6

E-mail Print PDF

This is VB6 sample code use to show all drives exist in your computer. Drill down or expand each drive to see more folders and files.

Sample Code
Option Explicit
Private fso

Private Sub Form_Load()
    Dim Drv
    Dim nodeX As Node
    Dim nodeY As Node
    Set fso = CreateObject("Scripting.FileSystemObject")
    For Each Drv In fso.Drives
        Set nodeX = TreeView1.Nodes.Add(, , Drv.Path, Drv.DriveLetter, 1)
        Set nodeY = TreeView1.Nodes.Add(nodeX, tvwChild)
End Sub

Private Sub TreeView1_Expand(ByVal Node As MSComctlLib.Node)
    On Error Resume Next
    Dim lPath As String
    Dim fDir, fName, ffile, ffname
    Dim fNode As Node
    Dim nNode As Node
    lPath = Node.Key
    Do While Node.Children > 0
        TreeView1.Nodes.Remove Node.Child.Index
    If Right(lPath, 1) <> "\" Then lPath = lPath & "\"
    Set fDir = fso.GetFolder(lPath)
    For Each fName In fDir.subfolders
            Set fNode = TreeView1.Nodes.Add(Node.Key, tvwChild, _
                                            fName.Path, fName.Name, 2)
            Set nNode = TreeView1.Nodes.Add(fName.Path, tvwChild)

    Set ffname = fDir.Files
    Dim x As Long
    For Each fName In ffname
        x = fName.Size \ 1024
        Set fNode = TreeView1.Nodes.Add(Node.Key, tvwChild, _
                                        fName.Path & fName.Name, fName.Name & "   " & _
                                        IIf((x \ 1024) > 0, x \ 1024 & " MB", _
                                                            IIf(x > 0, x & " KB", _
                                                            fName.Size & " bytes")), 3)
End Sub
Download Sample Code Here

VB Programming: Create Typing English in VB6

E-mail Print PDF

This is a sample code programmed in Visual Basic 6. It will guide you to create Typing Application.

Sample code here

Private Sub Form_KeyPress(KeyAscii As Integer)
    If curInd < Len(strWord) Then
        If Asc(Mid(strWord, curInd + 1, 1)) = KeyAscii Then
            If curInd < Len(strWord) Then
                SetLetterColor curInd, vbBlack, False, 8
                curInd = curInd + 1
                SetLetterColor curInd, vbRed, True, 10
                If curInd = Len(strWord) Then
                    curInd = 0
                    SetLetterColor curInd, vbRed, True, 10
                End If
            End If
        End If
    End If
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = 16 Then _
        Me.cmdShift.BackColor = &H8000000F
    If KeyCode = 17 Then _
        Me.cmdLeftCtrl.BackColor = &H8000000F
    If KeyCode = 18 Then _
        Me.cmdLeftAlt.BackColor = &H8000000F
End Sub

Download Source Code Here

Page 3 of 4