Applecore Pages on Microsoft Access

Getting the date/time that a file was created or last viewed

Although Access has a built-in function FileDateTime, this only returns the date and time that a file was last modified. If you wish to know the date that a file was created or last viewed you will need to use some API calls - below are three functions that will return the date/time a file was created, the date/time it was last modified, and the date/time it was last viewed:

Option Compare Database
Option Explicit

Private Declare Function apiGetFileTime Lib "kernel32" Alias "GetFileTime" _
    (ByVal hFile As Long, lpCreationTime As FILETIME, _
    lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function apiCreateFile Lib "kernel32" Alias "CreateFileA" _
    (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
    lpSecurityAttributes As Byte, ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function apiCloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long
Private Declare Function apiFileTimeToSystemTime Lib "kernel32" Alias "FileTimeToSystemTime" _
    (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
 
Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type
 
Private Const GENERIC_READ = &H80000000
Private Const FILE_SHARE_READ = &H1
Private Const OPEN_EXISTING = 3
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20

Function fFileCreateDate(strFile As String) As Date
'   Function to return the date & time that a file was created
'   Accepts:
'       strFile - a string containing the name of a file
'   Returns:
'       The date and time that the file was created

    Dim typCreateTime As FILETIME
    Dim typViewTime As FILETIME
    Dim typModifyTime As FILETIME
    Dim typSystemTime As SYSTEMTIME
    Dim lngHandle As Long
    Dim lngReturn As Long
    lngHandle = apiCreateFile(strFile, GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
    lngReturn = apiGetFileTime(lngHandle, typCreateTime, typViewTime, typModifyTime)
    lngReturn = apiCloseHandle(lngHandle)
    lngReturn = apiFileTimeToSystemTime(typCreateTime, typSystemTime)
    With typSystemTime
        fFileCreateDate = DateSerial(.wYear, .wMonth, .wDay) & " " & TimeSerial(.wHour, .wMinute, .wSecond)
    End With
End Function
 
Function fFileModifyDate(strFile As String) As Date
'   Function to return the date & time that a file was last modified
'   Accepts:
'       strFile - a string containing the name of a file
'   Returns:
'       The date and time that the file was last modified

    Dim typCreateTime As FILETIME
    Dim typViewTime As FILETIME
    Dim typModifyTime As FILETIME
    Dim typSystemTime As SYSTEMTIME
    Dim lngHandle As Long
    Dim lngReturn As Long
    lngHandle = apiCreateFile(strFile, GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
    lngReturn = apiGetFileTime(lngHandle, typCreateTime, typViewTime, typModifyTime)
    lngReturn = apiCloseHandle(lngHandle)
    lngReturn = apiFileTimeToSystemTime(typModifyTime, typSystemTime)
    With typSystemTime
        fFileModifyDate = DateSerial(.wYear, .wMonth, .wDay) & " " & TimeSerial(.wHour, .wMinute, .wSecond)
    End With
End Function
 
Function fFileViewDate(strFile As String) As Date
'   Function to return the date & time that a file was last viewed
'   Accepts:
'       strFile - a string containing the name of a file
'   Returns:
'       The date and time that the file was last viewed

    Dim typCreateTime As FILETIME
    Dim typViewTime As FILETIME
    Dim typModifyTime As FILETIME
    Dim typSystemTime As SYSTEMTIME
    Dim lngHandle As Long
    Dim lngReturn As Long
    lngHandle = apiCreateFile(strFile, GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
    lngReturn = apiGetFileTime(lngHandle, typCreateTime, typViewTime, typModifyTime)
    lngReturn = apiCloseHandle(lngHandle)
    lngReturn = apiFileTimeToSystemTime(typViewTime, typSystemTime)
    With typSystemTime
    fFileViewDate = DateSerial(.wYear, .wMonth, .wDay) & " " & TimeSerial(.wHour, .wMinute, .wSecond)
    End With
End Function

Top

 


HOME | NEW | TABLES | QUERIES | FORMS | REPORTS | GENERAL | API | DOWNLOADS | TUTORIAL | RESOURCES
E-MAIL
Copyright & Disclaimer

 

Last modified at 06/06/2006 14:53:14