home *** CD-ROM | disk | FTP | other *** search
- #ifndef _EMPLOYEE_DDX_CPP
- #define _EMPLOYEE_DDX_CPP
-
-
- #include <stdio.h>
-
-
- //Add a data exchange for the date field
- //See Tech Note 026,and articles Q114961 and Q110719 for more information
- //This is a global function
- void DDX_FieldText(
- CDataExchange * pDX, UINT nID,
- TIMESTAMP_STRUCT & date, CRecordset * pSet )
- {
- //Saving, we need to convert from string to date
-
- if ( pDX->m_bSaveAndValidate )
- {
- char caDate[120];
- pDX->PrepareEditCtrl( nID );
- pDX->m_pDlgWnd->GetDlgItem( nID )->
- GetWindowText( caDate, sizeof caDate );
- int nMonth, nDay, nYear, nFields;
- nMonth = nDay = nYear = 0;
- nFields = sscanf( caDate, "%d/%d/%d",
- & nMonth, & nDay, & nYear );
- if ( 3 == nFields ) //Did we get three values?
- {
- if ( nYear < 100 )
- nYear += 1900;
- date.year = nYear;
- date.month = nMonth;
- date.day = nDay;
- date.hour = 0;
- date.minute = 0;
- date.second = 0;
- date.fraction = 0;
- return;
- }
- AfxMessageBox( "Use date format:\nm/d/yyyy",
- MB_OK | MB_ICONEXCLAMATION );
- pDX->Fail( ); //Put focus back to field
- }
- else
- {
- CString strDate;
- if ( 12 < date.month && 31 < date.day ) //Uninitialized date
- strDate = "";
- else
- strDate.Format( "%u/%u/%u",
- date.month, date.day, date.year );
- pDX->m_pDlgWnd->GetDlgItem( nID )->
- SetWindowText( strDate );
- }
- }
-
- void DDV_Date( CDataExchange * pDX, TIMESTAMP_STRUCT & date, CRecordset *
- pSet )
- {
- if ( ! pDX->m_bSaveAndValidate )
- return;
- CString str1, str2, msg;
- int nMaxDays = 30; //Usually they all have 30 days except Feb
- int month = date.month;
-
- nMaxDays += month < 8 ? //Odd months are longer until July
- month & 1 : ! ( month & 1 );
- if ( 2 == month ) //February
- {
- int nLeapYear =
- 0 == date.year % 4; //Leap years always evenly divisible by 4
- if ( nLeapYear )
- nLeapYear =
- 0 == date.year % 100 //If the year is even div by 100
- ?
- 0 == date.year % 400 //Then it must also be even div by 400
- :
- 1;
-
- nMaxDays = 28 + nLeapYear; //Add a day for leap years
- }
- if ( month < 1 || month > 12 )
- {
- str1 = "Month must be between 1 and 12\n";
- nMaxDays = 31; //Not more than 31 days in an invalid month
- }
- if ( date.day < 1 || date.day > nMaxDays )
- str2.Format( "\nDay must be between 1 and %d", nMaxDays );
- msg = str1 + str2;
- if ( msg.IsEmpty( ) )
- return; //No problems noted
- AfxMessageBox(msg, MB_OK | MB_ICONEXCLAMATION );
- pDX->Fail( );
- }
-
-
-
-
- #endif //Already included