Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Date/Time from Access DB. 1

Status
Not open for further replies.
Jan 20, 2005
180
US
I have a access db that Im trying to retrive items from. I also have some DB code that access's the db and so such. However I can not figure out how to get the date/time db type into a c type...

This is an example of the code Im using for other types

_variant_t vtValue;
vtValue = m_Rec->Fields->GetItem(FieldName)->GetValue();
FieldValue=vtValue.dblVal;

any help would be great.
 
I dont know if it will work or not, as I dont know what you are asking.
Im new to the vc++ db access...

From what I can tell the current way I have been accing the database will not work for date/time access fields. The best I can tell is in VC++ the datatype is DBDATE. However Im kinda lost on how to get that into a variable.

Im still learning.
 
Code:
FieldValue=vtValue.dblVal;

why are you using dblVal? if you need date/time, you should get the DATE date member of VARIANT:

Code:
FieldValue=vtValue.date;


------------------------
 
As I said that was example for 'other types', the dblVal is for retrieving double types, also have int, long and so forth, but the date, type doesnt seem to work for me either, so I must be doing something else wrong... Thanks for responding...
 
Hmm.. ok, what C Type do I use for FieldValue in this case?
 
You may use (or modify) my class for this case:
Code:
/// DB OLE VT_DATE data receiver (with sugar;).
class VDate
{
public:

  VDate(const _variant_t& v);
  VDate(int yy, int mon, int ddd);
  int	day()	const { return d; }
  int	month() const { return m; }
  int	year()	const { return y; }
  const char* strdmy() const;
  const char* strymd() const;
  bool isEmpty()	const { return dd == 0; }
  bool isNull()	const { return isEmpty(); }

  VDate& operator =(const _variant_t& v);

  bool operator ==(const VDate& vd) const
  { return dd == vd.dd; }
  bool operator < (const VDate& vd) const
  { return dd <  vd.dd;  }
  bool operator <=(const VDate& vd) const
  { return dd <= vd.dd; }
  bool operator !=(const VDate& vd) const
  { return dd != vd.dd; }
  bool operator > (const VDate& vd) const
  { return dd >  vd.dd;  }
  bool operator >=(const VDate& vd) const 
  { return dd >= vd.dd; }

private:
  mutable char str[20];
  bool cvtdos(double val);
  unsigned short dd, dt;
  int	d, m, y;
  int	h, mm, s; // not used...
};
// Implementation:
VDate& VDate::operator =(const _variant_t& v)
{
	if (v.vt == VT_DATE)
		cvtdos(v);
	else
	{
		dd = dt = 0;
		y = m = d = h = mm = s = 0;
	}
	memset(str,0,sizeof str);
	return *this;
}

VDate::VDate(const _variant_t& v):dd(0),dt(0),
		d(0), m(0), y(0), h(0), mm(0), s(0)
{
	if (v.vt == VT_DATE)
		cvtdos(v);
	memset(str,0,sizeof str);
}

VDate::VDate(int yy, int mon, int ddd):dd(0), dt(0),
		d(0), m(0), y(0), h(0), mm(0), s(0)
{
  if (yy > 0 && mon >= 1 && mon <= 12 && ddd >= 1 
  && ddd <= 31)
  {
     if (yy < 29)
     yy += 2000;
     y = yy;
     m = mon;
     d = ddd;
     dd = ddd | (m<<5) | ((yy-1980)<<9);
  }
  memset(str,0,sizeof str);
}

const char* VDate::strdmy() const
{
  if (dd)
  {
     if (!*str || str[2] != '.')
     {
	_snprintf(str,sizeof str,"%02d.%02d.%04d",d,m,y);
     }
  }
  return str;
}

const char* VDate::strymd() const
{
  if (dd)
  {
     if (!*str || str[2] != '.')
     {
	_snprintf(str,sizeof str,"%04d-%02d-%02d",y,m,d);
     }
  }
  return str;
}

bool VDate::cvtdos(double val)
{
   if (VariantTimeToDosDateTime(val,&dd,&dt))
   {
	d = dd & 0x1F;
	m = (dd>>5) & 0xF;
	y = (dd >> 9) + 1980;
	if (dt)
	{
           s  = (dt & 0x1F) << 1;
	   mm = (dt >> 5) & 0x3F;
	   h  = dt >> 11;
	}
	return true;
   }
   else
	dd = dt = 0;
   return false;
}
Don't forget to #import MSADO15.dll...
It's not an ideal, but it works.
 
Most excellent!!
Thank you very very much...

I can get something that works with that...
 
Just a few simple lines of code from your example and it works great. Thank you very much for the example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top