I'm making a code in VBA, that can loop through dates and add some data to some tables in Access. I feel like i've tried everything and browsed the web and it still dosen't seem to work. I think my problem is the formatting of dates, because i use an european date input, instead of an US input. I've tried to format the dates, as seen in the code below.
Dim TODAY as string, TimeReg as string, UserReg as string, Init as string, _
SendFrom as string, SendTo as string, DateFrom as Date, DateTo as date, _
Comment as string
'Me.txtDateF.value Input f.ex. = 01-01-2020
'Me.txtDateT.value Input f.ex. = 16-01-2020
'Values'
TODAY = Me.txtDD.Value
TimeReg = Me.txtTime.value
UserReg = Left(Me.txtUser.value, 3)
Init = Me.cmbInit.value
If IsNull(Me.txtFrom.value) Then
SendFrom = ""
Else
SendFrom = Me.txtFra.value
End if
SendTo = Me.cmbTo.Column(1)
DateFrom = CDate(Format(Me.txtDateF.value, "dd-mm-yyyy"))
DateTo = CDate(Format(Me.txtDateT.value, "dd-mm-yyyy"))
Comment = Me.txtComment.value
'Define team on which it will use in SQL-Query
Select Case Me.txtTeam.value
Case "Team 1"
Team = "Team1"
TeamCom = "Team1Com"
Case "Team 2" etc...
End select
DoCmd.SetWarnings False
Do While DateFrom < DateTo
strSQL = "UPDATE [" & Team & "] SET [" & Init & "] = '" & SendTo & "' WHERE Date = #" & DateFrom "#"
DoCmd.RunSQL strSQL
ChangeSQL = "INSERT INTO tblChanges " _
& "(Date, Time, InitChange, TimeR, ShiftB, ShiftA, DateR, DateT, Comment)" _
& "VALUES (#" & TODAY & "#, #" & TimeReg & "#, '" & Init & "', '" & UserReg & "', '" & SendFrom & "', '" & SendTo & "', #" & DateFrom "# '" & Comment & "');"
DoCmd.RunSQL ChangeSQL
DateFrom = DateAdd("d", 1, DateFrom)
Loop
DoCmd.SetWarnings True
If the Input-dates is from f.ex. 01-01-2020 to 24-01-2020, the following dates change.
- 01-01-2020
- 13-01-2020
- 14-01-2020
- 15-01-2020
- 16-01-2020
- 17-01-2020
- 18-01-2020
- 19-01-2020
- 20-01-2020
- 21-01-2020
- 22-01-2020
- 23-01-2020
- 24-01-2020
- 01-02-2020
- 01-03-2020
It's missing the dates between 01-01-2020 to 12-01-2020. It seems to me like the format is off.