Report based on query (1 Viewer)

rkaptu

Registered User.
Local time
Today, 07:35
Joined
Oct 27, 2017
Messages
19
HI

I am working on a report based on a query that displays data between 2 different dates.

The query is based on a table that includes one column with Inspection dates, and in the query I added a Criteria 'Between [Start_Date] and [End_Date]'. I also created two parameters in the Query Expr1:[Start_Date] and Expr2:[End_Date].

When report loads it asks me to enter manually the Start and End Date.

I would like to control this by loading a form in the Open_Report Event where dates can be entered with the date picker to make it easier and avoid invalid data. How can I do this?

So far I used this code, but it is still asking me the missing parameters as if there is nothing

Private Sub Report_Open(Cancel As Integer)

Dim rst As DAO.Recordset
Dim dbs As DAO.Database
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef

Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("Task Schedule Query by Inspector")

qdf.Parameters.Refresh
qdf.Parameters("Start_DAte").Value = Date
qdf.Parameters("End_Date").Value = Date + 1

Set rs = qdf.OpenRecordset()

End Sub


Thanks in advance
 

Ranman256

Well-known member
Local time
Today, 10:35
Joined
Apr 9, 2015
Messages
4,337
have you tried the report using a query? (NOT code)
 

sxschech

Registered User.
Local time
Today, 07:35
Joined
Mar 2, 2010
Messages
792
To create the report using a parameter on a form you would modify your query to point to the field on the form. You shouldn't need to create a recordset.

Here is an example from one of my queries.
Code:
WHERE qryEventEvaluations.EndDate =[Forms]![frmSurveys].[txtEventDate]
If the form is open, then when you run the report the criteria will be based on what is in the form field. If the form is closed, a parameter will come up and you can manually enter the information.

Also, if your query is actually named "Task Schedule Query by Inspector"
would suggest either removing the spaces or using underscores:
TaskScheduleQuerybyInspector
Task_Schedule_Query_by_Inspector

Otherwise may have issues down the road as will need to put brackets around it.

Then you put the query name in the Recordsource of the report instead of the vba recordset.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:35
Joined
Feb 19, 2002
Messages
43,257
Although you certainly can open a form from within the report to capture parameters, it is more logical to use a Form to open the report and collect the arguments on that form. That allows you to have one form that can run multiple reports and capture the same arguments without having to reenter them.

The report opens its own Recordset which is why your attempt isn't working.

If you use a form to open the report, you could use the where argument of the OpenReport method to pass in the values from controls on the form.
Code:
    Dim strWhere As String
    If IsDate(Me.txtStartDate) Then
        If IsDate(Me.txtEndDate) Then
            If Me.txtStartDate <= Me.txtEndDate Then
                strWhere = "InspectionDate Between #" & Me.txtStartDate & "# AND #" & Me.txtEndDate & "#"
                DoCmd.OpenReport "reportname", acViewPreview, , strWhere
            Else
                MsgBox "Start Date must be <= End Date.", vbOKOnly
                Exit Sub
            End If
        Else
            MsgBox "End date is required.", vbOKOnly
            Exit Sub
        End If
    Else
        MsgBox "Start Date is required.", vbOKOnly
        Exit Sub
    End If
Using this metnod, the report would not have criteria dealing with the InspectionDate although it might have non-variable criteria.
 

rkaptu

Registered User.
Local time
Today, 07:35
Joined
Oct 27, 2017
Messages
19
Thanks for all your replies.

So I have created a form from which I call the report. In the form user can enter date range, which I am passing to the report as an openarg, as shown below.

DoCmd.OpenReport "weekly_report", acViewReport, , , , Me.DateRange & "|" & inspectors

Where Me.DateRange refers to the value of a textbox in the form. Inspectors is a string listing the inspectors for whom report is needed.

In the Open Report Event I confirmed that the OpenArg is coming through correctly and I managed to separate the date range from the rest.

Now the query uses the 2 variables Start_date and End_Date to limit the records shown. How do I assign value to these 2 variables? I change query name to Weekly_Report_Query

Thanks
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:35
Joined
Feb 19, 2002
Messages
43,257
Perhaps you should try my suggestion.
 

Users who are viewing this thread

Top Bottom