HillTJ
To train a dog, first know more than the dog..
- Local time
 - Today, 05:04
 
- Joined
 - Apr 1, 2019
 
- Messages
 - 731
 
Hi, I'm writing an equipment calibration database, where I lookup the date of last calibration, add a new "frequency" & save the new record. See my code attached. This code works for all records where there is a previous one, but crashes when a new record is entered. It assumes a date exists in [Anniversary_Date]. I had a go with the 'if not null' line. but this didn't work.
Would appreciate a heads up.
	
	
	
		
 Would appreciate a heads up.
		Code:
	
	
	Private Sub Calibration_Frequency_AfterUpdate()
Dim ID As Long
Dim Temp_Date As Date
On Error GoTo ErrorHandler
If Not IsNull(Me.[Anniversary_Date]) Then ' This affects the new Record too!!
   ID = DMax("InspectID", "Qry_Inspect_Equipment")
      
   Temp_Date = DLookup("[Anniversary_Date]", "TBL_Inspect_Record", "InspectID=" & ID)
    Select Case [Calibration_Frequency]
    Case 1 ' Annually
        Me.[Anniversary_Date] = DateAdd("yyyy", [Calibration_Unit], [Temp_Date])
    Case 2 'Monthly
        Me.[Anniversary_Date] = DateAdd("m", [Calibration_Unit], [Temp_Date])
    Case 3 'Weekly (Weekdays)
        Me.[Anniversary_Date] = DateAdd("ww", [Calibration_Unit], [Temp_Date])
    Case 4 ' Daily
        Me.[Anniversary_Date] = DateAdd("d", [Calibration_Unit], [Temp_Date])
    Case Else
    End Select
End If
ExitError:
    Exit Sub
    
ErrorHandler:
  Select Case Err.Number
    Case 9999
        Resume Next
    Case 999
        Resume Next
    Case Else
        Call LogError(Err.Number, Err.Description, "Calculate Anniversary Date")
        Resume ExitError
    End Select
 
End Sub