Filter data by using a form and open a report based on this data

A

AccessProject

Hello,

I created a form with 3 criterias: location, date to and from and course
name. I have a reset button and a filter/search button. I want to open a
report called "Sign-in" based on any of these criterias.

Onclick I have filter all 3 criterias but I don't know how to open a report.
I used the codes from previous posting:
Private Sub cmdFilter_Click()
....
'Text field example. Use quotes around the value in the string.
If Not IsNull(Me.txtFilterLocation) Then
strWhere = strWhere & "([Location] = """ & Me.txtFilterLocation &
""") AND "
End If

'Number field example. Do not add the extra quotes.
If Not IsNull(Me.cboFilterTrainingName) Then
strWhere = strWhere & "([TrainingName] = " &
Me.cboFilterTrainingName & ") AND "
End If

'Date field example. Use the format string to add the # delimiters and
get the right international format.
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([TrainingDate] >= " &
Format(Me.txtStartDate, conJetDate) & ") AND "
End If

'Another date field example. Use "less than the next day" since this
field has times as well as dates.
If Not IsNull(Me.txtEndDate) Then 'Less than the next day.
strWhere = strWhere & "([TrainingDate] < " & Format(Me.txtEndDate +
1, conJetDate) & ") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
'For debugging, remove the leading quote on the next line. Prints to
Immediate Window (Ctrl+G).
Debug.Print strWhere
Me.Filter = strWhere
Me.FilterOn = True
DoCmd.OpenReport "Sign-in Sheet", acViewPreview, , Me.Filter
End If

End Sub
 
M

Marshall Barton

AccessProject said:
I created a form with 3 criterias: location, date to and from and course
name. I have a reset button and a filter/search button. I want to open a
report called "Sign-in" based on any of these criterias.

Onclick I have filter all 3 criterias but I don't know how to open a report.
I used the codes from previous posting:
Private Sub cmdFilter_Click()
...
'Text field example. Use quotes around the value in the string.
If Not IsNull(Me.txtFilterLocation) Then
strWhere = strWhere & "([Location] = """ & Me.txtFilterLocation &
""") AND "
End If

'Number field example. Do not add the extra quotes.
If Not IsNull(Me.cboFilterTrainingName) Then
strWhere = strWhere & "([TrainingName] = " &
Me.cboFilterTrainingName & ") AND "
End If

'Date field example. Use the format string to add the # delimiters and
get the right international format.
If Not IsNull(Me.txtStartDate) Then
strWhere = strWhere & "([TrainingDate] >= " &
Format(Me.txtStartDate, conJetDate) & ") AND "
End If

'Another date field example. Use "less than the next day" since this
field has times as well as dates.
If Not IsNull(Me.txtEndDate) Then 'Less than the next day.
strWhere = strWhere & "([TrainingDate] < " & Format(Me.txtEndDate +
1, conJetDate) & ") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left$(strWhere, lngLen)
'For debugging, remove the leading quote on the next line. Prints to
Immediate Window (Ctrl+G).
Debug.Print strWhere
Me.Filter = strWhere
Me.FilterOn = True
DoCmd.OpenReport "Sign-in Sheet", acViewPreview, , Me.Filter
End If

End Sub


I don't understand why you are messing with the form's
Filter, but from where I sit the rest of the code looks like
it should work. Did you try it? If not, why not? If you
did, what about it didn't do what you want?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top