Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

230

 

Project 1

DATABASE PROGRAMMING USING VC++.NET

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

AfxMessageBox(“Please enter your Address”);

 

 

 

 

((CEdit*)GetDlgItem(IDC_PADD))->SetFocus();

 

 

 

 

return;

 

 

 

}

 

 

 

 

//

 

 

 

 

 

 

 

CEdit *pAmnt;

 

 

 

 

 

pAmnt=(CEdit*)GetDlgItem(IDC_AMOUNT);

 

 

 

//Checking for the type of account and the min. amt for that account

 

 

 

CButton *pSaveing,*pCurrent,*pChecking;

 

 

 

 

 

 

Y

 

 

 

pSaveing=(CButton*)GetDlgItem(IDC RADIO1);

 

 

 

 

 

L

 

 

 

pCurrent=(CButton*)GetDlgItem(IDC RADIO2);

 

 

 

 

 

F

 

 

 

pChecking=(CButton*)GetDlgItem(IDC RADIO3);

 

 

 

 

 

M

 

 

 

 

if(pSaveing->GetCheck()==BST CHECKED)

 

 

{

A

 

 

 

 

 

m_pSet->m ype account=”S”;

 

 

 

 

 

E

 

 

 

 

 

if(m_pSet->m Balance<50)

 

 

 

 

 

T

 

 

{

AfxMessageBox(“Minimum Acceptable Amount is $50”); pAmnt->SetFocus();

return;

}

}

else if(pCurrent->GetCheck()==BST_CHECKED)

{

m_pSet->m_Type_account=”C”; if(m_pSet->m_Balance<100)

{

AfxMessageBox(“Minimum Acceptable Amount is $100”); pAmnt->SetFocus();

return;

}

}

else if (pChecking->GetCheck()==BST_CHECKED)

{

m_pSet->m_Type_account=”K”;

Team-Fly®

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

231

 

 

 

if(m_pSet->m_Balance<150)

{

AfxMessageBox(“Minimum Acceptable Amount is $150”); pAmnt->SetFocus();

return;

}

}

//Disabling and enabling controls

CComboBox* cmb=(CComboBox*)GetDlgItem(IDC_ACCNO);

CButton* pAdd, *pMod, *pDel, *pUpd, *pDep, *pTransfer, *pCancel; pAdd=(CButton*)GetDlgItem(IDC_SAVE);

pAdd->EnableWindow(true); pMod=(CButton*)GetDlgItem(IDC_MOD); pMod->EnableWindow(true); pUpd=(CButton*)GetDlgItem(IDC_UPD); pUpd->EnableWindow(false); pCancel=(CButton*)GetDlgItem(IDC_CANCEL1); pCancel->EnableWindow(false); pDel=(CButton*)GetDlgItem(IDC_DELETE); pDel->EnableWindow(true); pDep=(CButton*)GetDlgItem(IDC_DEPOSIT); pDep->EnableWindow(true); pTransfer=(CButton*)GetDlgItem(IDC_TRANSFER); pTransfer->EnableWindow(true);

//The code preceding this line is common to both Add and Modify operations. From //here, the code becomes specific to saving the new record.

//The variable ‘m_bAddNewAccount’ is set while the control entered the OnSave //method.

if (m_bAddNewAccount)

{

cmb->EnableWindow(true); pMod=(CButton*)GetDlgItem(IDC_MOD); pMod->EnableWindow(true);

int nNewAccountNum=0; if(!m_pSet->IsEOF())

232 Project 1 DATABASE PROGRAMMING USING VC++.NET

{

m_pSet->MoveLast();

nNewAccountNum = m_pSet->m_Account_Number + 1;

}

CString strNewAccountNum; strNewAccountNum.Format(“%d”, nNewAccountNum);

//Adding an empty record to the recordset and transferring data from the view to //set.

m_pSet->AddNew(); UpdateData(true);

m_pSet->m_Valid_Acc=”VA”;

CButton *pSaveing,*pCurrent,*pChecking; pSaveing=(CButton*)GetDlgItem(IDC_RADIO1); pCurrent=(CButton*)GetDlgItem(IDC_RADIO2); pChecking=(CButton*)GetDlgItem(IDC_RADIO3);

if(pSaveing->GetCheck()==BST_CHECKED) m_pSet->m_Type_account=”S”;

else if(pCurrent->GetCheck()==BST_CHECKED) m_pSet->m_Type_account=”C”;

else if (pChecking->GetCheck()==BST_CHECKED) m_pSet->m_Type_account=”K”;

//Updating the database – value transferred from the recordset to the database if(!m_pSet->Update())

AfxMessageBox(“Unable to Save....”); if(m_pSet->Requery()==0)

return;

cmb->AddString(strNewAccountNum); if(!m_pSet->IsBOF() && m_pSet->IsEOF())

m_pSet->MoveLast();

AfxMessageBox(“Account has been created”);

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

233

 

 

 

CButton *chk1; chk1=(CButton*)GetDlgItem(IDC_CHECK1); chk1->SetCheck(false);

CButton *bMod,*bDel; bMod=(CButton*)GetDlgItem(IDC_MOD); bMod->EnableWindow(true); bDel=(CButton*)GetDlgItem(IDC_DELETE); bDel->EnableWindow(true);

m_bAddNewAccount = FALSE;

}

