reportbuilder

Hi all,
I have seen forms in databases where users select from unbound fields and click a button that would give them a report based on their selections. Here's is the code but it give me an error "object required"
Private Sub cmdPreview_Click()
On Error GoTo Err_cmdPreview_Click
Dim strSQL As String
Dim strnewQuery As String
strSQL = strSQL & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "

strnewQuery = "SELECT qryOccurences_All.*, qryOccurences_All. FROM qryOccurences_All.IDX_OccurID WHERE " & strSQL & ";"
Dim rptDocName As String
rptDocName = "rptOccur_All"
DoCmd.OpenReport rptDocName, acPreview

If strSQL <> "" Then
MsgBox "There are no records that match your criteria! Please select new criteria.", vbInformation
Exit Sub

Else
DoCmd.OpenReport "rptOccur_All", acViewPreview
DoCmd.Close acForm, "frmReportbuiler"
Exit Sub
Exit_cmdPreview_Click:
Exit Sub
Err_cmdPreview_Click:
MsgBox Err.Description
Resume Exit_cmdPreview_Click
Resume
End If
End Sub
Please help me with my code.
Thanks


Answer this question

reportbuilder

  • V&amp;#237;ctor M.

    strSQL = strSQL & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "
  • CarlBrochu

    Hi,

    If that line is giving you an object required error then I'd check if the ctl variable has been defined. Your code snippet doesn't define it. Thats the only thing on that line that would cause that error.

    Also this line...

    If strSQL <> "" Then

    will always be true because you always set strSQL's value with the line thats causing the error. You'll want to check a RecordCount here.



  • Josamoto80

    also.... sorry...

    it looks like your code has been set up to run in a loop, but your not calling it in a loop. So you can remove the inital strSQL from the start of this line.

    strSQL = strSQL & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "
    Also you'll want to remove the trailing ' & " And " ' from this line too. When you use strSQL in this line

    strnewQuery = "SELECT qryOccurences_All.*, qryOccurences_All. FROM qryOccurences_All.IDX_OccurID WHERE " & strSQL & ";"
    The trailing And makes the SELECT statement invalid. You end up with...
    SELECT qry...... WHERE [ ] like ' ' And;


  • Victor Lobanov

    Hi,

    What line of code gives you the error



  • reportbuilder