This example is designed to make it really easy to add more filter boxes if
you ever need to. It solves several problems:
1. It applies the filter only if there is something in the controls (not if
they are left null.)
2. It checks the dates are valid.
3. It formats the dates the way JET expects, and adds the # delimiter. (Do
NOT change the Const line to match your regional settings: leave it as is.)
4. It illustrates how to print the contents of the filter string into the
Immediate Window. After running it, press Ctrl+G to see what the filter
looked like.
5. If your OverTimeDate field has a time component as well as the date, it
still returns the last date (by asking for 'less than the next day.')
6. If the person's name contains an apostropy, it still works (by using
double-quotes.)
Private Sub cmdPrint_Click()
Dim strWhere As String
Dim lngLen As Long
Const strcJetDate = "\#mm\/dd\/yyyy\#"
If Not IsNull(Me.cboEmployees) Then
strWhere = strWhere & "(EmpName = """ & Me.cboEmployees & """) AND "
End If
If IsDate(Me.StartDate) Then
strWhere =strWhere & "(OverTimeDate >= " & _
Format(Me.StartDate, strcJetDate) & ") AND "
End If
If IsDate(Me.EndDate) Then
strWhere =strWhere & "(OverTimeDate < " & _
Format(Me.EndDate + 1, strcJetDate) & ") AND "
End If
'Chop off the trailing " AND "
lngLen=Len(strWhere) - 5
If lngLen 0 Then
strWhere = Left$(strWhere, lngLen)
End If
Debug.Print strWhere
DoCmd.OpenReport "rptOverTimeApproval", acViewPreview, , strWhere
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users -
http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Naushad" <nh*****@kockw.comwrote in message
news:13**********************************@w7g2000h sa.googlegroups.com...
>
I am trying to open the report conditionally from the dialogue box
form. In the form there are three field.
cboEmployees
StartDate
EndDate
I have used these field in criteria to open the report. If I use the
field name and between command, the report is not opening but when I
use the greater than and less than equal to, the report is opening but
there is no records. The used code are as follows:
Private Sub cmdPrint_Click()
Dim strSelectEmp As String
'strSelectEmp = "[EmpName] = '" & Me.cboEmployees & "' AND
(datevalue([OverTimeDate]) Between '" & DateValue(Me.StartDate) And
DateValue(Me.EndDate) & "')"
strSelectEmp = "[EmpName] = '" & Me.cboEmployees & "' and
(datevalue([overtimedate]) >= '" & DateValue(Me.StartDate) & "' and
datevalue([overtimedate]) <= '" & DateValue(Me.EndDate) & "')"
DoCmd.OpenReport "rptOverTimeApproval", acViewPreview, ,
strSelectEmp
End Sub
Can anybody help me to solve the problem.
Thanks in advance
Naushad