Can I ask a question if this isn't solved yet.
Code:
d = DateSerial(Year(Date), 4, 1)
w = Weekday(d, vbSunday)
FirstSunday1 = d + IIf(w, 8 - w, 0)
d = DateSerial(Year(Date), 10, 1)
w = Weekday(d, vbSunday)
FirstSunday2 = d + IIf(w, 8 - w, 0)
d = DateSerial(Year(DateAdd("yyyy", 1, Date)), 4, 1)
w = Weekday(d, vbSunday)
FirstSunday3 = d + IIf(w, 8 - w, 0)
when you use an IIF statement you should be comparing W to something to find out if it's true or not
Code:
d = DateSerial(Year(Date), 4, 1)
w = Weekday(d)
FirstSunday1 = DateAdd("d", (IIf(w = 1, 0, 8 - w)), d)
Second, you do not need, as far as I can tell, the firstsunday3 at all
Third, you have some logical inconsistencies in your case statement right now
fourth, you are not correctly figuring your firstsunday1 and firstsunday2, you are figuring the date but not the 2AM part to check against try this:
Code:
Dim intState As Integer
Dim cdatetime As Date
Dim TimeOffset As Integer
Dim strInterval As String
Dim FirstSunday1 As Date
Dim FirstSunday2 As Date
Dim FirstSunday3 As Date
Dim d As Date
Dim w As Integer
'set default time
'--------------------------------
TimeOffset = 0
strInterval = ""
'***********************
intState = StateID
'intState = 3
'***********************
d = DateSerial(Year(Date), 4, 1)
w = Weekday(d, vbSunday)
FirstSunday1 = DateAdd("h", 2, (DateAdd("d", (IIf(w = 1, 0, 8 - w)), d)))
d = DateSerial(Year(Date), 10, 1)
w = Weekday(d, vbSunday)
FirstSunday2 = DateAdd("h", 2, (DateAdd("d", (IIf(w = 1, 0, 8 - w)), d)))
cdatetime = Now()
Debug.Print "intState = " & intState
Debug.Print "cdatetime = " & cdatetime
Debug.Print "firstsunday1 = " & FirstSunday1
Debug.Print "firstsunday2 = " & FirstSunday2
Select Case intState
Case 3 'Northern Territory
If cdatetime < firstsunday Or cdatetime > FirstSunday2 Then
TimeOffset = -90
strInterval = "n"
Debug.Print "NORTHERN TERRITORY - NOT IN DAYLIGHT SAVINGS"
ElseIf cdatetime >= FirstSunday1 And cdatetime <= FirstSunday2 Then
TimeOffset = -30
strInterval = "n"
Debug.Print "NORTHERN TERRITORY - IN DAYLIGHT SAVINGS"
Else
Debug.Print "NORTHERN TERRITORY DEAD LAND"
End If
Case 4 'Queensland
If cdatetime < firstsunday Or cdatetime > FirstSunday2 Then
TimeOffset = -1
strInterval = "h"
Debug.Print "QUEENSLAND - NOT IN DAYLIGHT SAVINGS"
ElseIf cdatetime >= FirstSunday1 And cdatetime <= FirstSunday2 Then
TimeOffset = 0
strInterval = "n"
Debug.Print "QUEENSLAND - IN DAYLIGHT SAVINGS"
End If
Case 5 'South Australia
If cdatetime < firstsunday Or cdatetime > FirstSunday2 Then
TimeOffset = -30
strInterval = "n"
Debug.Print "SOUTH AUSTRALIA - NOT IN DAYLIGHT SAVINGS"
ElseIf cdatetime >= FirstSunday1 And cdatetime <= FirstSunday2 Then
TimeOffset = -30
strInterval = "n"
Debug.Print "SOUTH AUSTRALIA - IN DAYLIGHT SAVINGS"
End If
Case 8 'Western Australia
If cdatetime < firstsunday Or cdatetime > FirstSunday2 Then
TimeOffset = -3
strInterval = "h"
Debug.Print "WESTERN AUSTRALIA - NOT IN DAYLIGHT SAVINGS"
ElseIf cdatetime >= FirstSunday1 And cdatetime <= FirstSunday2 Then
TimeOffset = -2
strInterval = "h"
Debug.Print "WESTERN AUSTRALIA - IN DAYLIGHT SAVINGS"
End If
End Select
Debug.Print "TimeOffset = " & TimeOffset
Debug.Print "strInterval = " & strInterval
If strInterval <> "" Then
Debug.Print "Time Change Applied"
'Forms!frmSchools!box1.Value = "not applicable"
cdatetime = DateAdd(strInterval, TimeOffset, cdatetime)
Else
Debug.Print "No Time Change Applied"
End If
Debug.Print cdatetime