Microsoft Access VBA Tutorials for self-paced learning, Class Modules, SQL Techniques, and AI Integration Guides.

Assigning Images To ListView Items Tutorial-03

The ImageList ActiveX Control.

To assign images to the ListView control, we need the support of another ActiveX control: the ImageList control. You may have noticed how Windows Explorer displays icons—such as folder icons in the closed state, open when the folder is selected, and displaying files with different icons based on the file type. While the ListView control does not provide that level of flexibility, it does allow us to display icon images in Column Headers, List Items, and ListSubItems when populating their contents. 

The Sample Demo Images.

Sample Preview

The following image shows an example of the Northwind Trading Employees listing, where each employee’s details are displayed in the ListView control along with their small photo icons. These images are assigned through the ImageList control and linked to the ListView items, making the data both informative and visually appealing.

Note: Using larger image sizes will automatically increase the row height of the ListView records, so you can display bigger photos if required.

The following sample image shows the ListView control (displayed in the right-side panel) used together with the TreeView control. The TreeView ActiveX control was already introduced in an earlier tutorial series. For quick reference, you can find the list of TreeView Control Tutorial Series links at the bottom of this page.

In the above image, icons have been applied to all data columns and the column header labels to demonstrate how images can be displayed in the TreeView Control.

On the left panel, the TreeView control nodes show the familiar folder icons in open and closed states. These behave differently from the ListView items: when a TreeView node is clicked, the open-folder image is displayed; clicking the same node again reverts it to the closed-folder image.

The ListView Control Programming Tutorial Series.

I hope you have already gone through the ListView Control Tutorial Sessions 1 and 2 and are now ready to continue with this new episode on using the ImageList Control along with the ListView Control.

For your convenience, the links to the earlier tutorials are provided below. I encourage you to review them before proceeding, as they cover basic concepts of the ListView Control along with supporting VBA code. This background will put you in a better position to follow along and clearly understand the new features we are adding in this session.

  1. ListView Control Tutorial-01.

  2. ListView Control Tutorial-02.

