Hi I am trying to run a change an existing query in real time to allow date filtering for 4 different categories. I can get two of them to work. Calibration Date and Icepoint Date. But for The two calculated fields Calibration due date and Icepoint date I cannot get it to filter properly e.g. for 2013 dates it also includes 2014 dates and just does not work properly. I am thinking its due to it being a calculated field but don't have a clue how to fix it. see pasted code for calibration due date filter where Todate and Fromdate are the 2 dates used. I also have the on current code and the exit code to reset the query to its original status.
any help is much appreciated.
Thanks
Public Sub SetDate1()
'Apply date filter and rebuild query in real time
On Error GoTo Err_SetDate1
If IsNull(Me!ToDate) And IsNull(Me!FromDate) Then
MsgBox ("Please Enter Date First"), vbExclamation
GoTo Exit_SetDate1
End If
If IsNull(Me!ToDate) Then Me!ToDate = Me!FromDate
If IsNull(Me!FromDate) Then Me!FromDate = Me!ToDate
Dim A As Date
Me.FromDate = Format([Forms]![frmMain]![FromDate], "dd/mm/yyyy")
Me.ToDate = Format([Forms]![frmMain]![ToDate], "dd/mm/yyyy")
A = [Forms]![frmMain]![FromDate]
If [Forms]![frmMain]![FromDate] = [Forms]![frmMain]![ToDate] Then
wSQL = " SELECT qryR2.[Thermometer ID], qryR2.Range, tblThermometers.Type, qryL2.[Location Description], qryL2.[Location ID], qryLC2.Range, qryL2.Department, qryLC2.[Calibration Date], qryIP2.[Icepoint Date], qryL2.[Responsible Person], IIf(IsNull([qryR2].[Deactivation Date]),-1,0) AS Active, qryR2.[Activation Date], qryR2.[Deactivation Date], IIf([qryLC2].[Pass/Fail]='Fail','Decomission or Repeat',Format(DateAdd('m',[qryR2].[Calibration Term],[qryLC2].[Calibration Date]),'dd/mm/yyyy')) AS [Calibration Due], IIf([tblThermometers].[Icepoint Required]=False,'NA',IIf([qryIP2].[Pass/Fail]='Fail','Repeat Full Calibration',Format(DateAdd('m',[tblThermometers].[Icepoint Term],[qryIP2].[Icepoint Date]),'dd/mm/yyyy'))) AS [Icepoint Due]" & _
" FROM ((tblThermometers RIGHT JOIN (qryL2 RIGHT JOIN qryR2 ON qryL2.[Thermometer ID] = qryR2.[Thermometer ID]) ON tblThermometers.[Thermometer ID] = qryR2.[Thermometer ID]) LEFT JOIN qryIP2 ON qryR2.[Thermometer ID] = qryIP2.[Working Ref]) LEFT JOIN qryLC2 ON (qryR2.[Thermometer ID] = qryLC2.[Working Ref]) AND (qryR2.Range = qryLC2.Range)"
wSQL = wSQL & " WHERE (((IIf(IsNull([qryR2].[Deactivation Date]),-1,0))=[Forms]![frmMain]![Active]) AND ((IIf([qryLC2].[Pass/Fail]='Fail','Decomission or Repeat',Format(DateAdd('m',[qryR2].[Calibration Term],[qryLC2].[Calibration Date]),'dd/mm/yyyy')))" & "=" & "#" & A & "#" & ") AND ((IIf(IsNull([Forms]![frmMain]![RangeFilter]),True,[qryR2].[Range]=[Forms]![frmMain]![RangeFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![TypeFilter]),True,[tblThermometers].[Type]=[Forms]![frmMain]![TypeFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![LocationDescriptionFilter]),True,[qryL2].[Location Description]=[Forms]![frmMain]![LocationDescriptionFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![LocationIDFilter]),True,[qryL2].[Location ID]=[Forms]![frmMain]![LocationIDFilter]))<>False)" & _
" AND ((IIf(IsNull([Forms]![frmMain]![DepartmentFilter]),True,[qryL2].[Department]=[Forms]![frmMain]![DepartmentFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![ResponsiblePersonFilter]),True,[qryL2].[Responsible Person]=[Forms]![frmMain]![ResponsiblePersonFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![ThermometerIDFilter]),True,[qryR2].[Thermometer ID]=[Forms]![frmMain]![ThermometerIDFilter]))<>False));"
rsSQL.SQL = wSQL
ElseIf [Forms]![frmMain]![FromDate] > [Forms]![frmMain]![ToDate] Then
MsgBox ("Your Date Range is Incorrect"), vbCritical
Else
wSQL = " SELECT qryR2.[Thermometer ID], qryR2.Range, tblThermometers.Type, qryL2.[Location Description], qryL2.[Location ID], qryLC2.Range, qryL2.Department, qryLC2.[Calibration Date], qryIP2.[Icepoint Date], qryL2.[Responsible Person], IIf(IsNull([qryR2].[Deactivation Date]),-1,0) AS Active, qryR2.[Activation Date], qryR2.[Deactivation Date], IIf([qryLC2].[Pass/Fail]='Fail','Decomission or Repeat',Format(DateAdd('m',[qryR2].[Calibration Term],[qryLC2].[Calibration Date]),'dd/mm/yyyy')) AS [Calibration Due], IIf([tblThermometers].[Icepoint Required]=False,'NA',IIf([qryIP2].[Pass/Fail]='Fail','Repeat Full Calibration',Format(DateAdd('m',[tblThermometers].[Icepoint Term],[qryIP2].[Icepoint Date]),'dd/mm/yyyy'))) AS [Icepoint Due]" & _
" FROM ((tblThermometers RIGHT JOIN (qryL2 RIGHT JOIN qryR2 ON qryL2.[Thermometer ID] = qryR2.[Thermometer ID]) ON tblThermometers.[Thermometer ID] = qryR2.[Thermometer ID]) LEFT JOIN qryIP2 ON qryR2.[Thermometer ID] = qryIP2.[Working Ref]) LEFT JOIN qryLC2 ON (qryR2.[Thermometer ID] = qryLC2.[Working Ref]) AND (qryR2.Range = qryLC2.Range)"
wSQL = wSQL & " WHERE (((IIf(IsNull([qryR2].[Deactivation Date]),-1,0))=[Forms]![frmMain]![Active]) AND ((IIf([qryLC2].[Pass/Fail]='Fail','Decomission or Repeat',Format(DateAdd('m',[qryR2].[Calibration Term],[qryLC2].[Calibration Date]),'dd/mm/yyyy'))) Between [Forms]![frmMain]![FromDate] And [Forms]![frmMain]![ToDate]) AND ((IIf(IsNull([Forms]![frmMain]![RangeFilter]),True,[qryR2].[Range]=[Forms]![frmMain]![RangeFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![TypeFilter]),True,[tblThermometers].[Type]=[Forms]![frmMain]![TypeFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![LocationDescriptionFilter]),True,[qryL2].[Location Description]=[Forms]![frmMain]![LocationDescriptionFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![LocationIDFilter]),True,[qryL2].[Location ID]=[Forms]![frmMain]![LocationIDFilter]))<>False)" & _
" AND ((IIf(IsNull([Forms]![frmMain]![DepartmentFilter]),True,[qryL2].[Department]=[Forms]![frmMain]![DepartmentFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![ResponsiblePersonFilter]),True,[qryL2].[Responsible Person]=[Forms]![frmMain]![ResponsiblePersonFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![ThermometerIDFilter]),True,[qryR2].[Thermometer ID]=[Forms]![frmMain]![ThermometerIDFilter]))<>False));"
rsSQL.SQL = wSQL
End If
Me.frmLatestCalibrations.Form.RecordSource = "qryLog"
Exit_SetDate1:
Exit Sub
Err_SetDate1:
MsgBox Err.Description
Resume Exit_SetDate1
End Sub
any help is much appreciated.
Thanks
Public Sub SetDate1()
'Apply date filter and rebuild query in real time
On Error GoTo Err_SetDate1
If IsNull(Me!ToDate) And IsNull(Me!FromDate) Then
MsgBox ("Please Enter Date First"), vbExclamation
GoTo Exit_SetDate1
End If
If IsNull(Me!ToDate) Then Me!ToDate = Me!FromDate
If IsNull(Me!FromDate) Then Me!FromDate = Me!ToDate
Dim A As Date
Me.FromDate = Format([Forms]![frmMain]![FromDate], "dd/mm/yyyy")
Me.ToDate = Format([Forms]![frmMain]![ToDate], "dd/mm/yyyy")
A = [Forms]![frmMain]![FromDate]
If [Forms]![frmMain]![FromDate] = [Forms]![frmMain]![ToDate] Then
wSQL = " SELECT qryR2.[Thermometer ID], qryR2.Range, tblThermometers.Type, qryL2.[Location Description], qryL2.[Location ID], qryLC2.Range, qryL2.Department, qryLC2.[Calibration Date], qryIP2.[Icepoint Date], qryL2.[Responsible Person], IIf(IsNull([qryR2].[Deactivation Date]),-1,0) AS Active, qryR2.[Activation Date], qryR2.[Deactivation Date], IIf([qryLC2].[Pass/Fail]='Fail','Decomission or Repeat',Format(DateAdd('m',[qryR2].[Calibration Term],[qryLC2].[Calibration Date]),'dd/mm/yyyy')) AS [Calibration Due], IIf([tblThermometers].[Icepoint Required]=False,'NA',IIf([qryIP2].[Pass/Fail]='Fail','Repeat Full Calibration',Format(DateAdd('m',[tblThermometers].[Icepoint Term],[qryIP2].[Icepoint Date]),'dd/mm/yyyy'))) AS [Icepoint Due]" & _
" FROM ((tblThermometers RIGHT JOIN (qryL2 RIGHT JOIN qryR2 ON qryL2.[Thermometer ID] = qryR2.[Thermometer ID]) ON tblThermometers.[Thermometer ID] = qryR2.[Thermometer ID]) LEFT JOIN qryIP2 ON qryR2.[Thermometer ID] = qryIP2.[Working Ref]) LEFT JOIN qryLC2 ON (qryR2.[Thermometer ID] = qryLC2.[Working Ref]) AND (qryR2.Range = qryLC2.Range)"
wSQL = wSQL & " WHERE (((IIf(IsNull([qryR2].[Deactivation Date]),-1,0))=[Forms]![frmMain]![Active]) AND ((IIf([qryLC2].[Pass/Fail]='Fail','Decomission or Repeat',Format(DateAdd('m',[qryR2].[Calibration Term],[qryLC2].[Calibration Date]),'dd/mm/yyyy')))" & "=" & "#" & A & "#" & ") AND ((IIf(IsNull([Forms]![frmMain]![RangeFilter]),True,[qryR2].[Range]=[Forms]![frmMain]![RangeFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![TypeFilter]),True,[tblThermometers].[Type]=[Forms]![frmMain]![TypeFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![LocationDescriptionFilter]),True,[qryL2].[Location Description]=[Forms]![frmMain]![LocationDescriptionFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![LocationIDFilter]),True,[qryL2].[Location ID]=[Forms]![frmMain]![LocationIDFilter]))<>False)" & _
" AND ((IIf(IsNull([Forms]![frmMain]![DepartmentFilter]),True,[qryL2].[Department]=[Forms]![frmMain]![DepartmentFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![ResponsiblePersonFilter]),True,[qryL2].[Responsible Person]=[Forms]![frmMain]![ResponsiblePersonFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![ThermometerIDFilter]),True,[qryR2].[Thermometer ID]=[Forms]![frmMain]![ThermometerIDFilter]))<>False));"
rsSQL.SQL = wSQL
ElseIf [Forms]![frmMain]![FromDate] > [Forms]![frmMain]![ToDate] Then
MsgBox ("Your Date Range is Incorrect"), vbCritical
Else
wSQL = " SELECT qryR2.[Thermometer ID], qryR2.Range, tblThermometers.Type, qryL2.[Location Description], qryL2.[Location ID], qryLC2.Range, qryL2.Department, qryLC2.[Calibration Date], qryIP2.[Icepoint Date], qryL2.[Responsible Person], IIf(IsNull([qryR2].[Deactivation Date]),-1,0) AS Active, qryR2.[Activation Date], qryR2.[Deactivation Date], IIf([qryLC2].[Pass/Fail]='Fail','Decomission or Repeat',Format(DateAdd('m',[qryR2].[Calibration Term],[qryLC2].[Calibration Date]),'dd/mm/yyyy')) AS [Calibration Due], IIf([tblThermometers].[Icepoint Required]=False,'NA',IIf([qryIP2].[Pass/Fail]='Fail','Repeat Full Calibration',Format(DateAdd('m',[tblThermometers].[Icepoint Term],[qryIP2].[Icepoint Date]),'dd/mm/yyyy'))) AS [Icepoint Due]" & _
" FROM ((tblThermometers RIGHT JOIN (qryL2 RIGHT JOIN qryR2 ON qryL2.[Thermometer ID] = qryR2.[Thermometer ID]) ON tblThermometers.[Thermometer ID] = qryR2.[Thermometer ID]) LEFT JOIN qryIP2 ON qryR2.[Thermometer ID] = qryIP2.[Working Ref]) LEFT JOIN qryLC2 ON (qryR2.[Thermometer ID] = qryLC2.[Working Ref]) AND (qryR2.Range = qryLC2.Range)"
wSQL = wSQL & " WHERE (((IIf(IsNull([qryR2].[Deactivation Date]),-1,0))=[Forms]![frmMain]![Active]) AND ((IIf([qryLC2].[Pass/Fail]='Fail','Decomission or Repeat',Format(DateAdd('m',[qryR2].[Calibration Term],[qryLC2].[Calibration Date]),'dd/mm/yyyy'))) Between [Forms]![frmMain]![FromDate] And [Forms]![frmMain]![ToDate]) AND ((IIf(IsNull([Forms]![frmMain]![RangeFilter]),True,[qryR2].[Range]=[Forms]![frmMain]![RangeFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![TypeFilter]),True,[tblThermometers].[Type]=[Forms]![frmMain]![TypeFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![LocationDescriptionFilter]),True,[qryL2].[Location Description]=[Forms]![frmMain]![LocationDescriptionFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![LocationIDFilter]),True,[qryL2].[Location ID]=[Forms]![frmMain]![LocationIDFilter]))<>False)" & _
" AND ((IIf(IsNull([Forms]![frmMain]![DepartmentFilter]),True,[qryL2].[Department]=[Forms]![frmMain]![DepartmentFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![ResponsiblePersonFilter]),True,[qryL2].[Responsible Person]=[Forms]![frmMain]![ResponsiblePersonFilter]))<>False) AND ((IIf(IsNull([Forms]![frmMain]![ThermometerIDFilter]),True,[qryR2].[Thermometer ID]=[Forms]![frmMain]![ThermometerIDFilter]))<>False));"
rsSQL.SQL = wSQL
End If
Me.frmLatestCalibrations.Form.RecordSource = "qryLog"
Exit_SetDate1:
Exit Sub
Err_SetDate1:
MsgBox Err.Description
Resume Exit_SetDate1
End Sub
Last edited: