The majority of the ListView enhancements are provided in the CListViewEnh class. This module contains a standalone procedure to load a DAO recordset into a ListView
control.
Note: This code is not supported in the 64-bit version of Access 2010 or 2013 due to the use of the Common Controls of MSComCtl32.ocx.
Procedure Name | Type | Description |
(Declarations) | Declarations | Declarations and private variables for the modListView module. |
ADORecordSetToListView | Procedure | Loads the contents of an ADO recordset in a standard unbound listview control from left to right until it runs out of available columns in the ListView control. If you want to see the columns in a different order, select them in the desired order from the query that creates the recordset. |
RecordSetToListView | Procedure | Loads the contents of a DAO recordset in a standard unbound listview control from left to right until it runs out of available columns in the ListView control. If you want to see the columns in a different order, select them in the desired order from the query that creates the recordset. |
' Example of modListView ' This example assumes Access VBA as the host language ' 1. Create a new form ' 2. Add a ListView control called 'lvwProducts' ' Set the following properties ' View 3 - lvwReport ' ColumnHeaders - Insert 4 column headers ' 3. Add a CommandButton control called 'cmdDAO' ' 4. Add a CommandButton control called 'cmdADO' ' 5. Paste all the code from this example to the new form's module. ' 6. Run the form to load the listview control ' This example assumes that the sample files are located in the folder named by the following constant. Private Const mcstrSamplePath As String = "C:\TVSBSamp" Private Const mcstrConnect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mcstrSamplePath & "\sample.mdb" Private Sub Form_Load() cmdADO.Caption = "Load with ADO" cmdDAO.Caption = "Load with DAO" End Sub Private Sub cmdDAO_Click() Dim dbs As DAO.Database Dim rst As DAO.Recordset Dim strSQL As String strSQL = "SELECT Products.ProductID, Products.ProductName, Categories.CategoryName, Suppliers.CompanyName, Suppliers.ContactName " & _ "FROM Suppliers " & _ "INNER JOIN (Categories " & _ "INNER JOIN Products " & _ "ON Categories.CategoryID = Products.CategoryID) " & _ "ON Suppliers.SupplierID = Products.SupplierID" Set dbs = DAO.DBEngine(0).OpenDatabase(mcstrSamplePath & "\sample.mdb") Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot) Screen.MousePointer = vbHourglass ' Example of RecordSetToListView ' Load the recordset into the listview, using the value in the ProductID column as they listitem key, ' and the value in the Subject column as the 'tag' property of the listitem RecordSetToListView rst, lvwProducts.Object, "ProductID", "ProductName" Screen.MousePointer = vbDefault End Sub Private Sub cmdADO_Click() Dim rstProducts As ADODB.Recordset Dim strSQL As String strSQL = "SELECT Products.ProductID, Products.ProductName, Categories.CategoryName, Suppliers.CompanyName " & _ "FROM Suppliers " & _ "INNER JOIN (Categories INNER JOIN Products " & _ "ON Categories.CategoryID = Products.CategoryID) " & _ "ON Suppliers.SupplierID = Products.SupplierID " & _ "ORDER BY Products.ProductName " Set rstProducts = New ADODB.Recordset rstProducts.Open strSQL, mcstrConnect ' Example of ADORecordSetToListView ' Load the recordset into the listview, using the value in the ProductID column as they listitem key. ADORecordSetToListView rstProducts, lvwProducts.Object, "ProductID" End Sub
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."
Van T. Dinh, Microsoft MVP