Public Sub findInList(lst As Access.ListBox, fldName As String, varSearch As Variant)
On Error GoTo errlable
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim varItm As Variant
Set rs = lst.Recordset
Select Case rs.Fields(fldName).Type
'Numeric
Case 16, 9, 1, 2, 5, 20, 7, 21, 3, 4, 19, 6
If IsNumeric(varSearch) Then
rs.FindFirst fldName & " = " & varSearch
End If
'Date
Case 8, 22
If IsDate(varSearch) Then
rs.FindFirst fldName & " = " & SQLDate(varSearch)
End If
'Text
Case 18, 10
If Not IsNumeric(varSearch) And Not IsDate(varSearch) Then
rs.FindFirst fldName & " = " & SQLStr(varSearch)
End If
Case Else
MsgBox "No a valid field type"
End Select
If rs.NoMatch Then
MsgBox "Search not found"
lst = Null
Else
lst.Selected(rs.AbsolutePosition) = True
End If
Exit Sub
errlable:
If Err.Number = 3265 Then
MsgBox "Field not in list box row source"
Else
MsgBox Err.Number & " " & Err.Description
End If
End Sub
Function SQLDate(varDate As Variant) As String
'Purpose: Return a delimited string in the date format used natively by JET SQL.
'Argument: A date/time value.
'Note: Returns just the date format if the argument has no time component,
' or a date/time format if it does.
'Author: Allen Browne. allen@allenbrowne.com, June 2006.
If IsDate(varDate) Then
If DateValue(varDate) = varDate Then
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
Else
SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
End If
End If
End Function
Public Function SQLStr(varStr As Variant) As String
SQLStr = "'" & varStr & "'"
End Function