troubleshooting Question

combo box returns records for multiple subforms/Streamline my code

Avatar of Karen Schaefer
Karen SchaeferFlag for United States of America asked on
Microsoft AccessMicrosoft DevelopmentSQL
3 Comments1 Solution476 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
ramrom
consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros