Function Code_Fragment_7_14()

Dim dbs As Database
Dim rst_a, rst_b, rst_c, rst_d As Recordset
Dim Query_a, Query_b, Query_c, Query_d As String

Set dbs = CurrentDb()

'Since Access does not support the statement INSERT INTO VALUES .... WHERE
'You must first check the where clause and depending on the result allow or disallow the insertion

' This is the way to chek the WHERE clause
Query_a = "SELECT *" _
        & "FROM INCUMBENTS AS I2 " _
        & "WHERE I2.SSN = " & Chr(34) & "111223333" & Chr(34) & " " _
        & "  AND I2.START_DATE < #01/01/1998# " _
        & "  AND I2.END_DATE > #01/01/1997#;"
Query_b = "SELECT *" _
        & "FROM POSITIONS AS P " _
        & "WHERE P.PCN = " & Chr(34) & "341288" & Chr(34) & " " _
        & "  AND P.START_DATE <= #01/01/1997# " _
        & "  AND P.END_DATE > #01/01/1997#;"
Query_c = "SELECT *" _
        & "FROM POSITIONS AS P " _
        & "WHERE P.PCN = " & Chr(34) & "341288" & Chr(34) & " " _
        & "  AND P.START_DATE < #01/01/1998# " _
        & "  AND P.END_DATE >= #01/01/1998#;"


Query_d = "SELECT *" _
        & "FROM POSITIONS AS P " _
        & "WHERE P.PCN = " & Chr(34) & "341288" & Chr(34) & " " _
        & "  AND P.END_DATE > #01/01/1997# " _
        & "  AND P.END_DATE < #01/01/1998# " _
        & "  AND NOT EXISTS ( SELECT * " _
        & "                   FROM POSITIONS AS P2 " _
        & "                   WHERE P2.PCN = " & Chr(34) & "341288" & Chr(34) & " " _
        & "                     AND P2.START_DATE <= P.END_DATE " _
        & "                     AND P.END_DATE < P2.END_DATE);"
      
      
      
Set rst_a = dbs.OpenRecordset(Query_a)
Set rst_b = dbs.OpenRecordset(Query_b)
Set rst_c = dbs.OpenRecordset(Query_c)
Set rst_d = dbs.OpenRecordset(Query_d)

' This is the way to ask for NOT EXIST (if the result is empty then NOT EXIST is true)
If (rst_a.EOF And rst_a.BOF) And Not (rst_b.EOF And rst_b.BOF) And Not (rst_c.EOF And rst_c.BOF) And (rst_d.EOF And rst_d.BOF) Then
    dbs.Execute "INSERT INTO INCUMBENTS " _
              & "VALUES (" & Chr(34) & "111223333" & Chr(34) & ", " & Chr(34) & "341288" & Chr(34) & ", " _
              & "#01/01/1997#,#01/01/1998#);"
    MsgBox ("Insertion completed")
Else
    MsgBox ("You entered an invalid input")
End If

End Function
