I'm trying to feed a long string into the Where Condition of the DoCmd.OpenReport command. I'm getting a runtime error that my Where Condition is too long to run.
According to the docs:
In the code below I build a Where Condition based on the results of a query. The Where Condition is a string like below (this is a shorter version than in production):The maximum length of the WhereCondition argument is 32,768 characters (unlike the Where Condition action argument in the Macro window, whose maximum length is 256 characters).
You'll notice that there are some duplicates, which could be removed to shorten the string with some more logic. The problem is my FULL string is only ~5,000 characters, which is substantially less than the upper limit.ProjectID = 6 Or ProjectID = 6 Or ProjectID = 6 Or ProjectID = 19 Or ProjectID = 178 Or ProjectID = 178 Or ProjectID = 179 Or ProjectID = 179 Or ProjectID = 179 Or ProjectID = 180 Or ProjectID = 180
Code to generate the query string:
Code to generate the report:Code:Private Sub cboxRegion_AfterUpdate() Dim db As DAO.Database Dim rs As DAO.Recordset Dim selectedRegion As String Dim upperLimit As Integer Dim lowerLimit As Integer Dim rsQuery As String Dim query As String Dim count As Integer Me.tboxQuery = Null If Not IsNull(Me.cboxRegion) Then selectedRegion = Me.cboxRegion End If Set db = CurrentDb() ' Find all the projects that the selected individual has been assigned to rsQuery = "SELECT dbo_tblProjects.ProjectID " & _ "FROM (dbo_tblProjects INNER JOIN dbo_junctionProjectIndividuals ON dbo_tblProjects.ProjectID = dbo_junctionProjectIndividuals.ProjectID) " & _ "INNER JOIN dbo_luIndividuals ON dbo_junctionProjectIndividuals.IndividualID = dbo_luIndividuals.IndividualsID " & _ "WHERE dbo_luIndividuals.Region = '" & selectedRegion & "'; " Set rs = db.OpenRecordset(rsQuery, dbOpenDynaset, dbSeeChanges) rs.MoveLast upperLimit = rs.RecordCount If (rs.RecordCount = 0) Then MsgBox "This individual is not yet assigned to any projects!" Else lowerLimit = 0 count = 0 End If rs.MoveFirst 'Build Query String for report Do While Not rs.EOF ' Build a query string If count < upperLimit - 1 Then query = query + "ProjectID = " & rs!ProjectID & " Or " Else query = query + "ProjectID = " & rs!ProjectID & " " End If count = count + 1 'Move to the next record. Don't ever forget to do this. rs.MoveNext Loop Me.tboxQuery = query rs.Close Set rs = Nothing End Sub
Code:Private Sub Command10_Click() If IsNull(Me.tboxQuery) Then MsgBox "Please select a Region before attempting to run a report.", vbQuestion, "No Region selected" Else DoCmd.OpenReport "rpt_Complete", acViewReport, , Me.tboxQuery End If End Sub