DateTime with ODBC

Here is my solution for a (little) problem using ODBC and date & time (for VC++ 4.0 & 5.0).

If using ODBC & date/time fields in a typical CRecordView, ClassWizard automatically binds a CTime object to the specific recordset. This object has few limits, but the most important is: there is no way to attach an Edit control in a CRecordView to that field.

With this code, it is possible to bind a date/time field to an Edit control using all MFC utilities, just like string parsing, data validation, data automatic set/get and so on.

To implement this function you have to do following steps:

1. Add date.h & date.cpp to your project

2. #include "date.h" in your CMyRecordView.cpp

3. Modify CMyRecordView::DoDataExchange( CDataExchange* pDX) as following


void CMyRecordView::DoDataExchange(CDataExchange* pDX) 
{ 
 CRecordView::DoDataExchange(pDX); 
 //{{AFX_DATA_MAP(CEsamiObiettiviView) 
 DDX_FieldText(pDX, IDC_EDT_NOTE, m_pSet->m_Note, m_pSet); 
 DDV_MaxChars(pDX, m_pSet->m_Note, 256); 
 ... 
 ... 
 //}}AFX_DATA_MAP 
 DDX_FieldDate( pDX, IDC_EDT_DATE, m_pSet->m_Date, m_pSet ); 
}

4. Modify CMyRecordSet as follows:

CTime m_Date becomes TIMESTAMP_STRUCT m_Date

5. Find CMyOwnApp::InitInstance() and add setlocale( LC_ALL, ".OCP" ) This function helps you to use the ControlPanel setup for date & time representations (don't forget to #include "locale.h").

6. That's all!

Reading the date.cpp it is possible to find three important functions:


DDX_FieldDate(...) 
GetDate(...) 
SetDate(...) 
This is an easy way to create your own bind method with a particular control & table fields: DDX_FieldsXYZ executes the controls; GetXYZ retrieves data from your control (don't forget to create a validation routine) and SetXYZ displays data in the format you prefer.

Following source contains two functions DDX_FieldDate for date only, and DDX_FieldDateTime for date and time too.

I test it only on Access DB via ODBC, could someone tests this code with SQLServer or other DBServer (via ODBC too)?

Download source - 3 KB

Date Last Updated: April 3, 1999



Comments

  • this also works with sql server

    Posted by sharma_shlabh on 05/23/2005 05:41am

    this is a very good code works well with sql server

    Reply
  • linking Excel Driver to Visual C++

    Posted by Legacy on 06/16/2003 12:00am

    Originally posted by: Steve

    My Problem is, i have an excel driver set up in odbc, but when i try and link it to my Visual C++ (MFC) project through the setup steps i cant add a table like when using Access, does anybody know the base class or what i have to do to set up, (CRecordSet doesnt work, or i cant figure out how to use it with .xls!!!) im trying to read and write to the database!!

    Thanks in advance,
    Steve:o)

    Reply
  • how define time field in DAO and how write date of system on that field

    Posted by Legacy on 03/02/2003 12:00am

    Originally posted by: ASHKAN

    Dear sir,
    I create a *.mdb file with DAO but I can not write date of system in one field of database record. In other word I dont know how define time field in DAO and how write date of system on that field.Please help me with a source file
    thanks in advance,
    sincerely yours,

    Reply
  • how define time field in DAO and how write date of system on that field

    Posted by Legacy on 03/02/2003 12:00am

    Originally posted by: ASHKAN

    Dear sir,
    I create a *.mdb file with DAO but I can not write date of system in one field of database record. In other word I dont know how define time field in DAO and how write date of system on that field.Please help me with a source file
    thanks in advance,
    sincerely yours,

    Reply
  • how define time field in DAO and how write date of system on that field

    Posted by Legacy on 03/02/2003 12:00am

    Originally posted by: ASHKAN

    Dear sir,
    I create a *.mdb file with DAO but I can not write date of system in one field of database record. In other word I dont know how define time field in DAO and how write date of system on that field.Please help me with a source file
    thanks in advance,
    sincerely yours,

    Reply
  • what is TIMESTAMP_STRUCT?error occur!

    Posted by Legacy on 08/15/2002 12:00am

    Originally posted by: blue

    I do as the step you said,but many errors occur because
    the TIMESTAMP_STRUCT structure ?what is TIMESTAMP_STRUCT?
    May I include any head file(s)?thank you!

    Reply
  • Invert lines for empty date

    Posted by Legacy on 07/12/2002 12:00am

    Originally posted by: Emilie

    Thanks a lot for this. But I've noticed a bug when the date
    
    stored in the database is empty.

    To solve this problem, I've inverted two lines in the date.cpp file, in order to print an empty string in the field :

    void SetDate(HWND hWnd, TIMESTAMP_STRUCT ts)
    {
    COleDateTime date( ts.year, ts.month, ts.day, 0, 0, 0);
    CString sBuffer;

    >>> // empty string if data is invalid
    >>> if( date.GetStatus() == COleDateTime::invalid )
    >>> sBuffer = "";
    >>> else
    >>> // don't forget setlocale(...) in your code
    >>> sBuffer = date.Format("%x");
    ::SetWindowText(hWnd, sBuffer.GetBufferSetLength(sBuffer.GetLength()));
    }

    The previous version of this file put the verification of
    the validity of the date after the traitment for this case, and that caused the bug.

    Hope this helps..

    Emilie

    Reply
  • Great!!

    Posted by Legacy on 03/07/2002 12:00am

    Originally posted by: Bart

    Thnx a lot! Just what I needed, works fine....

    Reply
  • C++ Takes it up the ass

    Posted by Legacy on 11/01/2001 12:00am

    Originally posted by: Cameron

    Just wanted to say that programming takes it up the ass

    Reply
  • DTX - Database Toolbox For MFC 1.8

    Posted by Legacy on 08/18/2001 12:00am

    Originally posted by: anonymous

    DTX - Database Toolbox For MFC 1.8 is a good complete database application framework. Use DAO and ADO. Including DTXDAoTable, DTXADOTable, DBEdit, DBNavigator, DBGrid, DBImage, Calculator, Calculator edit, DBRichEdit, DBListCtrl, etc. ~70+ ready to use class. Try this now including Release version
    homepage:
    http://server33.hypermart.net/celibol
    DTX homePage:
    http://server33.hypermart.net/celibol/dtx.html
    Download link
    http://server33.hypermart.net/celibol/dtx.zip

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds