combo box returns records for multiple subforms/Streamline my code

Karen Schaefer
Karen Schaefer used Ask the Experts™
on
My form has 1 lookup field(combo) to return a selected Program(school)'s data on 5 different subforms.  I need to be able to return a limited group of data or ALL data for each subform, depending on lookup field's results.  

The code returns the correct record for the first selection on open of the form, however, the drop down field(search) become disabled and I am unable to make another selection.  As you can see I tried requery/refresh without success.

Below please find my code for the AfterUpdate of the lookup field.

I am also looking for suggestions on how to streamline my code - I need to be able to set the recordsource of the subforms on the fly.

Thanks,
K
Private Sub cboSearch_AfterUpdate()
    Dim rs As Object
    Dim nRecID As String
    Dim strSQL As String
    Dim strSql1 As String
    Dim strSql2 As String
    Dim strSql3 As String
    Dim strSql4 As String
    
   On Error GoTo cboSearch_AfterUpdate_Error
    
    nRecID = Me.cboSearch
    strSQL = "SELECT tblAssignPrgSchool.ProgramRecID, tblPrograms.[Program Title]," & _
                " tblPrograms.[Program Type], tblPrograms.[Program Engagement]," & _
                " tblPrograms.[Program Meetings], tblPrograms.[Program Updates]," & _
                " tblPrograms.[IP Agreement], tblPrograms.[IP Expiration Date]," & _
                " tblPrograms.ROI, tblPrograms.Linked_CTC_CTE, tblPrograms.ERT," & _
                " tblPrograms.Program_Ranking, tblAssignPrgSchool.SchoolNameRecID," & _
                " tblAssignPrgSchool.DateModified" & _
            " FROM tblPrograms INNER JOIN tblAssignPrgSchool ON" & _
                " tblPrograms.ProgramRecID = tblAssignPrgSchool.ProgramRecID"
    
    If nRecID = 0 Then 'frmAssignSchoolPrg_sub
        strSQL = strSQL
    Else
        strSQL = strSQL & " WHERE tblPrograms.ProgramRecID = " & nRecID & ""

    End If
    
    strSql1 = "SELECT *" & _
            " FROM tblPrgContacts"

    If nRecID = 0 Then  'frmlPrgContacts_sub
        strSql1 = strSql1
    Else
        strSql1 = strSql1 & " WHERE tblPrgContacts.ProgramRecID = " & nRecID & ""

    End If
  
    strSql2 = "SELECT *" & _
            " FROM tblProgramCertificates"

    If nRecID = 0 Then  'frmPrgCert_sub
        strSql2 = strSql2
    Else
        strSql2 = strSql2 & " WHERE tblProgramCertificates.ProgramRecID = " & nRecID & ""

    End If
    
    strSql3 = "SELECT tblGrants_Assign.GrantRecID, tblGrants_Assign.SchoolNameRecID," & _
                " tblGrants_Assign.GrantAmount, tblGrantInfo.GrantStartDate, tblGrantInfo.GrantEndDate," & _
                " tblGrants_Assign.LeadSchool, tblGrants_Assign.ProgramRecID, tblGrants_Assign.DateModified," & _
                " tblGrants_Assign.GrantComments, tblGrants_Assign.DateModified" & _
            " FROM tblGrantInfo INNER JOIN tblGrants_Assign ON" & _
                " tblGrantInfo.GrantRecID = tblGrants_Assign.GrantRecID"
    
    If nRecID = 0 Then  'frmAttachments_sub
        strSql3 = strSql3
    Else
        strSql3 = strSql3 & " WHERE tblGrants_Assign.ProgramRecID = " & nRecID & ""

    End If
    
    strSql4 = "SELECT *" & _
            " FROM tblAttachments"
    
    If nRecID = 0 Then
        strSql4 = strSql4
    Else
        strSql4 = strSql4 & " WHERE tblAttachments.ProgramRecID = " & nRecID & ""

    End If

    Forms![frmPrograms]![frmAssignSchoolPrg_sub].Form.RecordSource = strSQL
    Forms![frmPrograms]![frmlPrgContacts_sub].Form.RecordSource = strSql1
    Forms![frmPrograms]![frmPrgCert_sub].Form.RecordSource = strSql2
    Forms![frmPrograms]![frmGrants_Assign_sub].Form.RecordSource = strSql3
    Forms![frmPrograms]![frmAttachments_sub].Form.RecordSource = strSql4
    Refresh
   On Error GoTo 0
   Exit Sub

cboSearch_AfterUpdate_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cboSearch_AfterUpdate of VBA Document Form_frmPrograms"

End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
What happens if you change:
           Dim nRecID As String
   
to this:
          Dim nRecID As Long      'Long integer?
consultant
Commented:
Do you mean the form has a combobox control?
What is its rowsource?
Is it bound to anything (should not be).
How do you know it is disabed? Did you explicitly examine its enabled property?
I see nothing in your code that could affect this property.

You could simplify things by setting the subforms' recordsources to select statements without where clauses and setting their filters to "ProgramRecID =" & nRecID

This requires setting the link master and child fields on the subform controls.

You could assign 1 letter aliases to the tables,  shortening the code.

I see no need to refer to tblAssignPrgSchool in the first query.

What is on  the main form besides the combo and subforms?

You could enclose the final assignments in a with block:
    with Forms![frmPrograms]
        ![frmAssignSchoolPrg_sub].Form.RecordSource = strSQL
        ]![frmlPrgContacts_sub].Form.RecordSource = strSql1
        ![frmPrgCert_sub].Form.RecordSource = strSql2
        ]![frmGrants_Assign_sub].Form.RecordSource = strSql3
        ]![frmAttachments_sub].Form.RecordSource = strSql4
    end with
Karen SchaeferBI ANALYST

Author

Commented:
thanks for your input.

K

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial