Avatar of Karen Schaefer
Karen SchaeferFlag for United States of America

asked on 

combo box returns records for multiple subforms/Streamline my code

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

Microsoft AccessMicrosoft DevelopmentSQL

Avatar of undefined
Last Comment
Karen Schaefer
Avatar of puppydogbuddy
puppydogbuddy

What happens if you change:
           Dim nRecID As String
   
to this:
          Dim nRecID As Long      'Long integer?
ASKER CERTIFIED SOLUTION
Avatar of ramrom
ramrom
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Karen Schaefer

ASKER

thanks for your input.

K
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo