Custom Query from user response

  • Thread starter Thread starter kenny
  • Start date Start date
K

kenny

I am trying to create a query for a report based on
several variable options.

1. Team Name
2. Job Class
3. Date Range (from 1/1/2004 - 2/5/2004)
4. Report type. (% of times the team was on goal or
counts of the total queue items.

It is possible to have a form for the user to set the
above variables and create a query and/or report on the
fly?

The db consists of 3 tables.
1. with the criteria
2. Teams
3. Job Functions.
 
Yes!

There are several approaches. One way I like is to use
combo boxes whereever possible for the input criteria
controls, and option groups to select equal to, greater
than, less than, or not equal to operators. Then you can
build an SQL query string either when the command button
to run the report is pressed, or incrementally, using the
AfterUpdate event of each control. The latter is helpful
while debugging.

Steven Lebans has a Windows-style calendar control on his
website to assist in inputting dates.

HTH
Kevin Sprinkel
 
Thank you.
So you are saying in the Build Event of the print option
form I can Construct a SQL string. How do I get the SQL
string to the report.

If I can create only two reports, one for the total queue
counts and another for the % of times on goal, then I can
provide the query the options for the team, job type, and
the date range.

Usually the report asks where it is getting its data from.
do I need to put into the control source "forms!
nameofmyform"?
 
Thank you.
So you are saying in the Build Event of the print option
form I can Construct a SQL string. How do I get the SQL
string to the report.

There is no form Build Event; I use the AfterUpdate event
of each control to build a selection string which I then
store in another control on the form. You can set Visible
= False for this control if you don't want your users to
see it. See the code below, which applies to a form
containing only combo boxes. The generic
WriteFilterString subroutine is called whenever one of the
combo boxes is updated. It loops through all the relevant
controls (ignoring labels, etc.), and writes the string
out to the invisible control.

To get the report to use the string as the criteria, set a
variable equal to the control, and pass the variable as an
argument to the OpenReport method. See the
cmdPreviewReport sub below.
If I can create only two reports, one for the total queue
counts and another for the % of times on goal, then I can
provide the query the options for the team, job type, and
the date range.
Usually the report asks where it is getting its data from.
do I need to put into the control source "forms!
nameofmyform"?

The report gets its data from the table or query specified
in the Form level Record Source property.

HTH
Kevin Sprinkel

Form Module Code
============================================

Option Compare Database

Private Sub cboBuildingType_AfterUpdate()
Call WriteFilterString
End Sub


Private Sub cboClient_AfterUpdate()
Call WriteFilterString
End Sub

Private Sub cboOwnerType_AfterUpdate()
Call WriteFilterString
End Sub

Private Sub WriteFilterString()
Dim intindex As Integer
Dim ctl As Control

'Reinitialize string
Me!txtFilterString = ""

' Loop through all form controls; if there's data, add
to filter string
For Each ctl In Me.Controls
If ctl.ControlType = acComboBox Then

' ctl.Name is the name of the control, e.g.,
' cboBuildingType. The field name corresponding to this
' control, for which I wish to set criteria is named
' BuildingType, so the code strips the 'cbo' from the
' control name to use in the filter string.

If (Nz(ctl.Value) <> 0 And Nz(ctl.Value) <> "") Then
Me!txtFilterString = Me!txtFilterString & LTrim
(Right(ctl.Name, Len(ctl.Name) - 3)) & _
"=" & ctl.Value & " AND "
End If
End If
Next ctl

' Strip end of filter
Me.txtFilterString = Left(Me.txtFilterString, Len
(Me.txtFilterString) - 5)
End Sub

Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click

Dim strDocName As String
Dim strFilter As String

stDocName = "rptBidReturn"
strFilter = Me.txtFilterString
DoCmd.OpenReport stDocName, acPreview, , strFilter

' Reset all controls
For Each ctl In Me.Controls
If ctl.ControlType = acComboBox Then
ctl.Value = ""
End If
Next ctl
Me!txtFilterString = ""

Exit_cmdPreviewReport_Click:
Exit Sub

Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click

End Sub
 
You have been very helpful.
One other thing.

I am trying to show in my report a percentage of times a
certain goal was met. So for the [urgoal] field (as bool)
I want to count the number of times it is true.
I saw where I can create a new text box and for the
control source enter =Count([urgoal]).

Now to provide a percentage of the times this goal was
actually met I need to count only the times it was true.

I tried -- =Count(Where[urgoal]=true). I get an error
that there is no where function in the expression builder.

How else might I go about counting the number of times
that field was true or false?
 
I am trying to show in my report a percentage of times a
certain goal was met. So for the [urgoal] field (as bool)
I want to count the number of times it is true.
I saw where I can create a new text box and for the
control source enter =Count([urgoal]).

How else might I go about counting the number of times
that field was true or false?

Since True = -1 and False = 0,

=Abs(Sum([yourbooleanfield]))

Kevin Sprinkel
 
Back
Top