Full Version: How To Add An Image In A Listview In An Access Form
UtterAccess Forums > Microsoft® Access > Access Forms
Good day folks!
would like your help if you may; i simply want to add an image to a listview in my access form. The listview gets its data from one of my tables.
This is the code that loads the data, the other part of it is in the form's onload event.
Option Explicit
Function FillList(Domain As String, LV As Object) As Boolean
      '  Purpose:   to fill a ListView control with data from a table or
      '             query
      '  Arguments: a Domain which is the name of the table or query, and
      '             a ListView control object
      '  Returns:   A Boolean value to indicate if the function was
      '             successful
      Dim db As Database, rs As Recordset
      Dim intTotCount As Integer
      Dim intCount1 As Integer, intCount2 As Integer
      Dim colNew As ColumnHeader, NewLine As ListItem
      On Error GoTo Err_Man
      ' Clear the ListView control.
      ' Set Variables.
      Set db = CurrentDb
      Set rs = db.OpenRecordset(Domain)
      ' Set Column Headers.
      For intCount1 = 0 To rs.Fields.Count - 1
         Set colNew = LV.ColumnHeaders.Add(, , rs(intCount1).Name)
      Next intCount1
      LV.View = 3    ' Set View property to 'Details'.
      LV.GridLines = True
      LV.FullRowSelect = True
      ' Set Total Records Counter.
      intTotCount = rs.RecordCount
      ' Loop through recordset and add Items to the control.
      For intCount1 = 1 To intTotCount
         If IsNumeric(rs(0).Value) Then
            Set NewLine = LV.ListItems.Add(, , Str(rs(0).Value))
            Set NewLine = LV.ListItems.Add(, , Str(rs(0).Value))
         End If
         For intCount2 = 1 To rs.Fields.Count - 1
            NewLine.SubItems(intCount2) = rs(intCount2).Value
         Next intCount2
      Next intCount1
      Exit Function
         ' Ignore Error 94 which indicates you passed a NULL value.
         If Err = 94 Then
            Resume Next
         ' Otherwise display the error message.
            MsgBox "Error: " & Err.Number & Chr(13) & _
               Chr(10) & Err.Description
         End If
End Function
Doug Steele
You need to add an ImageList control to your form, loads the pictures into that image control and associate the images in the Image control with the ListView control.
andy Birch has an example at ListView Demo 4 - Adding the Associated Icons (Note: Randy's site is aimed at VB programmers. Since there are some major differences between forms in Access and in VB, not all of his examples can be ported directly into Access. I think this particular one, though, should be okay)
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.