//If the user clicked the Modify button before clicking on Update, then the //control is transferred to this part of the code.

else

{

if(!m_pSet->IsOpen()) m_pSet->Open();

if(m_pSet->IsBOF() && m_pSet->IsEOF ()) AfxMessageBox(“No Records in the Database...”);

while (!m_pSet->IsEOF())

{

if (m_pSet->m_Account_Number == lAcctNumToEdit) break;

m_pSet->MoveNext();

}

//recordset is opened in the edit mode and the values are transferred from the //controls to the variables.

m_pSet->Edit(); m_pSet->m_Valid_Acc=”VA”;

GetDlgItemText(IDC_MADD,m_pSet->m_Mailing_Addr);

GetDlgItemText(IDC_PADD,m_pSet->m_Permanent_Addr);

GetDlgItemText(IDC_EMAIL,m_pSet->m_eMailID);

GetDlgItemText(IDC_PHONE,m_pSet->m_Phone_Number);

CButton *pSaveing,*pCurrent,*pChecking;

234 Project 1 DATABASE PROGRAMMING USING VC++.NET

pSaveing=(CButton*)GetDlgItem(IDC_RADIO1); pCurrent=(CButton*)GetDlgItem(IDC_RADIO2); pChecking=(CButton*)GetDlgItem(IDC_RADIO3);

if(pSaveing->GetCheck()==BST_CHECKED) m_pSet->m_Type_account=”S”;

else if(pCurrent->GetCheck()==BST_CHECKED) m_pSet->m_Type_account=”C”;

else if (pChecking->GetCheck()==BST_CHECKED) m_pSet->m_Type_account=”K”;

//Then the database is updated and the view is refreshed. if(!m_pSet->Update())

AfxMessageBox(“Updation of Record Failed!!!”);

UpdateData(false);

if(m_pSet->Requery()==0) return;

if(!m_pSet->IsOpen()) m_pSet->Open();

while (!m_pSet->IsEOF())

{

if (m_pSet->m_Account_Number == lAcctNumToEdit) break;

m_pSet->MoveNext();

}

AfxMessageBox(“Record has been updated”);

//Then enable and disable the controls

CButton *chk1; chk1=(CButton*)GetDlgItem(IDC_CHECK1); chk1->SetCheck(false);

CEdit *pFName,*pLName,*pDOB,*pRefAcc,*pAmount;

pFName=(CEdit*)GetDlgItem(IDC_FNAME);

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

235

 

 

 

pFName->EnableWindow(true); pLName=(CEdit*)GetDlgItem(IDC_LNAME); pLName->EnableWindow(true); pDOB=(CEdit*)GetDlgItem(IDC_DOB); pDOB->EnableWindow(true);

pRefAcc=(CEdit*)GetDlgItem(IDC_REFACCNO); pRefAcc->EnableWindow(true);

pAmount=(CEdit*)GetDlgItem(IDC_AMOUNT); pAmount->EnableWindow(true);

CComboBox *pcmbAcc; pcmbAcc=(CComboBox*)GetDlgItem(IDC_ACCNO); pcmbAcc->EnableWindow(true);

}

m_bFromUpdate = TRUE;

OnInitialUpdate();

}

The VerifyDate Function

Following is the code for one of the validating functions, VerifyDate, which validates the date of birth:

BOOL CBankOperationView::VerifyDate(CString strDate)

