Sorry, I have the date subtraction backwards. DOH! Change code to: EndDate - StartDate. I also forgot to set the dteDate variable before entering the loop. Need to reset dteDate variable outside the If Then. SiteID is text field so need apostrophe delimiters. And I just realized need to add 1 to the calculated date difference. Working code:
Code:
Dim dteDate As Date
Dim i As Integer, intDays As Integer
intDays = CDate(Me.TxtEnd) - CDate(Me.txtStart) + 1
dteDate = CDate(Me.txtStart)
For i = 1 To intDays
If IsNull(DLookup("ID", "MyTable", "Datadate = #" & dteDate & "# AND SiteID='" & Me.SiteID & "'")) Then
CurrentDb.Execute "INSERT INTO MyTable(Datadate, SiteID) VALUES(#" & dteDate & "#, " & Me.SiteID & ")"
End If
dteDate = dteDate + 1
Next
Me.Requery
Debug techniques could help reveal these errors in code. Review link at bottom of my post. Why don't you apply those techniques to the bad code as learning exercise before changing.