home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / access / diverses / sbfrmf / sbfrmf.txt < prev    next >
Text File  |  1995-02-27  |  2KB  |  46 lines

  1. Function SearchSubform ()
  2.     Dim strCriteria As String, strField  As String, strLinkChild As String
  3.     Dim strLinkMaster As String, rsForm  As Recordset, rsSubForm As Recordset
  4.     Dim strSearchValue As String, i As Integer, db As Database, varLinkValue As Variant
  5.  
  6.     'Identify the name of the control being searched
  7.     'and the names of the fields involved in the linking relationship
  8.  
  9.     strField = Screen.activecontrol.Name
  10.     strLinkChild = CStr(Me![Contacts].LinkChildFields)
  11.     strLinkMaster = CStr(Me![Contacts].LinkMasterFields)
  12.     strSearchValue = InputBox$("Please enter value to search for:", "Searching in " & strField)
  13.  
  14.     'Build the search criteria and search
  15.     'Note that you search the underlying table or query of the subform.
  16.     'You don't search the recordset clone of the subform, as this only holds
  17.     'records matching the current main form record.
  18.  
  19.     Set db = dbEngine(0)(0)
  20.     Set rsSubForm = db.OpenRecordset("Contacts", DB_OPEN_DYNASET)
  21.     strCriteria = strField & " like " & Chr$(39) & strSearchValue & Chr$(42) & Chr$(39)
  22.     rsSubForm.FindFirst strCriteria
  23.     
  24.     'On finding the subform record, note the value of the subform link field.
  25.     'This value will have a match on the main form.
  26.  
  27.     If Not rsSubForm.nomatch Then
  28.         For i = 0 To rsSubForm.Fields.count - 1
  29.             If rsSubForm.Fields(i).Name = strLinkChild Then
  30.                 varLinkValue = rsSubForm.Fields(i).value
  31.                 Exit For
  32.             End If
  33.         Next
  34.  
  35.         'On the main form, search for the record with the matching link field value.
  36.         'Use the bookmark property to display that record.
  37.  
  38.         Set rsForm = Me.recordsetclone
  39.         strCriteria = strLinkMaster & " like " & Chr$(39) & varLinkValue & Chr$(42) & Chr$(39)
  40.         rsForm.FindFirst strCriteria
  41.         If Not rsForm.nomatch Then
  42.             Me.bookmark = rsForm.bookmark
  43.         End If
  44.     End If
  45. End Function
  46.