Martyn,
If I understand you correctly that you wish to allow your users to select
one or more criteria for a report, the way I do it is to present the users
combo boxes that list the criteria selections on an unbound form.
In the AfterUpdate event procedure of each combo box, a general procedure,
WriteFilterString, is called that loops through all of the form controls, and
builds an SQL statement, writing it to an invisible textbox on the form,
txtFilterString.
When the user presses the Preview Report button, the report either runs the
report with no criteria (if txtFilterString is null) or uses the built string
as the optional Filter parameter of the OpenReport method. Another button
allows the user to clear all criteria.
Somehow, you need to embed in the code which table *field* is associated
which each combo box value. I do it by naming the control "txt" +
<fieldname>. The code then strips off the first 3 characters to get the
field name. Alternatively, you could use the control's Tag property.
Hope that helps.
Sprinks
Private Sub WriteFilterString()
Dim intindex As Integer
Dim ctl As Control
On Error Resume Next
'Reinitialize control
Me!txtFilterString = ""
' Loop through all form controls; if there's data, add to filter string
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acComboBox
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
‘ Handle other control types if they need to be handled differently
here
' To use a textbox value as a wildcard, use the Like operator:
' Me!txtFilterString = Me!txtFilterString & _
' "[" & LTrim(Right(ctl.name, Len(ctl.name) -
3)) _
' & "] Like " & "'" & "*" & ctl.Value & "*" &
"'" & " AND "
'
End Select
Next ctl
' Strip end of filter
Me!txtFilterString = Left(Me!txtFilterString, Len(Me!txtFilterString) - 5)
End Sub
Private Sub YourComboOrTextBox_AfterUpdate()
‘ AfterUpdate code for each parameter
Call WriteFilterString
End Sub
Private Sub cmdPreviewReport_Click()
On Error GoTo Err_cmdPreviewReport_Click
Dim strDocName As String
Dim strFilter As String
strDocName = "YourReport"
strFilter = ""
' If no criteria selected, preview entire report
If IsNull(Me!txtFilterString) Then
DoCmd.OpenReport strDocName, acViewPreview
Else
‘Otherwise, use filter the report by the built string
strFilter = Me!txtFilterString
DoCmd.OpenReport strDocName, acViewPreview, , strFilter
End If
Exit_cmdPreviewReport_Click:
Exit Sub
Err_cmdPreviewReport_Click:
MsgBox Err.Description
Resume Exit_cmdPreviewReport_Click
End Sub
Private Sub cmdClearSelection_Click()
' Reset all controls to blank
For Each ctl In Me.Controls
If (ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Or _
ctl.ControlType = acOptionGroup) Then
ctl.Value = Null
End If
Next ctl
End Sub
martyn said:
I have a parametised report. In Reports I select a report and then have to
type in a no., this queries a table and it then reports with correct
selection of data and I can print it. This is rubbish, User will need a form
whit a combo list of report headings (this is a field I hold)
How do I do this please. I am new !