Hello! I borrowed this coding from accessmonster.com and it looks good, however when I try to compile it I get a "Compile Error: User-defined type not defined," and it stops at the first line of code on the Private Declare. I am not familiar with this error, but I imagine it is calling a kernel function that I don't have, so I went to tools, references, and added the Microsoft Office 14.0 Object Library, but that didn't do it. I am in Access 2010. Your help is much appreciated!
Here's the code:
Option Explicit
Private Declare Function SystemTimeToTzSpecificLocalTime Lib "kernel32" _
(lpTimeZoneInformation As TimeZoneInfo, lpUniversalTime As SystemTime, lpLocalTime As SystemTime) As Long
Public Function fConvertUTCtoLocalTime(pUTC As Date) As Date
On Error GoTo Err_fConvertUTCtoLocalTime
Dim lngRet As Long
Dim udtTZI As TimeZoneInfo
Dim stUTC As SystemTime
Dim stLocal As SystemTime
'Get TimeZone information
lngRet = GetTimeZoneInformation(udtTZI)
stUTC.intYear = Year(pUTC)
stUTC.intMonth = Month(pUTC)
stUTC.intDay = Day(pUTC)
stUTC.intHour = Hour(pUTC)
stUTC.intMinute = Minute(pUTC)
stUTC.intSecond = Second(pUTC)
stUTC.intMilliseconds = 0
'Converts a time in Coordinated Universal Time (UTC) to a specified time zone 's corresponding local time.
'It takes into account whether the time to be converted falls in daylight saving time or not
lngRet = SystemTimeToTzSpecificLocalTime(udtTZI, stUTC, stLocal)
'Return Date type
fConvertUTCtoLocalTime = DateSerial(stLocal.intYear, stLocal.intMonth, stLocal.intDay) + TimeSerial(stLocal.intHour, stLocal.intMinute, stLocal.intSecond)
Exit_fConvertUTCtoLocalTime:
Exit Function
Err_fConvertUTCtoLocalTime:
MsgBox Err.Description
Resume Exit_fConvertUTCtoLocalTime
End Function
Here's the code:
Option Explicit
Private Declare Function SystemTimeToTzSpecificLocalTime Lib "kernel32" _
(lpTimeZoneInformation As TimeZoneInfo, lpUniversalTime As SystemTime, lpLocalTime As SystemTime) As Long
Public Function fConvertUTCtoLocalTime(pUTC As Date) As Date
On Error GoTo Err_fConvertUTCtoLocalTime
Dim lngRet As Long
Dim udtTZI As TimeZoneInfo
Dim stUTC As SystemTime
Dim stLocal As SystemTime
'Get TimeZone information
lngRet = GetTimeZoneInformation(udtTZI)
stUTC.intYear = Year(pUTC)
stUTC.intMonth = Month(pUTC)
stUTC.intDay = Day(pUTC)
stUTC.intHour = Hour(pUTC)
stUTC.intMinute = Minute(pUTC)
stUTC.intSecond = Second(pUTC)
stUTC.intMilliseconds = 0
'Converts a time in Coordinated Universal Time (UTC) to a specified time zone 's corresponding local time.
'It takes into account whether the time to be converted falls in daylight saving time or not
lngRet = SystemTimeToTzSpecificLocalTime(udtTZI, stUTC, stLocal)
'Return Date type
fConvertUTCtoLocalTime = DateSerial(stLocal.intYear, stLocal.intMonth, stLocal.intDay) + TimeSerial(stLocal.intHour, stLocal.intMinute, stLocal.intSecond)
Exit_fConvertUTCtoLocalTime:
Exit Function
Err_fConvertUTCtoLocalTime:
MsgBox Err.Description
Resume Exit_fConvertUTCtoLocalTime
End Function