`
Windows API: Passing Visual Basic DATE to COM objects 2006-12-16 -- Jim Kane >I need to send dates to a COM object that uses the VB DATEs. Does anyone >know how to pass a value that populates as a DATE in VB. > > MSDN suggests using a DOUBLE* but these folks are locked into the 8 byte > VB DATE type. I can populate the Clarion DATE, Longs, SQL Server > DateTimes, etc just fine. I have tried formatted strings, starting and > ending my date with #, passing as a DATETIME, as a Clarion Date, as a > formatted LONG (D1, D2, D10, D17, same with dash at the end D10-) and > nothing makes it through. > > I can find no reference to this in the Help or on the NG. (maybe I am > searching in the wrong places) Dates or date times are passed as either a Real or as a variant In VB the datetime is the number of days since a 'base date' and the fractional part is the time as a fraction of a day. In other words: vbdatediff Equate(36161) ticksperday Equate(8640000) VT_DATE Equate(7) RealValue Real CWDate Long CWTime Long To covert a clarion date to vb date ( no time ): RealValue = CWDate-vbDateDiff To add a time: RealValue += ( CWTime -1 )/ TicksPerDay To stuff it into a variant - you MUST use ole view to determine if a variant or double ( real in clarion ) is expected. variantreal Group vt ushort r1 ushort r2 ushort r3 ushort realvalue real End clear( VariantReal ) !or variantinit() VariantReal.VT = VT_date VariantReal.RealValue = RealValue Now pass the variant either by value ( as 4 long parameters ) or by address - again OleView tells you which. You can't do com without oleview. I would caution you not to pass a date as a string without a lot of testing to be sure the com object and you are using the same month-day order. Some com objects change their settings for month-day based on the control pannel settings, others are hard coded, and still others depend on the lcid parameter passed if you are using IDispatch. I once tried to pass dates by strings but I had this one customer for whom the code would fail. Long story short the customer had a relative in Mexico so they changed the control pannel setting to make IM and other stuff work better and then forget to change it back. When my code ran while they were set to mexico they'd get invalid date messages if it was after the 12th of the month. The best way to control this and successfully pass a date as a string and always have it work no matter what the users regional settings are is to use VariantChangeTypeEX and pass a LCID parameter. See MSDN for that API if to see how hit handles dates. Also be advised VariantChangeTypeEx is not available on all OS - I think it's win95 it's not available on. Personally I dont think it's worth the trouble to use the string method. Jim Kane Printed November 23, 2024, 2:12 am This article has been viewed/printed 35220 times. Google search has resulted in 47 hits on this article since January 25, 2004. |