Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Listbox: Does an Item Exist

From Wiki

Jump to: navigation, search

Here is a sample function to check if an item exists in any listbox control.

  Function CheckForItem(strItem, ListB As ListBox) As Boolean
  Dim rs As DAO.Recordset
  Dim db As Database
  Dim tdf As TableDef
  
      Set db = CurrentDb
  
      CheckForItem = False
  
      Select Case ListB.RowSourceType
          Case "Value List"
              CheckForItem = InStr(ListB.RowSource, strItem) > 0
  
          Case "Table/Query"
              Set rs = db.OpenRecordset(ListB.RowSource)
  
              For i = 0 To rs.Fields.Count - 1
                  strList = strList & " & "","" & " & rs.Fields(i).Name
              Next
  
              rs.FindFirst "Instr(" & Mid(strList, 10) & ",'" & strItem & "')>0"
  
              If Not rs.EOF Then CheckForItem = True
  
          Case "Field List"
  
              Set tdf = db.TableDefs(ListB.RowSource)
  
              For Each itm In tdf.Fields
                  If itm.Name = strItem Then CheckForItem = True
              Next
  
      End Select
  
  End Function

598 Rating: 1.0/5 (2 votes cast)