11.07.2015 Views

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>101</strong> TECH TIPSFor <strong>Visual</strong> <strong>Studio</strong> DevelopersVB4/32, VB5, VB6Level: Advanced✰✰✰✰✰ Five Star TipOverride Built-In KeywordsYou can override some of the built-in VB keywords with your ownversion of the function. For instance, FileDateTime is a handy builtinfunction in VB, but it suffers from one big problem: It can’t setthe date/time of a file. By overriding the built-in function, however,you can provide this feature. With this approach, the function candetermine for itself how it is being used and perform accordingly.You can override a number of keywords and functions in thismanner:Private Declare Function SystemTimeToFileTime Lib _"kernel32" (lpSystemTime As SYSTEMTIME, _lpFileTime As FILETIME) As LongPrivate Declare Function LocalFileTimeToFileTime _Lib "kernel32" (lpLocalFileTime As FILETIME, _lpFileTime As FILETIME) As LongPrivate Declare Function CreateFile Lib "kernel32" _Alias "CreateFileA" (ByVal lpFileName As _String, ByVal dwDesiredAccess As Long, ByVal _dwShareMode As Long, lpSecurityAttributes As _Any, ByVal dwCreationDisposition As Long, _ByVal dwFlagsAndAttributes As Long, _ByVal hTemplateFile As Long) As LongPrivate Declare Function SetFileTime Lib "kernel32" _(ByVal hFile As Long, lpCreationTime As Any, _lpLastAccessTime As Any, lpLastWriteTime As _Any) As LongPrivate Declare Function CloseHandle Lib "kernel32" _(ByVal hObject As Long) As LongPrivate Type FILETIMEdwLowDateTime As LongdwHighDateTime As LongEnd TypePrivate Type SYSTEMTIMEwYear As IntegerwMonth As IntegerwDayOfWeek As IntegerwDay As IntegerwHour As IntegerwMinute As IntegerwSecond As IntegerwMilliseconds As IntegerEnd TypePrivate Const GENERIC_WRITE As Long = &H40000000Private Const FILE_SHARE_READ As Long = &H1Private Const FILE_SHARE_WRITE As Long = &H2Private Const OPEN_EXISTING As Long = 3Public Function FileDateTime(ByVal FileName As String, _Optional ByVal TimeStamp As Variant) As Date' Raises an error if one occurs just like FileDateTimeDim x As LongDim Handle As LongDim System_Time As SYSTEMTIMEDim File_Time As FILETIMEDim Local_Time As FILETIMEIf IsMissing(TimeStamp) Then'It's missing so they must want to GET the timestamp'This acts EXACTLY like the original built-in functionFileDateTime = VBA.FileDateTime(FileName)ElseIf VarType(TimeStamp) vbDate Then'You must pass in a date to be validErr.Raise 450ElseSystem_Time.wYear = Year(TimeStamp)System_Time.wMonth = Month(TimeStamp)System_Time.wDay = Day(TimeStamp)System_Time.wDayOfWeek = _Weekday(TimeStamp) - 1System_Time.wHour = Hour(TimeStamp)System_Time.wMinute = Minute(TimeStamp)System_Time.wSecond = Second(TimeStamp)System_Time.wMilliseconds = 0'Convert the system time to a file timex = SystemTimeToFileTime(System_Time, Local_Time)'Convert local file time to file time based on UTCx = LocalFileTimeToFileTime(Local_Time, File_Time)'Open the file so we can get a file handle to'the fileHandle = CreateFile(FileName, GENERIC_WRITE, _FILE_SHARE_READ Or FILE_SHARE_WRITE, _ByVal 0&, OPEN_EXISTING, 0, 0)If Handle = 0 ThenErr.Raise 53, "FileDateTime", _"Can't open the file"Else'Now change the file time and date stampx = SetFileTime(Handle, ByVal 0&, _ByVal 0&, File_Time)If x = 0 Then'Error occuredErr.Raise 1, "FileDateTime", _"Unable to set file timestamp"End IfCall CloseHandle(Handle)'Return newly set date/timeFileDateTime = VBA.FileDateTime(FileName)End IfEnd IfEnd Function—Darin Higgins, Fort Worth, TexasVB4/32, VB5, VB6, SQL Server 6.5 and up, Oracle 8i and upLevel: BeginningCompare Oracle and SQL Server DatesOracle and SQL Server databases use different date/time resolutions,which poses a problem when you compare times from the twodatabases: The times will rarely be equal. Solve this problem byallowing for a margin of error. Treat the dates and times as floatingpointnumbers and remember that each day is equal to the wholenumber 1, and there are 86,400 seconds in a day. This functionmatches times within five seconds (default) of one another:Public Function MatchTime(adoFldOracle As ADODB.Field, _adoFldSQLServer As ADODB.Field, _Optional ByVal Tolerance As Long = 5) As BooleanDim dtOracle As DateDim dtSQLServer As DateDim dblTolerance As DoubleConst <strong>One</strong>Second As Double = 1 / 86400dblTolerance = <strong>One</strong>Second * TolerancedtOracle = adoFldOracle.ValuedtSQLServer = adoFldSQLServer.ValueIf ((dtOracle > (dtSQLServer + dblTolerance)) Or _(dtOracle < (dtSQLServer - dblTolerance))) ThenMatchTime = FalseElseMatchTime = TrueEnd IfEnd Function—Andy Clark, Richmond, Va.28 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> SEPTEMBER 2001

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!