Source Data and Demo Form.

  1. Let’s begin by creating a new Form and preparing the Employees Table for our ListView Control demo project.

    1. Import the Employees Table from the NorthWind.accdb sample database.

    2. Create a new SELECT Query using the SQL statement provided below.

    3. Save this query with the name EmployeesQ.

    SELECT [TitleOfCourtesy] & " " & [FirstName] & " " & [LastName] AS [Employee Name], 
    Employees.EmployeeID, 
    Employees.Title, 
    Employees.HireDate, 
    Employees.Address, 
    Employees.City, 
    Employees.Region, 
    Employees.PostalCode, 
    Employees.Country, 
    Employees.HomePhone, 
    Employees.Extension, 
    Employees.Notes
    FROM Employees;
    
  2. If your Employees Table structure is different, don’t worry. For the first column value, I have combined three fields together to form [Employee Name]. For the remaining columns, you can use whatever fields you have—include all of them or just a few, and in any order you prefer.

    Steps to Set Up the Form

    1. Create a new Form and open it in Design View.

    2. From the ActiveX Controls List, insert a Microsoft ListView Control.

    3. From the same list, insert a Microsoft ImageList Control.

    4. Resize the ListView Control as shown in the sample image of the form (provided earlier).

    5. Move the ImageList Control to the top-right corner of the ListView Control, as shown in the demo image. You can also place it in any convenient location on the form.

    Note: The ImageList Control will not be visible when the form is opened in Normal View; it appears only in Design View for configuration.

  3. Select the ListView Control and open its Property Sheet.

    • Change the Name property to ListView1.

  4. Select the ImageList Control, open its Property Sheet, and

    • Change the Name property to ImageList0.

    Important: Both the ListView and ImageList controls come with their own dedicated Property Sheets. Some of their property names and values may also appear in the Access Property Sheet, but updates made there may not always reflect correctly in the controls themselves. For reliable results, always make changes in each control’s own Property Sheet.

    ListView Control Property Sheet.

  5. Right-click on the ListView Control, point to the ListViewCtrl Object option from the shortcut menu, and then select Properties.

    The General tab of the ListView Control Property Sheet will appear, as shown in the image below. 

  6. Change the property values on the General tab as shown in the image above.

    Let us begin by loading the Employees data into the ListView Control.

    The Form Module VBA Code

  7. Copy and Paste the following VBA Code into the Form's Class Module:

    Option Compare Database
    Option Explicit
    
    Dim lvwList As MSComctlLib.ListView
    Dim lvwItem As MSComctlLib.ListItem
    Dim ObjImgList As MSComctlLib.ImageList
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    
    
    Private Sub cmdClose_Click()
       DoCmd.Close acForm, Me.Name
    End Sub
    
    Private Sub Form_Load()
     Call LoadListView("EmployeesQ")
    End Sub
    
    Private Sub LoadListView(ByVal tblName As String)
        Dim strFldName As String
        Dim intCounter As Integer
        Dim j As Integer
        Dim strLabel As String
    
    'Assign ListView Control on Form to lvwList Object
    Set lvwList = Me.ListView1.Object
    'Set ObjImgList = Me.ImageList0.Object
        
    'Assign Form Header labels Caption Text
     strLabel = UCase(tblName) & " " & "IN LISTVIEW CONTROL - TUTORIAL-03"
     Me.Label8.caption = strLabel
     Me.Label9.caption = strLabel
     
     With lvwList
        '.Icons = ObjImgList
        '.SmallIcons = ObjImgList
        '.ColumnHeaderIcons = ObjImgList
        .Font = "Verdana"
        .Font.Size = 10
        .Font.Bold = True
     End With
     
     Set db = CurrentDb
     Set rst = db.OpenRecordset(tblName, dbOpenSnapshot)
     
     'Create Column Headers for ListView
     With lvwList
        .ColumnHeaders.Clear 'initialize header area
        For j = 0 To rst.Fields.Count - 1
            strFldName = rst.Fields(j).Name
       'Syntax:
       '.ColumnHeaders.Add Index, Key, Text, Width, Alignment, Icon
            .ColumnHeaders.Add , , strFldName, iif(j=0,3200,2000)
        Next
     End With
     
     'Initialize ListView Control
      While lvwList.ListItems.Count > 0
            lvwList.ListItems.Remove (1)
      Wend
    
     With lvwList
     Do While Not rst.EOF And Not rst.BOF
    
       'Syntax  .ListItems.Add(Index, Key, Text, Icon, SmallIcon)
            Set lvwItem = .ListItems.Add(, , CStr(Nz(rst.Fields(0).Value, "")))
            
       'Add next columns of data as sub-items of ListItem
            With lvwItem
       'Syntax     .Add Index,Key,Text,Report Icon,TooltipText
             For j = 1 To rst.Fields.Count - 1
                .ListSubItems.Add , , CStr(Nz(rst.Fields(j).Value, ""))
             Next
    
           End With
           rst.MoveNext
    Loop
    rst.Close
        'reset lvwItem object
        Set lvwItem = Nothing
    End With
    
    Set rst = Nothing
    Set db = Nothing
    End Sub
    
    

    Note:  The VBA lines in red in the ListView Control are commented out, and we will enable them shortly.

  8. Save your Form with the Name frmEmployees.

  9. Open the Form in Normal View. 

    The EmployeesQ Query Records Listing will look like the following Image:

  10. Review of the VBA Code

    We have already reviewed most of the above VBA code in ListView Control Tutorial Sessions 01 and 02, with only a few additions specific to the ImageList Control. These include its declaration, initialization, and a few lines for setting the Font Name, Font Size, and Font Style.

    Another important change is in the LoadListView() procedure. In this version, the procedure accepts a Table or Query Name as a parameter. All query types—except Action Queries—as well as Access Tables and Linked Tables, are valid inputs. The specified Table or Query name is supplied when the program is called from the Form_Load() Event Procedure.

    All field names from the given Table or Query are used as Column Header Labels (the third parameter) in the ColumnHeaders.Add() method. The Index and Key parameters (first and second) are not used here. The system automatically assigns Index values in sequence.

    • The fourth parameter specifies the column width in pixels. In our example, the first column is set to 3200 pixels to accommodate the Employee Name, while all other columns are set to 2000 pixels.

    • Alignment and Icon parameters for column headers are not used. By default, column headers are left-aligned. The available alignment options are:

      • 0 - lvwColumnLeft

      • 1 - lvwColumnRight

      • 2 - lvwColumnCenter

    You can view these options on the Column Headers Tab of the ListView Control Property Sheet:

    1. Click the Insert Column button and enter a temporary column name.

    2. Open the Alignment property to view the available options.

    3. Click Remove Column to delete the temporary column.

    Note: If you prefer to add Column Header Labels manually, instead of loading field names through VBA code, you can type them directly here. They will then appear as column headers when the data is displayed.

    A sample view of the Icon image, on the left side of the header column names, can be seen in the second demo image in the right-side panel at the top of this page.

    For the data itself:

    • The first column (Employee Name from the EmployeesQ query) is assigned to the ListItems.Text property using the .Add method. Here too, the Index and Key parameters are omitted—the system automatically inserts Index numbers as serial values.

    • From the second field onward, column values are added through the ListSubItems.Add() method of the ListView Control.

    Note: All values are stored as Text in ListItems.Text and ListSubItems.Text, regardless of their original data type in the source Table/Query. To prevent errors, the code checks for Null values and converts them to text using the CStr() function.

    The ImageList control.

    In the main program, the ImageList Control initialization statements have been temporarily commented out. These lines are highlighted in red in the code segment below. We will revisit, explain, and enable them once we are ready to proceed with uploading images into the ImageList Control.

    'Assign ListView Control on Form to lvwList Object
    Set lvwList = Me.ListView1.Object
    'Set ObjImgList = Me.ImageList0.Object
        
    'Assign Form Header labels Caption Text
     strLabel = UCase(tblName) & " " & "IN LISTVIEW CONTROL - TUTORIAL-03"
     Me.Label8.caption = strLabel
     Me.Label9.caption = strLabel
     
     With lvwList
        '.Icons = ObjImgList
        '.SmallIcons = ObjImgList
        '.ColumnHeaderIcons = ObjImgList
        .Font = "Verdana"
        .Font.Size = 10
        .Font.Bold = True
     End With
     

    The first red-highlighted statement initializes the ObjImgList object with the ImageList0 control placed on the frmEmployees form. Before making changes to the code, let’s first explore the available options for uploading images into the ImageList Control.

    About Uploading Images.

    The next step is to upload some sample images into the ImageList Control. This can be done in one of two ways.

    Before starting, prepare at least two small images in any popular format—such as JPG, JPEG, BMP, or PNGBMP is the preferred type. The ImageList Control supports the following standard image sizes (available on the General tab of its Property Sheet): 16×16, 32×32, 48×48 pixels, or a Custom size.

    To configure this:

    1. Right-click on the ImageList Control,

    2. Highlight the ImageListCtrl Object,

    3. Select Properties,

    4. Then, on the General tab, choose your desired image size before uploading any images.

    If you have larger images and want to keep their original size, select the Custom option. Otherwise, choosing a predefined size will automatically scale the images, which may reduce their quality. Keep in mind that using very large images will increase the row height when displayed in the ListView Control.

    For best results, icon-style images are ideal. However, you should experiment with different sizes—large, small, and very small—along with the available options to determine what works best for your project.

    You can upload images into the ImageList Control in one of the following two ways:

    1. Upload Images from disk through VBA Procedure.

    The sample VBA Procedure will look like the Code Segment given below, taken from the  TreeView Control Tutorial:

      
      Set objImgList = Me.ImageList0.Object
      objImgList.ListImages.Clear
      
    strFolder = "D:\Access\TreeView\"
    With objImgList
        With .ListImages
             .Add Index:=1, Key:="FolderClose", Picture:=LoadPicture(strFolder & "folderclose2.bmp")
             .Add Index:=2, Key:="FolderOpen", Picture:=LoadPicture(strFolder & "folderopen2.bmp")
             .Add Index:=3, Key:="ArrowHead", Picture:=LoadPicture(strFolder & "arrowhead.bmp")
        End With
    End With
    
    With tvw 'TreeView Control
        .ImageList = objImgList 'assign imagelist Object to TreeView Imagelist Property
    End With

    The first statement initializes the objImgList object with the ImageList0 control on the form.

    The next statement clears any existing images in the ImageList control, ensuring it is ready for new uploads from disk. For this method to work consistently, the required image files must always be available on the disk.

    The method objImgList.ListImages.Add()  is then used to upload images from disk. When using named parameters, the parameter values can be provided in any order. For example:

    • Index := 1 can appear at the end of the line,

    • Key := "FolderClose" can be listed first,

    and so on.

    However, if parameter names are omitted, the parameters must be supplied in the following order:

             .Add 1, "FolderClose", LoadPicture(strFolder & "folderclose2.bmp")

    To display an image in the ListView control, you can reference it either by using the Image Index Number (e.g., 1) or by specifying the Key value (e.g., "FolderClose") as the Icon or SmallIcon parameter in the ListItems.Add() method.

    We used this same approach earlier in the TreeView Control Tutorial. You may refer to that page and download the demo database for reference.

    This method loads the images into the ImageList object instance in memory, without altering the physical ImageList control on the form. However, it is important to note that the source images on disk must always be available every time frmEmployees is opened.

    2. Uploading Images from disk manually.

    This is a one-time setup task: locating the images on disk and uploading them into the ImageList Control.

    The key advantage of this method is that once images are uploaded into the ImageList Control, they remain embedded in the control. You won’t need to reload them from disk each time the form is opened. Moreover, the ImageList control—with the images included—can be copied and reused in other projects, or even shared with colleagues, eliminating the need for duplicate image setup.

    For this demonstration, let’s use the manual upload method, which is the more reliable approach. Prepare two sample .bmp images with a resolution of 50 x 50 pixels (e.g., image1.bmp, image2.bmp) and keep them ready in a folder, such as D:\Access\, for reference.

    Now, follow these steps:

    1. Open frmEmployees in Design View.

    2. Right-click on the ImageList Control, highlight ImageListCtrl Object, and select Properties.

    3. On the General tab, select the Custom option to retain the original resolution of the uploaded images.

    At this point, the General tab of the ImageList Control will appear as shown in the image below.

    The Images tab View of the ImageList Control

    Note: After testing uploaded images in the ListView control, if you wish to try a different size option (48×48, 32×32, or 16×16), you must first remove all existing images. Then, return to the General tab, select the new size option, and upload the images again. The uploaded images will automatically be resized to match the selected option.

    In the sample below, two images have been uploaded using the Insert Picture command button. The first image is currently selected, shown in a slightly raised position. The Index control displays the value:1, while the Key textbox shows the text First. The Index value is generated automatically, but the Key value must be entered manually. Use a meaningful Key name that is easy to remember and logically relates to the data.

    Both the Index number and the Key text can be used in the Icon or SmallIcon parameters of the ListItems.Add() method.

    If you plan to rely on Index numbers, make sure that the image upload sequence matches the data sequence in the ListView (for example, each employee’s name aligns correctly with their photo). However, a more practical approach is to use Key text values—such as an employee’s first name—since they are easier to associate directly with records. For generic icons, descriptive Key names (e.g., FolderClosed, FolderOpen) provide clarity about their purpose.

    Steps to upload images:

    1. Open the Images tab of the ImageList Control.

    2. Click Insert Picture, browse the file  'D:\Access\Image1.bmp' to select it, and click Open to upload the image.

    3. In the Key textbox, type a unique Key value (e.g., First).

    4. Repeat steps 2–3 for the second image (e.g., D:\Access\Image2.bmp), assigning it another unique Key value.

    Your ImageList Control is now configured with sample images and ready to display them in the ListView Control.

Assigning ImageList Object to ListView Object Properties.

To display images in the ListView Control, the following ListView Object properties must be linked to the ImageList Object:

  • ListView.ColumnHeaderIcons

  • ListView.Icons

  • ListView.SmallIcons

The next step is to assign the ImageList Object to the ListView Control through these properties in VBA code:

  • lvwList.ColumnHeaderIcons

  • lvwList.Icons

  • lvwList.SmallIcons

This must be done before you can use image references (Index or Key values) in the following methods:

  • ColumnHeaders.Add()

  • ListItems.Add()

  • ListSubItems.Add()

We have already added the necessary VBA statements in the LoadListView() procedure of the main program, but they are currently commented out. To activate them:

  1. Open the LoadListView() procedure.

  2. Locate the four lines of code (highlighted earlier in red).

  3. Remove the comment symbol (') at the beginning of each line to enable them.

  4. Update the code to include the appropriate Icon Index values in the method parameters.

For example, modify the following statements (originally shown in red in the main program) to use Icon Index numbers 1 and 2 for the Icon  SmallIcon parameters:

' Example modification in LoadListView() Set lvwList.ColumnHeaderIcons = objImgList Set lvwList.Icons = objImgList Set lvwList.SmallIcons = objImgList lvwList.ListItems.Add , , "Employee 1", , 1 ' Icon Index = 1 lvwList.ListItems.Add , , "Employee 2", , 2 ' SmallIcon Index = 2

This ensures that images stored in the ImageList Control are correctly displayed alongside the ListView items.

 With lvwList
 Do While Not rst.EOF And Not rst.BOF

   'Syntax  .ListItems.Add(Index, Key, Text, Icon, SmallIcon)
       ' Set lvwItem = .ListItems.Add(, , CStr(Nz(rst.Fields(0).Value,"")))
       'Change to 
         Set lvwItem = .ListItems.Add(, , CStr(Nz(rst.Fields(0).Value,"")), 1, 2)
        
   'Add next columns of data as sub-items of ListItem
        With lvwItem
   'Syntax     .Add Index,Key,Text,Report Icon,TooltipText
         For j = 1 To rst.Fields.Count - 1
           ' .ListSubItems.Add , , CStr(Nz(rst.Fields(j).Value, ""))
           'Change to           
             .ListSubItems.Add , , CStr(Nz(rst.Fields(j).Value, "")),,"Click"
         Next

       End With
       rst.MoveNext
Loop
rst.Close

Since we have only two images available, we will use the first image (Index = 1) as the Icon parameter and the second image (Index = 2) as the SmallIcon parameter.

  • The Icon image is displayed only when the ListView display option is set to 0 - lvwIcon.

  • The SmallIcon image is displayed in all other ListView display options.

In the ListSubItems.Add() method, we have not assigned any image reference. Instead, we used the next parameter to specify a Tooltip text "Click". This text will appear as a tooltip when the mouse pointer hovers over any column from the second column onward.

After making these code changes:

  1. Save the form frmEmployees.

  2. Open the form in Normal View.

  3. You should now see the ListView display, similar to the sample image shown at the top of this page.

The SmallIcon will remain visible in all ListView display modes, except for 'lvwIcon', which uses the larger Icon image.

Check the following sample ListView screenshots of Employee data for reference.

0 - lvwIcon View

ListView Icon View

Right-click to open the Large Image in a New Window.

2 - lvwList View

The first sample image at the top of this page shows the 03 - lvwReport view.
This is the only view that displays all column values in a datasheet-like format.

To explore other views:

  1. Open the form frmEmployees in Design View.

  2. Select the ListView control and open its Property Sheet.

  3. Locate the View property.

  4. Change the setting to try out each option (0 - lvwIcon, 1 - lvwSmallIcon, 2 - lvwList, 3 - lvwReport).

  5. Save the form and open it in Normal View to see how the data is displayed in each case.

This hands-on test helps you understand how the same data looks in different ListView display modes and which one best fits your application.

Download the Demo Database.

 

  1. Microsoft TreeView Control Tutorial
  2. Creating an Access Menu with a TreeView Control
  3. Assigning Images to TreeView Nodes
  4. Assigning Images to TreeView Nodes-2
  5. TreeView Control Checkmark Add Delete
  6. TreeView ImageCombo Drop-down Access
  7. Re-arrange TreeView Nodes By Drag and Drop
  8. ListView Control with MS-Access TreeView
  9. ListView Control Drag Drop Events
  10. TreeView Control With Sub-Forms
Share:

ListView Control Tutorial-02

ListView Control Tutorial.

Continued from last week's ActiveX ListView Control Tutorial-01.

In this session, we will learn how to search for specific rows and column values in the ListView control and display the search results in a Label control on the form. This capability is particularly useful when working with large volumes of data. We will also examine several important ListView property settings.

To begin, we will see how easily columns can be rearranged in the ListView control, similar to the column arrangement available in the Access Datasheet View. To support this demonstration, additional controls—including Textboxes, Comboboxes, Command Buttons, and Labels—have been added to the form, making it easier to select search parameters and display the search results.

For this demonstration, I have made a slight modification to last week's sample data. The values in the first column are now sourced from the Employees table in the Northwind sample database. A query was created to combine the LastName and FirstName fields into a single field, the alias Student, while the EmployeeID field is used as the Key (for example, X01, X02, and so on).

Before moving on to search operations, let us first explore how to rearrange columns in the ListView control using the drag-and-drop feature.

Note: If you have not completed the previous tutorial and would like to continue directly with this session, download the demo database from the ListView Control Tutorial-01 page. Extract the ZIP file, open the database, and you will find the demo form ready in Normal View.

Open the database containing the demo form from the previous session (or the one you created) and switch the form to Normal View.

Now, let us rearrange a column by dragging it from the middle of the list. For example, drag the Weight column and drop it in the position of the Age column. The expected result is that the Age column should shift one position to the right, making room for the Weight column.

Move the mouse pointer over the Weight column header, then press and hold the left mouse button. You will notice that the column header moves slightly downward when the button is pressed. While holding the mouse button, drag the column to the left and drop it onto the Age column header.

At this point, you will notice that nothing happens. This is because the required property setting has not yet been enabled. In fact, enabling a single property is all that is required for this feature to work.

To enable it:

  1. Switch the form to Design View.

  2. Right-click on the ListView control, highlight the ListViewCtrl Object option, and select Properties.

  3. On the Properties window, you will find the option AllowColumnReorder on the right side. Place a check mark to enable it, then click Apply, and click the OK button to close the Properties window.

  4. Now, repeat the drag-and-drop steps explained earlier and observe the result. This simple setting is all that’s required to enable column reordering in the ListView control.

  5. You may be asking: What about rearranging rows?
    Unlike columns, row reordering requires additional programming using event procedures—similar to the drag-and-drop techniques we implemented earlier in the TreeView control. We will cover that part later in this tutorial series.

  6. For now, feel free to experiment by moving any column, even the first column, to any position you like.

Note: Before dropping the source column, ensure that the target column is fully covered by the highlighted frame of the incoming column. If not, the column may shift to the next position on the right instead of replacing the intended target.

Next: Searching for Information in the ListView

Now, let’s move on to learning how to quickly search for information within the ListView—especially useful when working with large volumes of data.

For this purpose, we have added a subroutine to the Tutorial-01 module. This subroutine loads the column header names into a ComboBox on the form, which is displayed with a red background for visibility. The selected column name will be used to search and retrieve specific values (such as Age, Height, Weight, or Class) for a student.

New VBA Code Added to the Form’s Class Module

A new VBA procedure has been added to the Class Module of last week’s Tutorial Form.

This procedure populates the txtColCombo ComboBox with a list of column header labels (field names). These labels correspond to the data fields in the ListView, such as Age, Height, Weight, or Class.

During the search-and-find operation, one of these column values can be selected to retrieve the corresponding detail for a student, along with the student’s name.

Private Sub txtColCombo()
'Column Header List Combo
Dim lvwColHead As MSComctlLib.ColumnHeader
Dim cboName As ComboBox

Set cboName = Me.txtCol
cboName.RowSourceType = "Value List"

For Each lvwColHead In lvwList.ColumnHeaders
    If lvwColHead.Index = 1 Then
        'Nothing
    Else
        cboName.AddItem lvwColHead.Text
    End If
Next
'cboName.DefaultValue = "=txtCol.Column(0, 0)"

Set lvwColHead = Nothing
Set cboName = Nothing
End Sub

The ComboBox will not have a default value for the column header name. If a column is selected, the corresponding value for the student will be displayed in the large Label below the student’s name. If left blank, the search operation will return only the student’s name.

The search operation is flexible and fast, supporting two main methods:

  1. Search by providing text – The search text can come from any column, either as a full match or as a partial string from the left. Since each row in the ListView control has two types of object members—ListItem (first column) and ListSubItems (remaining columns)—the search operation treats these separately.

  2. Search options via an option group – Next to the search-text input TextBox, an option group with two checkboxes allows you to choose where to search:

    • First option (default): Searches the first column (ListItem) for the given text.

    • Second option: Searches within the ListSubItem columns.

Note: Rearranging columns affects only their visual position, not the object type. Dragging a ListSubItem into the first column does not convert it into a ListItem.

To retrieve a value from a specific column, select the column name from the ComboBox located below the search-text TextBox. For example, if you want to find a student’s Height, select Height from the ComboBox.

After setting the search criteria, click the Find Item Command Button. If the search is successful, the result will appear in the large Label control below the button.

The [Find Item] Command Button Click.

Calls the SearchAndFind() Procedure.

Private Sub SearchAndFind()
'Find by Student Name
Dim lstItem As MSComctlLib.ListItem
Dim strFind As String
Dim strColName As String
Dim strColVal As String
Dim j As Integer
Dim intOpt As Integer
Dim msgText As String

Me.Refresh
intOpt = Me.Opts


strFind = Nz(Me![txtFind], "")
strColName = Nz(Me![txtCol], "")

Select Case intOpt
    Case 1
        Set lstItem = lvwList.FindItem(strFind, , , lvwPartial)
    
        If Not lstItem Is Nothing Then
            j = lstItem.Index
            'format the display text
            msgText = lvwList.ColumnHeaders.Item(1).Text
            msgText = msgText & " : " & lstItem.Text & vbCr & vbCrLf
        Else
            MsgBox "Text '" & strFind & "' Not Found!", vbOKOnly + vbCritical, "cmdFind_Click()"
            Exit Sub
        End If
    Case 2
        Set lstItem = lvwList.FindItem(strFind, lvwSubItem, , lvwPartial)
        If Not lstItem Is Nothing Then
       'format the display text
            j = lstItem.Index
            msgText = lvwList.ColumnHeaders.Item(1).Text
            msgText = msgText & ": " & lstItem.Text & vbCr & vbCrLf
        Else
            MsgBox strFind & " Not Found!", vbOK + vbCritical, "cmdFind_Click()"
            Exit Sub
        End If
End Select

        If Len(strColName) = 0 Then 'If column name is not selected
            GoTo nextStep
        Else
            'Get the column value
            strColVal = GetColVal(lstItem, strColName)
            msgText = msgText & String(8 - (Len(strColName)), " ") & _
            strColName & ": " & Nz(strColVal, "")
        End If
nextStep:

If Len(msgText) > 0 Then 'assign to form label
    lvwList.ListItems.Item(j).Selected = True
    lblMsg.caption = msgText
End If

End Sub

At the beginning of the program, the Student Name and the optional Column Name are copied from their respective TextBoxes into the variables strFind and strColName, following validation checks.

Note: The Column Name ComboBox has its Not-in-List property set to Yes. This means you may either select a valid value from the list, type it in, or leave it blank. However, if you type in a value that does not exist in the list, it will not be accepted.

Depending on the selected search option (1 – ListItem or 2 – ListSubItem), the scan process is moved to the appropriate object(s).

Using either method, the program locates the ListItem object (row) that contains the search text. The Index value of the ListItem is then saved in the variable J for later use in the program.

Note: The ListView control automatically assigns index numbers when items are first populated.

Once found, the ListItem.Text value is retrieved. This value is combined with the first column header’s text (for example, Student: Robert King) and stored in the MsgText string, which is then displayed in the Label control on the form.

If a column header name is selected in the ComboBox, the program calls the GetColVal() function, passing the ListItem object and the selected column header text as parameters. This feature is especially useful for retrieving additional details about a student, such as their Height, directly from the record.

The GetColVal() Function VBA Code.

Private Function GetColVal(lvwItem As MSComctlLib.ListItem, ByVal colName As String) As String
Dim i As Integer
Dim strVal As String
    'first column is student name
    'check for column value from 2nd column onwards
    For i = 2 To lvwList.ColumnHeaders.Count
        If lvwList.ColumnHeaders(i).Text = colName Then 'if col name matches
            strVal = lvwItem.ListSubItems.Item(i - 1).Text 'get column value
            Exit For 'No further scanning required
        End If
    Next
GetColVal = strVal 'return the retrieved the value
End Function

The GetColVal() function requires two parameters:

  1. The ListItem object, which contains the student’s name.

  2. The Column Name to be retrieved.

The student’s details—such as Age, Height, Weight, and Class—are stored in the ListItem.ListSubItems collection. The function scans through the lvwList.ColumnHeader values to locate the matching column name.

Once a match is found, the corresponding column index is used to retrieve the value from the ListSubItems object and returned to the calling procedure.

The [Find By Key] Command Button Click Event Procedure.

We have introduced another method to find a Student’s Name (or related information) by using the Unique Key Value assigned to a list item at the time of creating the list.

Although assigning a Key is optional, it is always recommended to add a Unique Key String Value (note: the key must begin with an alphabet character). This approach makes searching more efficient.

For example, when dealing with personal identification records, the following can serve as the Key:

  • Social Security Number

  • National Identity Card Number

  • Passport Number

  • Driving License Number

Using identifiers as the ListItem Key makes it much faster and easier to locate a record compared to the conventional search-by-text method.

The cmdKey_Click() Event Procedure.

Calls FindByKey() Subroutine.
Private Sub FindByKey()
Dim colHeader As MSComctlLib.ColumnHeader
Dim lvItem As MSComctlLib.ListItem
Dim lvKeyVal As String
Dim lvColName As String
Dim txt As String
Dim msgText As String
Dim varcolVal As Variant

lvKeyVal = UCase(Nz(Me!txtKey, ""))
lvColName = Nz(Me!txtCol, "")

If len(lvKeyVal) > 0 then
On Error Resume Next 
Set lvItem = lvwList.ListItems.Item(lvKeyVal) 'get the item by Key
If Err > 0 Then
    Err.Clear
    MsgBox "Key Value: '" & lvKeyVal & "' Not Found!", vbOKOnly + vbCritical, "cmdKey_Click()"
    On Error GoTo 0
    Exit Sub
End If
Else
	MsgBox "Please Provide a Valid Key-Value!",vbOKOnly + vbCritical, "cmdKey_Click()"
    Exit Sub
End If

txt = lvItem.Text 'get the student name
'format message text
msgText = lvwList.ColumnHeaders.Item(1).Text & " : "
msgText = msgText & txt & vbCr & vbCrLf

If Len(lvColName) > 0 Then 'if column name is given
    varcolVal = GetColVal(lvItem, lvColName) 'get column val of student
    msgText = msgText & String(8 - Len(lvColName), " ") & lvColName & ": " & varcolVal ' add it to display
End If

lvItem.Selected = True 'highlight the item on form
Me.lblMsg.caption = msgText 'assign details to form Label
End Sub

As shown in the subroutine, we can directly locate the ListItem containing the Student’s name by using the Key value in a single statement:

Set lvItem = lvwList.ListItems.Item(xKeyVal)

The next line retrieves the ListItem.Text (the Student’s name) into the variable txt. The following two lines then build the message string by inserting the Student’s name into the variable msgText.

Next, an If…Then statement checks whether a Column Name has been entered in the ComboBox control. If a valid column is found, the program calls the GetColVal() function with the appropriate parameters to retrieve the value from that column. The returned value is stored in the variable varColVal and passed back to the calling procedure.

Finally, the Column Name and the retrieved value are appended to the msgText string, which is then displayed in the Label control on the Form.

The next statement highlights the Student’s record row as a visual cue that the searched item has been found. At the same time, the value stored in msgText is displayed in the Label control by setting its Caption property on the Form.

The Full VBA Code on the Form Module.

Option Compare Database
Option Explicit

Dim lvwList As MSComctlLib.ListView 'ListView Control
Dim lvwItem As MSComctlLib.ListItem '
Dim ObjImgList As MSComctlLib.ImageList
Const prfx As String = "K"

Private Sub Form_Load()
    Call LoadListView
    Call txtColCombo
End Sub

Private Function LoadListView()
'Populate the ListView control with Student Details
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim intCounter As Integer
Dim strKey As String

'Assign ListView Control on Form to lvwList Object
 Set lvwList = Me.ListView1.Object
 
With lvwList
    .AllowColumnReorder = True
    .Enabled = True
    .Font = "Verdana"
    .Font.Bold = True
    .Font.Size = 9
    .ForeColor = vbBlack
    .BackColor = vbWhite
 End With
 
 'Create Column Headers for ListView
 With lvwList
    .ColumnHeaders.Clear 'initialize header area
    
   'Syntax: .ColumnHeaders.Add Index, Key, Text, Width, Alignment, Icon
    .ColumnHeaders.Add , , "Student", 2500
    .ColumnHeaders.Add , , "Age", 1200
    .ColumnHeaders.Add , , "Height", 1200
    .ColumnHeaders.Add , , "weight", 1200
    .ColumnHeaders.Add , , "Class", 1200
    
 End With
 
 'Initialize ListView Control
  While lvwList.ListItems.Count > 0
        lvwList.ListItems.Remove (1)
  Wend

'Student Names and Ids are taken from Employees Table
'through the StudentQ Query.
Set db = CurrentDb
Set rst = db.OpenRecordset("StudentQ", dbOpenDynaset)

With lvwList
    Do While Not rst.EOF And Not rst.BOF
        intCounter = rst![EmployeeID]
        strKey = "X" & Format(intCounter, "00") 'Key Value sample: X01
        
    'Syntax: .ListItems.Add(Index, Key, Text, Icon, SmallIcon)
        Set lvwItem = .ListItems.Add(, strKey, rst![Student])
        
        With lvwItem
    'Syntax: .Add Index,Key,Text,Report Icon,TooltipText
            .ListSubItems.Add , strKey & CStr(intCounter), CStr(5 + intCounter)
            .ListSubItems.Add , strKey & CStr(intCounter + 1), CStr(135 + intCounter)
            .ListSubItems.Add , strKey & CStr(intCounter + 2), CStr(40 + intCounter)
            .ListSubItems.Add , strKey & CStr(intCounter + 3), ("Class:" & Format(intCounter, "00"))

       End With
        rst.MoveNext
    Loop
rst.Close
Set rst = Nothing
Set db = Nothing
Set lvwItem = Nothing
End With
lvwList.Refresh

End Function


Private Sub cmdClose_Click()
   DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdFind_Click()
Call SearchAndFind

End Sub

Private Sub cmdKey_Click()
Call FindByKey
End Sub

Private Function GetColVal(lvwItem As MSComctlLib.ListItem, ByVal colName As String) As String
Dim i As Integer
Dim strVal As String
    'first column is student name
    'check for column value from 2nd column onwards
    For i = 2 To lvwList.ColumnHeaders.Count
        If lvwList.ColumnHeaders(i).Text = colName Then 'if col name matches
            strVal = lvwItem.ListSubItems.Item(i - 1).Text 'get column value
            Exit For 'No further scanning required
        End If
    Next
GetColVal = strVal 'return the retrieved the value
End Function



Private Sub txtColCombo()
'Column Header List Combo
Dim lvwColHead As MSComctlLib.ColumnHeader
Dim cboName As ComboBox

Set cboName = Me.txtCol
cboName.RowSourceType = "Value List"

For Each lvwColHead In lvwList.ColumnHeaders
    If lvwColHead.Index = 1 Then
        'Nothing
    Else
        cboName.AddItem lvwColHead.Text
    End If
Next
'cboName.DefaultValue = "=txtCol.Column(0, 0)"

Set lvwColHead = Nothing
Set cboName = Nothing
End Sub


Public Sub SearchAndFind()
'Find by Student Name
Dim lstItem As MSComctlLib.ListItem
Dim strFind As String
Dim strColName As String
Dim strColVal As String
Dim j As Integer
Dim intOpt As Integer
Dim msgText As String

Me.Refresh
intOpt = Me.Opts

strFind = Nz(Me![txtFind], "")
strColName = Nz(Me![txtCol], "")

Select Case intOpt
    Case 1
        Set lstItem = lvwList.FindItem(strFind, , , lvwPartial)
        If Not lstItem Is Nothing Then
            j = lstItem.Index
            'format the display text
            msgText = lvwList.ColumnHeaders.Item(1).Text
            msgText = msgText & " : " & lstItem.Text & vbCr & vbCrLf
        Else
           MsgBox "Text '" & strFind & "' Not Found in the List!", vbOKOnly + vbCritical, "cmdFind_Click()"
        Exit Sub
        End If
    Case 2
        Set lstItem = lvwList.FindItem(strFind, lvwSubItem, , lvwPartial)
        If Not lstItem Is Nothing Then
       'format the display text
            j = lstItem.Index
            msgText = lvwList.ColumnHeaders.Item(1).Text
            msgText = msgText & ": " & lstItem.Text & vbCr & vbCrLf
        Else
            MsgBox strFind & " Not Found!", vbOK + vbCritical, "cmdFind_Click()"
            Exit Sub
        End If
End Select

        If Len(strColName) = 0 Then 'If column name is not selected
            GoTo nextStep
        Else
            'Get the column value
            strColVal = GetColVal(lstItem, strColName)
            msgText = msgText & String(8 - (Len(strColName)), " ") & _
            strColName & ": " & Nz(strColVal, "")
        End If
nextStep:

If Len(msgText) > 0 Then 'assign to form label
    lblMsg.caption = msgText
    lvwList.ListItems.Item(j).Selected = True
End If
End Sub

Public Sub FindByKey()
Dim colHeader As MSComctlLib.ColumnHeader
Dim lvItem As MSComctlLib.ListItem
Dim lvKeyVal As String
Dim lvColName As String
Dim txt As String
Dim msgText As String
Dim varcolVal As Variant


lvKeyVal = UCase(Nz(Me!txtKey, ""))
lvColName = Nz(Me!txtCol, "")

On Error Resume Next
If Len(lvKeyVal) > 0 Then
Set lvItem = lvwList.ListItems.Item(lvKeyVal) 'get the item by Key
    If Err > 0 Then
        Err.Clear
        MsgBox "Key Value: '" & lvKeyVal & "' Not Found!", vbOKOnly + vbCritical, "cmdKey_Click()"
       On Error GoTo 0
        Exit Sub
    End If
Else
    MsgBox "Please Provide a Valid Key-Value!", vbOKOnly + vbCritical, "cmdKey_Click()"
    Exit Sub
End If

txt = lvItem.Text 'get the student name
'format message text
msgText = lvwList.ColumnHeaders.Item(1).Text & " : "
msgText = msgText & txt & vbCr & vbCrLf

If Len(lvColName) > 0 Then 'if column name is given
    varcolVal = GetColVal(lvItem, lvColName) 'get column val of student
    msgText = msgText & String(8 - Len(lvColName), " ") & lvColName & ": " & varcolVal ' add it to display
End If

lvItem.Selected = True 'highlight the item on form
Me.lblMsg.caption = msgText 'assign details to form Label
End Sub

Download the Demo Database from the following Link:



  1. Microsoft TreeView Control Tutorial
  2. Creating an Access Menu with a TreeView Control
  3. Assigning Images to TreeView Nodes
  4. Assigning Images to TreeView Nodes-2
  5. TreeView Control Checkmark Add Delete
  6. TreeView ImageCombo Drop-down Access
  7. Re-arrange TreeView Nodes By Drag and Drop
  8. ListView Control with MS-Access TreeView
  9. ListView Control Drag Drop Events
  10. TreeView Control With Sub-Forms
Share:

Activex ListView Control Tutorial-01

ListView Control Tutorial.

In Microsoft Access, the ListBox control is often used to display a few columns of data, making it easy to locate and select items. Its data source may be assigned to the Row Source property as a value list, or dynamically from a table or query.

The ComboBox control, by contrast, keeps its list hidden until the user clicks to expand it and make a selection. Both of these are standard Access controls on forms.

There is also another familiar list-style control that we encounter frequently in Access: the Datasheet View. Whether records are displayed from a table or query, the datasheet presents them as a large, scrollable list in tabular format.

In addition to these built-in controls, Microsoft Access also allows us to use ActiveX controls. A common example is the Microsoft Common Dialog Control (often used for file browsing).

The focus of this topic is the Windows ListView control. You can think of it as very similar to Windows Explorer: it can display items as large icons, small icons, a simple list, or in a detailed view with multiple columns. Data from an Access table or query can be loaded into the ListView, giving you the ability to:

  • Rearrange columns or rows,

  • Sort items interactively,

  • Display images next to items,

  • Present records in a more flexible, customizable layout.

This control is widely used in other programming environments such as VB6, VB.NET, and C#. In this article, we will explore how it is integrated into a Microsoft Access database.

Below is a simple ListView demo screen displaying sample data:

We will use the image-like display shown earlier as the starting point for this ListView Control tutorial. With just a few lines of VBA code, we have uploaded ten rows of data into the ListView control.

By default, the ListView ActiveX control may not appear in the list of available ActiveX controls in Access. To make it available, we must add the library file 'MSCOMCTL.OCX' from the C:\Windows\System32 folder to the Access references. Once registered, you will see it listed as Microsoft ListView Control, Version 6.0, along with other ActiveX controls.

This library file provides several useful controls, including ListView, TreeView, and ImageList. If you have already followed our earlier TreeView control tutorials, you are familiar with this library.

Adding the Windows Common Controls Library (MSCOMCTL.OCX)

Follow these steps to attach the MSCOMCTL.OCX file to your database:

  1. Open your database and press Alt + F11 to launch the VBA editor.

  2. From the Tools menu, select References…

  3. Click the Browse button.

  4. Locate the file MSCOMCTL.OCX (Microsoft Windows Common Controls) in one of the following folders:

    • C:\Windows\System32\ → on 32-bit systems, or on most Windows 11 installations.

    • C:\Windows\SysWOW64\ → on 64-bit systems.

  5. Select the file and click Open to attach it to your database.

  6. Press Alt + F11 again to return to the database window.

Designing a Sample Form with the ListView Control

We will now design a simple form that matches the sample image shown at the beginning of this tutorial.

  1. Create a new blank form in Design View.

  2. From the Controls group, select ActiveX Control.

  3. In the list of available ActiveX controls, locate and select Microsoft ListView Control, Version 6.0, then click OK to insert it onto the form’s Detail section.

  4. Resize the control:

    • Grab the bottom-right resize handle and drag it outward to make the ListView large enough to resemble the sample image.

    • Move the control slightly down and to the right to leave space for a heading label above and some margin on the left.

  5. With the ListView still selected, open the Property Sheet and rename the control by setting its Name property to: ListView1

  6. Create a Label control above the ListView.

    • Change its Caption property to: ListView Control Tutorial

    • Apply any formatting you prefer (font size, bold, color, etc.) to make the heading stand out.

  7. Insert a Command Button below the ListView.

    • Set its Name property to: cmdClose

    • Set its Caption property to: Close

When completed, your form design should look similar to the following layout:

  1. Now, save the Form named: ListViewTutorial and keep the Form in the design view.

  2. Press Alt+F11 to go back to the Form’s Class Module Window.

    The VBA Code.

  3. Copy and paste the following Code into the Form's VBA Module, replacing existing lines of code, if any:

    Option Compare Database
    Option Explicit
    
    Dim lvwList As MSComctlLib.ListView
    Dim lvwItem As MSComctlLib.ListItem
    Dim ObjImgList As MSComctlLib.ImageList
    Const prfx As String = "X"
    
    Private Sub cmdClose_Click()
       DoCmd.Close acForm, Me.Name
    End Sub
    
    Private Sub Form_Load()
        Call LoadListView
    End Sub
    
    Private Function LoadListView()
        Dim intCounter As Integer
        Dim strKey As String
    
    'Assign ListView Control on Form to lvwList Object
     Set lvwList = Me.ListView1.Object
     
     'Create Column Headers for ListView
     With lvwList
        .ColumnHeaders.Clear 'initialize header area
       'Parameter List:
    'Syntax: .ColumnHeaders.Add Index, Key, Text, Width, Alignment, Icon
        .ColumnHeaders.Add , , "Name", 2500
        .ColumnHeaders.Add , , "Age", 1200
        .ColumnHeaders.Add , , "Height", 1200
        .ColumnHeaders.Add , , "weight", 1200
        .ColumnHeaders.Add , , "Class", 1200
     End With
     
     'Initialize ListView Control
      While lvwList.ListItems.Count > 0
            lvwList.ListItems.Remove (1)
      Wend
        
     With lvwList
        For intCounter = 1 To 10
            strKey = prfx & CStr(intCounter) '
       'Syntax: .ListItems.Add(Index, Key, Text, Icon, SmallIcon)
            Set lvwItem = .ListItems.Add(, strKey, "Student " & intCounter)
            'Add next columns of data as sub-items of ListItem
            With lvwItem
          'Parameters =      .Add Index,Key,Text,Report Icon,TooltipText
                .ListSubItems.Add , strKey & CStr(intCounter), CStr(5 + intCounter)
                .ListSubItems.Add , strKey & CStr(intCounter + 1), CStr(135 + intCounter)
                .ListSubItems.Add , strKey & CStr(intCounter + 2), CStr(40 + intCounter)
                .ListSubItems.Add , strKey & CStr(intCounter + 3), ("Class:" & intCounter)
    
           End With
        Next
        'reset lvwItem object
        Set lvwItem = Nothing
    End With
    lvwList.Refresh
    
    End Function
  4. Save the Form with the name ListView Control Tutorial-01.

    Demo View of the Form.

  5. Open the Form in Normal View to have a look at our creation.

    If you find your form as shown below, then you are on the right track.

    We need to make some adjustments to the ListView control’s property settings. Earlier, we renamed the control to ListView1 using the standard Access Property Sheet. However, the ListView control has its own dedicated property sheet, which provides more detailed configuration options. Some of these settings also appear in the Access Property Sheet, but many are unique to the control itself.

  6. To access it, right-click the ListView control, point to ListViewCtrl Object, and then select Properties from the shortcut menu.

  7. This will open the ListView control’s own property sheet, as shown in the image below:

  8. At the top of the Property Sheet, you will see tabs that group various options. By default, the General tab is active. On this tab, the left side lists option values, while the right side contains corresponding checkboxes.

    For our form, we only need to adjust two ListView properties, which are disabled by default. Once enabled, these allow the ListView to display in different modes—such as large icons, small icons, simple lists, or Report View (as shown in the first image above).

    1. Check the Enabled property on the right side to activate the ListView control.

    2. From the View drop-down list on the left side, select lvwReport.

    3. Click the Apply button to confirm the change.

    4. Click OK to close the Property Sheet.

    Finally, save the form and open it in Normal View. The result should now look like the image shown earlier, except for any differences in form background color or other form-level settings.

  9. The Program's Functional Diagram.

    Before diving into the VBA code, it’s important to understand how data items are actually loaded into the ListView control. With a ListBox, the data arrangement is fairly straightforward. However, the ListView control uses a completely different approach. The loading process does not follow the logical sequence we might naturally expect.

    Once you see how the data flows from the source into a single row—illustrated as a diagram or flow chart—the concept becomes much easier to grasp. With this visual in mind, understanding the VBA code and its role in the process will be far more intuitive.

    The Data Flow Diagram.

    1. In the diagram, the box at the top-left corner represents the ListView control.

      The first step in preparing the list is to create the column headings. These headings (shown in red in the diagram) work the same way as field headers in a table’s Datasheet View. Each column heading is added to the ColumnHeaders collection of the ListView control using the ColumnHeaders.Add() method. Since our sample has five columns, the method is called five times, once for each heading.

      The next set of actions loads the actual data. Each row of data represents a single record with five fields. However, these fields are not loaded all at once—they are split between two different object members of the ListView control: ListItems and ListSubItems.

      • The first field value (the value for the first column) is added to the ListItems collection using the ListItems.Add method. For example, in the sample image, the value Student1 (from the first column of the first row) is stored in the ListItems object.

      • From the second column onward, the remaining field values are added as ListSubItems of the corresponding ListItem. This is done using the ListSubItems.Add method, called four times—once each for the Age, Height, Weight, and Class values.

      Together, these two steps complete a single row of data in the ListView control. The diagram illustrates this process with two rows of sample data.

      Once you understand this two-level structure—ListItems for the first column, ListSubItems for the remaining fields—the VBA code that builds the ListView will be much easier to follow.

    Let us go to the VBA Code Segment-wise.

    In the global declaration section of the module, we have declared the ListView object, the ListItem object, the ImageList object, and a constant variable with the string value "LV".

    Dim lvwList As MSComctlLib.ListView
    Dim lvwItem As MSComctlLib.ListItem
    Dim ObjImgList As MSComctlLib.ImageList
    Const prfx As String = "X"


    The variable lvwList is declared as a ListView object, lvwItem as a ListItem object of the ListView control, and ObjImgList as an ImageList object. The ImageList object is another ActiveX control that can store image icons for use with both the TreeView and ListView controls. For now, we will set the ImageList aside and return to it later.

    The constant Prfx is used as the Key value prefix in the ListItems.Add method, which accepts several optional parameters. The Key value must always be a string type.

    The LoadListView() function serves as the main program.

    On our form, the ListView control is named ListView1. The first executable statement in the program is:

    Set lvwList = Me.ListView1.Object 

      Assigns the ListView1 control on the Form into the Object variable lvwList declared in the Global declarations area.

      Next, we will get prepared to load the Column Header information.  First, we initialize the ColumnHeader object to ensure that it is empty.  When we repeatedly run the program, the control has a tendency to retain the earlier loaded values in the control.  When you open and close this form more than once, after disabling the 'ColumnHeaders.Clear' statement, you will know the difference. The same set of headings is added to the control every time and appears with empty rows below.

    You can verify this behavior manually with the following steps:

    1. Open the demo form once and then close it.

    2. Reopen the form in Design View.

    3. Right-click the ListView control, highlight the ListViewCtrl Object option, and select Properties from the menu.

    4. In the property sheet, go to the Column Headers tab.

    5. You will see the first column heading displayed in a text box, with its Index value (1) shown above.

    6. Move the mouse pointer to the right side of the index number box. Arrow buttons (left and right) will appear.

    7. Click the right arrow to scroll through and display the remaining column labels, one by one, as their index numbers change.

    8. If you open and close the form again, you will notice that the Column Headers tab now contains duplicate sets of the same column labels.

    The ColumnHeaders.Add method syntax is as follows:
    lvwList.ColumnHeaders.Add(Index, Key, Text, Width, Alignment, Icon)

    All parameters are optional.

    With lvwList
        .ColumnHeaders.Clear 'initialize header area
    'Parameter List:
    'Syntax: .ColumnHeaders.Add Index, Key, Text, Width, Alignment, Icon
        .ColumnHeaders.Add , , "Name", 2500
        .ColumnHeaders.Add , , "Age", 1200
        .ColumnHeaders.Add , , "Height", 1200
        .ColumnHeaders.Add , , "weight", 1200
        .ColumnHeaders.Add , , "Class", 1200
     End With 

    The Index value is automatically assigned as running serial numbers (1, 2, 3, and so on).

    The Key value is of the String data type. Although it is not typically used for column headers, it can be assigned if needed.

    The Text value is what appears on the control as the column label.

    To control the display width of each column, you can assign an approximate width value in pixels, based on the data expected under that column.

    If the Text alignment property is omitted, the default is Left alignment (0 - lvwAlignmentLeft). Alternatively, you can set it to Right alignment (1 - lvwAlignmentRight) or Center alignment (2 - lvwAlignmentCenter).

    Once the column headings are loaded, the next step is to insert the first record. Specifically, we start by loading the value in the first column of the first row. But before doing so, we must initialize the ListItems object with the following code segment:

    'Initialize ListView Control
      While lvwList.ListItems.Count > 0
            lvwList.ListItems.Remove (1)
      Wend

    The next code block loads the record list items one row at a time, generating a total of ten rows with sample values. For demonstration purposes, these values remain mostly constant, with a few variations to highlight the process. This is accomplished by placing the logic inside a For...Next loop, which iterates ten times, thereby creating ten rows of data in the ListView control.

    With lvwList
        For intCounter = 1 To 10
            strKey = prfx & CStr(intCounter) '
      'Syntax: .ListItems.Add(Index, Key, Text, Icon, SmallIcon)
            Set lvwItem = .ListItems.Add(, strKey, "Student " & intCounter)
            
      'Add next columns of data as sub-items of ListItem
            With lvwItem
      ' Syntax: .ListSubItems.Add Index,Key,Text,Report Icon,TooltipText
                .ListSubItems.Add , strKey & CStr(intCounter), CStr(5 + intCounter)
                .ListSubItems.Add , strKey & CStr(intCounter + 1), CStr(135 + intCounter)
                .ListSubItems.Add , strKey & CStr(intCounter + 2), CStr(40 + intCounter)
                .ListSubItems.Add , strKey & CStr(intCounter + 3), ("Class:" & intCounter)
    
           End With
        Next
        'reset lvwItem object
        Set lvwItem = Nothing
    End With

    The first statement inside the For...Next loop —

    strKey = prfx & CStr(intCounter)

    — prepares a unique Key value for the first list item (the first column).

    All parameters of the ListItems.Add() Methods are optional. However, in this case, the first three—Index, Key, and Text—are used in the same sequence as the Column Headers. The remaining two parameters are reserved for assigning an icon and a small icon image, if needed.

    When the value for the first column of a row is assigned to the ListItem (i.e., lvwList.ListItems), that object reference is stored in the lvwItem variable. This allows easy access to its sub-object, ListSubItems, without repeatedly writing the full object reference.

    lvwList.ListItems.Item(index).ListSubItems.Add() 

    Expressed in the short form with lvwItem.ListSubItems.Add()

    Using the short form 'lvwItem.ListSubItems.Add()' We can load the remaining column values into the ListView control.

    The ListSubItems.Add() method accepts its first three parameters in the same order as the ListItem (Index, Key, and Text), followed by the optional Icon image reference and Tooltip Text.

    For the Key value of each column, I have appended the current loop counter value plus an offset to ensure uniqueness across all columns. Although the Key parameter can be omitted, it is good practice to use it.

    The Method  ListSubItems.Add()  is called four times within the loop to insert values for the second through fifth columns.

    These steps repeat nine more times, ultimately creating ten sample records in the ListView control.

    The demo database containing this ListView control example is attached, ready to run and explore.

    In the next part of this tutorial, we will explore how to search and locate specific values within the ListView control, as well as how to rearrange columns—just like we do in Datasheet View.

    1. Microsoft TreeView Control Tutorial
    2. Creating an Access Menu with a TreeView Control
    3. Assigning Images to TreeView Nodes
    4. Assigning Images to TreeView Nodes-2
    5. TreeView Control Checkmark Add Delete
    6. TreeView ImageCombo Drop-down Access
    7. Re-arrange TreeView Nodes By Drag and Drop
    8. ListView Control with MS-Access TreeView
    9. ListView Control Drag Drop Events
    10. TreeView Control With Sub-Forms
Share:

PRESENTATION: ACCESS USER GROUPS (EUROPE)

Translate

PageRank

Post Feed


Search

Popular Posts

Blog Archive

Powered by Blogger.

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet External Links Queries Array Class Module msaccess reports Accesstips msaccess tips WithEvents Downloads Objects Menus and Toolbars MsaccessLinks Process Controls Art Work Collection Object Property msaccess How Tos Combo Boxes ListView Control Query VBA msaccessQuery Calculation Dictionary Object Event Graph Charts ImageList Control List Boxes TreeView Control Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes Recordset Top Values Variables msaccess email progressmeter Access2007 Copy Excel Expression Fields Join Methods Microsoft Numbering System RaiseEvent Records Security Split SubForm Table Tables Time Difference Utility WScript Workgroup Wrapper Classes database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Desktop Shortcuts Diagram Disk Dynamic Lookup Error Handler Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Message Box Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code