{

BOOL bValidDate = FALSE;

CString strTemp1, strTemp2, strTemp3; for (int i=0; i<10; i++)

{

strTemp1 = strDate.Left(2);

if (atoi(strTemp1)> 0 && atoi(strTemp1)< 13) // 1 to 12

{

//Valid Month

}

else

{

236 Project 1 DATABASE PROGRAMMING USING VC++.NET

return FALSE;

}

strTemp2 = strDate.Right(4);

if (atoi(strTemp2)> 1700 && atoi(strTemp2)< 3000)

{

//Valid Year

}

else

{

return FALSE;

}

strTemp3 = strDate.Mid(3, 2);

if (atoi(strTemp3)> 0 && atoi(strTemp3)< 32)

{

//Valid Month

int iMonth = atoi(strTemp1); switch(iMonth)

{

case 1:

bValidDate = TRUE; break;

case 2:

{

int nRes = atoi(strTemp2)/4;

if ( nRes == 0 && (atoi(strTemp3)> 0 && atoi(strTemp3)< 29) )

bValidDate = TRUE;

if ( nRes != 0 && atoi(strTemp3)> 0 && atoi(strTemp3)< 30) ////Leap year

bValidDate = TRUE;

}

break; case 3:

bValidDate = TRUE; break;

case 4:

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

237

 

 

 

if (atoi(strTemp3)> 0 && atoi(strTemp3)< 31) bValidDate = TRUE;

break; case 5:

bValidDate = TRUE; break;

case 6:

if (atoi(strTemp3)> 0 && atoi(strTemp3)< 31) bValidDate = TRUE;

break; case 7:

bValidDate = TRUE; break;

case 8:

bValidDate = TRUE; break;

case 9:

if (atoi(strTemp3)> 0 && atoi(strTemp3)< 31) bValidDate = TRUE;

break; case 10:

bValidDate = TRUE; break;

case 11:

if (atoi(strTemp3)> 0 && atoi(strTemp3)< 31) bValidDate = TRUE;

break; case 12:

bValidDate = TRUE; break;

}

}

}

return bValidDate;

}

238 Project 1 DATABASE PROGRAMMING USING VC++.NET

The Cancel Button

You have now seen the functionality of the Add, Modify, and Submit buttons. Next, I will provide the coding for the Cancel click button. Add the event handler and the code as shown:

void CBankOperationView::OnBnClickedCancel1()

{

//Enabling and disabling controls

CComboBox *cmb=(CComboBox*)GetDlgItem(IDC_ACCNO); cmb->EnableWindow(TRUE);

CEdit *pFName, *pLName, *pDOB, *pRefAcct, *pAmnt; pFName=(CEdit*)GetDlgItem(IDC_FNAME); pFName->EnableWindow(TRUE);

pLName=(CEdit*)GetDlgItem(IDC_LNAME); pLName->EnableWindow(TRUE);

pDOB = (CEdit*)GetDlgItem(IDC_DOB); pDOB->EnableWindow(TRUE);

pRefAcct=(CEdit*)GetDlgItem(IDC_REFACCNO); pRefAcct->EnableWindow(TRUE);

pAmnt=(CEdit*)GetDlgItem(IDC_AMOUNT); pAmnt->EnableWindow(TRUE);

CButton *pDep, *pTransfer, *pCancel;

pCancel=(CButton*)GetDlgItem(IDC_CANCEL1); pCancel->EnableWindow(FALSE);

pDep=(CButton*)GetDlgItem(IDC_DEPOSIT); pDep->EnableWindow(true);

pTransfer=(CButton*)GetDlgItem(IDC_TRANSFER); pTransfer->EnableWindow(true);

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

239

 

 

 

//Setting the Cancel property to true m_bFromCancel = TRUE;

//To refresh the records

OnInitialUpdate();

}

When the user wants to edit a record, he or she has to select the corresponding account number from the combo box. When this operation is done, the SelChange event of the combo box is trapped.

The OnSelChangeAccno Method

In the SelChange event, the selected record number is searched for in the recordset and, if found, values are displayed in the view. Following is the code for the same:

void CBankOperationView::OnSelchangeAccno()

{

//Placing the recordset pointer in the first record m_pSet->MoveFirst();

//retrieving the current selection from the combo box

CComboBox *cmb=(CComboBox*)GetDlgItem(IDC_ACCNO); int nCurIndex=cmb->GetCurSel();

//Retrieve the account number corresponding to the selection

CString str; cmb->GetLBText(nCurIndex,str); int accNo=atoi(str);

//Compare the retrieved number with the recordset number; if found update the //view

while( m_pSet->m_Account_Number != accNo ) m_pSet->MoveNext();

UpdateData(false);

//Code to disable and enable controls based on whether or not the account is //operational