How to make a Variant of type VT_Date

Started by Chris Cullen, April 28, 2005, 01:39:00 PM

Previous topic - Next topic

Chris Cullen

Hi

I have a bit of code that makes a variant date, well nearly!

Sub varMakeDateTime( vVar As Variant, nD As Long, nM As Long, nY As Long, nH As Long, nMin As Long, nS As Long)
Local tSysTime As SYSTEMTIME
Local dVar As Double

tSysTime.wDay = nD
tSysTime.wMonth   = nM
tSysTime.wYear   = nY
tSysTime.wHour = nH
tSysTime.wMinute = nMin
tSysTime.wSecond = nS
tSysTime.wMilliseconds = 0

' Now create a variant from the SYSTEMTIME

If IsFalse SystemTimeToVariantTime( tSysTime, dVar ) Then
Let vVar = EMPTY
Else
Let vVar = dVar
End If

End Sub


My question is, how can I set the actual type of vVar to VT_Date.  If you check it out with Variant_VT it's a R8 (double).  I know that if I use this to update an ADO date field, all is fine, its just that I have functions which check on the variant type and it would be better if the variant was seen as a date rather than a double.

Jose Roca


SUB FF_MakeDateVariant (BYVAL dbDate AS DOUBLE, BYREF vDate AS VARIANT)
  LOCAL lpvObj AS VARIANTAPI PTR  ' Pointer to a VARIANTAPI structure
  LET vDate = EMPTY               ' Make sure it's empty
  lpvObj = VARPTR(vDate)          ' Get the VARIANT address
  @lpvObj.vt = %VT_DATE           ' Mark it as VT_DATE
  @lpvObj.vd.date = dbDate        ' Set the date value
END SUB

Chris Cullen

Thanks once again Jose !

Perfect!

By the way, do you know what the pb declare is for the VariantChangeType API?  That would seem to be another way to convery variant data between types, it just is not declared in any of the PB headers :roll:

Jose Roca

Here are my translation of the Variant APi functions:


' **********************************************************************
' VARIANT API FUNCTIONS
' Note: Using AS ANY disables type checking, so you can pass
' variants declared with PB and variants declared as VARIANTAPI.
' Example:
' DIM v1 as VARIANTAPI, v2 AS VARIANT
' VariantInit v1
' v2 = 12345 AS DWORD
' VariantCopy v1, v2
' **********************************************************************
%VARIANT_NOVALUEPROP    = &H1
%VARIANT_ALPHABOOL      = &H2
%VARIANT_NOUSEROVERRIDE = &H4
%VARIANT_LOCALBOOL      = &H10

DECLARE SUB      VariantInit LIB "OLEAUT32.DLL" ALIAS "VariantInit" (BYREF pvarg AS ANY)
DECLARE FUNCTION VariantCopy LIB "OLEAUT32.DLL" ALIAS "VariantCopy" (BYREF pvargDest AS ANY, BYREF pvargSrc AS ANY) AS LONG
DECLARE FUNCTION VariantCopyInd LIB "OLEAUT32.DLL" ALIAS "VariantCopyInd" (BYREF pvarg AS ANY, BYREF pvargSrc AS ANY) AS LONG
DECLARE FUNCTION VariantClear LIB "OLEAUT32.DLL" ALIAS "VariantClear" (BYREF pvarg AS ANY) AS LONG
DECLARE FUNCTION VariantChangeType LIB "OLEAUT32.DLL" ALIAS "VariantChangeType" (BYREF pvargDest AS ANY, BYREF pvargSrc AS ANY, BYVAL wFlags AS WORD, BYVAL vt AS WORD) AS LONG
DECLARE FUNCTION VariantChangeTypeEx LIB "OLEAUT32.DLL" ALIAS "VariantChangeTypeEx" (BYREF pvargDest AS ANY, BYREF pvargSrc AS ANY, BYVAL wFlags AS WORD, BYVAL vt AS WORD) AS LONG
' **********************************************************************