home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / access / diverses / sbfrmf / sbfrmfnd.txt < prev   
Text File  |  1995-02-26  |  6KB  |  113 lines

  1. Function SearchSubFormEnhanced ()
  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.     Static strOldCriteria As String, OrgID, ContID, Searchfield As Control, strOldSearchValue As String
  6.     Dim Criteria As String
  7.  
  8.     '**** Purpose of code is to display main form records corresponding to
  9.     '**** values searched for on the subform. This overcomes the normal Access
  10.     '**** limitation of the Edit, Find facility.
  11.     '**** To use, change the form name 'Organisations Form', subform name "Contacts" and fields
  12.     '**** OrganisationID, ContactID and table Contacts to suit your application, then call from a menu.
  13.     '**** If you call from a button, use PreviousControl instead of ActiveControl.
  14.     '**** To find the next or previous match enter "/" or "\" as the search value.
  15.     '**** Cursor should be placed in field to be searched.
  16.     
  17.     '**************************************************************************************************
  18.     'Author: Charles Kangai
  19.     '        Blueneck Computer Training & Consulting Services
  20.     '        9 Chandos Road
  21.     '        Redland
  22.     '        Bristol, UK
  23.     '        Compuserve: 100424,1710 Internet: ckangai@blueneck.demon.co.uk
  24.     '        Tel: 0117-974 4416 Fax: 0117-923 8172 Int'l: Tel/Fax: 44-117-974 4416/44-117-923 8172
  25.     '**************************************************************************************************
  26.  
  27.     On Error GoTo ErrorSearchSubForm
  28.     'Identify the name of the control being searched
  29.     'and the names of the fields involved in the linking relationship
  30.  
  31.     strField = Screen.activecontrol.Name  'Use PreviousControl if you call function from a button
  32.     strLinkChild = CStr(Me![Contacts].LinkChildFields)
  33.     strLinkMaster = CStr(Me![Contacts].LinkMasterFields)
  34.     strSearchValue = InputBox$("Please enter value to search for:", "Searching in " & strField)
  35.     If strSearchValue = "" Then Exit Function
  36.  
  37.     'Build the search criteria and search
  38.     'Note that you search the underlying table or query of the subform.
  39.     'You don't search the recordset clone of the subform, as this only holds
  40.     'records matching the current main form record.
  41.  
  42.     Set db = dbEngine(0)(0)
  43.     Set rsSubForm = db.OpenRecordset("Contacts", DB_OPEN_DYNASET)
  44.     strCriteria = strField & " like " & Chr$(39) & strSearchValue & Chr$(42) & Chr$(39)
  45.     Select Case strSearchValue
  46.         Case "/"
  47.             'Find next
  48.             If IsEmpty(OrgID) Or IsEmpty(ContID) Then Exit Function  'Use selected to Find Next before the initial Find
  49.             strCriteria = strOldCriteria
  50.             'If your OrgID and ContactID fields are strings, you must have: strOldCriteria = "[OrganisationID] = " & """" & OrgID & """" & " AND [ContactID] = " & """" & ContID & """"
  51.             strOldCriteria = "[OrganisationID] = " & OrgID & " AND [ContactID] = " & ContID
  52.             'First, locate where you got to last time.
  53.             rsSubForm.FindFirst strOldCriteria
  54.             'Then find the next record
  55.             rsSubForm.FindNext strCriteria
  56.         Case "\"
  57.             'Find previous
  58.             If IsEmpty(OrgID) Or IsEmpty(ContID) Then Exit Function  'Use selected to Find Next before the initial Find
  59.             strCriteria = strOldCriteria
  60.             strOldCriteria = "[OrganisationID] = " & OrgID & " AND [ContactID] = " & ContID
  61.             'First locate where you got to last time
  62.             rsSubForm.FindFirst strOldCriteria
  63.             'Then find the previous record
  64.             rsSubForm.FindPrevious strCriteria
  65.         Case Else
  66.             'This is a new search.  Save the criteria before searching.
  67.             strOldSearchValue = strSearchValue
  68.             strOldCriteria = strCriteria
  69.             Set Searchfield = Screen.activecontrol
  70.             rsSubForm.FindFirst strCriteria
  71.     End Select
  72.     
  73.     'On finding the subform record, note the value of the subform link field.
  74.     'This value will have a match on the main form.
  75.  
  76.     If Not rsSubForm.nomatch Then
  77.         'Save the main record primary key and subform record primary key
  78.         'This enables you to search for the next record next time round.
  79.         OrgID = rsSubForm!OrganisationID
  80.         ContID = rsSubForm!ContactID
  81.         For i = 0 To rsSubForm.Fields.count - 1
  82.             If rsSubForm.Fields(i).Name = strLinkChild Then
  83.                 varLinkValue = rsSubForm.Fields(i).value
  84.                 Exit For
  85.             End If
  86.         Next
  87.  
  88.         'On the main form, search for the record with the matching link field value.
  89.         'Use the bookmark property to display that record.
  90.  
  91.         Set rsForm = Me.recordsetclone
  92.         strCriteria = strLinkMaster & " like " & Chr$(39) & varLinkValue & Chr$(42) & Chr$(39)
  93.         rsForm.FindFirst strCriteria
  94.         If Not rsForm.nomatch Then
  95.             Me.bookmark = rsForm.bookmark
  96.             '******This block is optional. Only use if you want to also highlight the subform record.
  97.             'Move focus to search field.
  98.             Searchfield.SetFocus
  99.             DoCmd FindRecord strOldSearchValue
  100.             '*******End of optional block
  101.         End If
  102.     End If
  103. ExitSearchSubForm:
  104.     Exit Function
  105. ErrorSearchSubForm:
  106.     If Err = 3070 Then
  107.         MsgBox "Check that you are on a subform field!"
  108.     Else
  109.         MsgBox Str$(Err) & " " & Error$
  110.     End If
  111.     Resume ExitSearchSubForm
  112. End Function
  113.