Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Private Function fLoadList()
On Error GoTo err_handle
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Variables
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim lvxObj As ListView
Dim lstItem As ListItem
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim iColWidth As Integer
Dim i As Integer
Dim strSQL As String
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Set the SQL statement for our recordsource
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'By using the SQL 'AS' keyword, we can give our columns custom names...
strSQL = "SELECT " & _
"EmployeeID as [ID], " & _
"EmployeeName as [Name], " & _
"NTLogin as [NT Login], " & _
"EmployeeTitle as [Title], " & _
"Active as [Active] " & _
"FROM tblEmployees " & _
"ORDER BY EmployeeID ASC;"
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Set up List View object, and invoke a recordset based on the SQL
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set lvxObj = lvxEmployees.Object
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Clear any items in the current list.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lvxObj.ListItems.Clear
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Clear existing, then add new column headers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'code loops through the open recordset's field names (the custom
'ones with set using the AS keyword, if you remember) and sets them
'as our ListViews columnheaders.
'We also set the column widths to be all the same, which is calculated
'by taking the width of the ListView itself, divided by the number of
'columns. I minus 20 of the end of each one, to ensure all fields are
'visible completely (not hanging over the edge of the listview).
With lvxObj.ColumnHeaders
.Clear
For i = 0 To rs.Fields.Count
For Each fld In rs.Fields
If i = 0 Then
iColWidth = 0 ' This hides the first ID column from the user, but retains it's value as the identifying property
Else
iColWidth = (lvxEmployees.Width / (rs.Fields.Count - 1)) - 20
End If
.Add , , fld.Name, iColWidth
i = i + 1
Next fld
Next i
End With
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Check values present in recordset
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If rs.BOF Then
'No data has been returned .. no need to add the items to the
' list view.
Else
'Records present.. setting up list of items
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Add in list items, with colour based on criteria of if the
' employee is active or not.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rs.MoveFirst
While Not rs.EOF
For i = 0 To rs.Fields.Count
If i = 0 Then
Set lstItem = lvxObj.ListItems.Add(, , Nz(Trim(rs(i)), "")) ' Set the value of the first column of the row
'Set the Colour based on criteria
If rs("Active") = 0 Then
lstItem.ForeColor = vbBlack ' Black if not active
Else
lstItem.ForeColor = vbRed ' Red if active
End If
ElseIf i < rs.Fields.Count Then
lstItem.SubItems(i) = Nz(Trim(rs(i)), "") ' set the subsequent columns, known as subitems.
'Repeat Colour setting based on criteria, for the subitems
If rs("Active") = 0 Then
lstItem.ForeColor = vbBlack ' Black if not active
Else
lstItem.ForeColor = vbRed ' Red if active
End If
Else
End If
Next i
rs.MoveNext
Wend
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Close off & Cleanup
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rs.Close
err_handle:
Select Case err.Number
Case 0
'ignore, not an Error
Case Else
'Handle error Appropriately.
End Select
End Function
Private Sub lvxEmployees_DblClick()
On Error GoTo err_handle
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Variables
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim lvxObj As ListView
Dim lstItem As ListItem
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Set objects
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set lvxObj = lvxEmployees.Object
Set lstItem = lvxObj.SelectedItem
'lstItem now contains the content of EmployeeID
msgbox Dlookup("EmployeeName", "tblEmployees", "EmployeeID = " & lstitem)
err_handle:
Select Case err.Number
Case 0
'ignore, not an Error
Case Else
'Handle error Appropriately.
End Select
End Sub
Private Function fListColumnSort(ColumnHeader)
On Error GoTo err_handle
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Variables etc
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim i As Integer
Dim lvxObj As ListView
Set lvxObj = lvxEmployees.Object
i = ColumnHeader.Index - 1
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Turn on sorting for that column header
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lvxObj.Sorted = True
lvxObj.SortKey = i
If lvxObj.SortOrder = lvxAscending Then
lvxObj.SortOrder = lvxDescending
ElseIf lvxObj.SortOrder = lvxDescending Then
lvxObj.SortOrder = lvxAscending
End If
lvxObj.Refresh
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Error Handling
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
err_handle:
Select Case err.Number
Case 0
'ignore, not an Error
Case Else
'Handle error Appropriately.
End Select
End Function