📄 ch14.htm
字号:
{
case 1:
DMod->NamesTable->Insert();
break;
case 2:
DMod->AddressTable->Insert();
break;
case 3:
DMod->PhoneTable->Insert();
break;
case 4:
DMod->EMailTable->Insert();
break;
}
}
void __fastcall TForm1::Cancel1Click(TObject *Sender)
{
AnsiString S;
switch (dynamic_cast<TComponent&>(*PageControl1->ActivePage).Tag)
{
case 1:
DMod->NamesTable->Cancel();
break;
case 2:
DMod->AddressTable->Cancel();
break;
case 3:
DMod->PhoneTable->Cancel();
break;
case 4:
DMod->EMailTable->Cancel();
break;
default:
ShowMessage(GetError(1, S));
}
}
void __fastcall TForm1::Delete1Click(TObject *Sender)
{
AnsiString S;
AnsiString Msg("Are you sure you want to delete \r %s?");
Set<TMsgDlgBtn, 0, 8> Btns;
Btns << mbYes << mbNo;
switch
(dynamic_cast<TComponent&>(*PageControl1->ActivePage).Tag)
{
case 1:
Msg = Format(Msg, OPENARRAY(TVarRec,
(DMod->NamesTableFirstLastCompany->AsString)));
if (MessageDlg(Msg, mtInformation, Btns, 0) ==
ID_YES)
DMod->CascadingDelete();
break;
case 2:
Msg = Format(Msg, OPENARRAY(TVarRec, (DMod->Address)));
if (MessageDlg(Msg, mtInformation, Btns, 0) == ID_YES)
DMod->AddressTable->Delete();
break;
case 3:
Msg = Format(Msg, OPENARRAY(TVarRec, (DMod->Phone)));
if (MessageDlg(Msg, mtInformation, Btns, 0) == ID_YES)
DMod->PhoneTable->Delete();
break;
case 4:
Msg = Format(Msg,
OPENARRAY(TVarRec, (DMod->EMail)));
if (MessageDlg(Msg, mtInformation, Btns, 0) == ID_YES)
DMod->EMailTable->Delete();
break;
default:
ShowMessage(GetError(1, S));
}
}
void __fastcall
TForm1::SearchClick(TObject *Sender)
{
AnsiString S, IndexName;
if (InputQuery("Search for Name", "Enter Name: ", S))
{
IndexName = DMod->NamesTable->IndexName;
SetupIndex(Sender);
DMod->NamesTable->FindNearest(OPENARRAY(TVarRec, (S)));
DMod->NamesTable->IndexName = IndexName;
}
}
void __fastcall TForm1::About1Click(TObject *Sender)
{
AboutBox->ShowModal();
}
void __fastcall
TForm1::PostBtnClick(TObject *Sender)
{
DMod->PostAll();
}
</FONT></PRE>
<P><A NAME="Heading12"></A><FONT COLOR="#000077"><B>Listing 14.3. The header for
the data module.</B></FONT></P>
<PRE><FONT
COLOR="#0066FF">///////////////////////////////////////
// File: DMod1.h
// Project: KdAdd
// Copyright (c) 1997 by Charlie Calvert
//
#ifndef DMod1H
#define DMod1H
#include <Forms.hpp>
#include <DBTables.hpp>
#include
<DB.hpp>
#include <Classes.hpp>
class TDMod : public TDataModule
{
__published:
TTable *NamesTable;
TDataSource *dsNames;
TTable *AddressTable;
TTable *PhoneTable;
TTable *MemoTable;
TDataSource *dsAddress;
TDataSource *dsPhone;
TDataSource *dsMemo;
TAutoIncField *PhoneTablePhoneCode;
TStringField *PhoneTableDescription;
TStringField *PhoneTableNumber;
TStringField *PhoneTableExt;
TIntegerField *PhoneTableNameCode;
TTable
*EMailTable;
TDataSource *dsEmail;
TAutoIncField *EMailTableEMailCode;
TStringField *EMailTableAddress;
TStringField *EMailTableDescription;
TStringField *EMailTableService;
TIntegerField *EMailTableNameCode;
TQuery
*EMailDeleteQuery;
TQuery *MemoDeleteQuery;
TQuery *PhoneDeleteQuery;
TQuery *AddressDeleteQuery;
TQuery *NamesDeleteQuery;
TAutoIncField *MemoTableMemoCode;
TStringField *MemoTableDescription;
TMemoField *MemoTableMemoData;
TIntegerField *MemoTableNameCode;
TAutoIncField *NamesTableNameCode;
TStringField *NamesTableFirstName;
TStringField *NamesTableLastName;
TStringField *NamesTableCompany;
TAutoIncField *AddressTableAddCode;
TStringField
*AddressTableAddress1;
TStringField *AddressTableAddress2;
TStringField *AddressTableCity;
TStringField *AddressTableState;
TStringField *AddressTableZip;
TIntegerField *AddressTableNameCode;
TStringField *NamesTableFirstLastCompany;
void __fastcall NamesTableCalcFields(TDataSet *DataSet);
private:
AnsiString __fastcall GetAddress();
AnsiString __fastcall GetPhone();
AnsiString __fastcall GetEMail();
public:
virtual __fastcall TDMod(TComponent* Owner);
void
PostAll(void);
void CascadingDelete(void);
__property AnsiString Address={read=GetAddress};
__property AnsiString Phone={read=GetPhone};
__property AnsiString EMail={read=GetEMail};
};
extern TDMod *DMod;
</FONT></PRE>
<PRE><FONT
COLOR="#0066FF">#endif
</FONT></PRE>
<P><A NAME="Heading13"></A><FONT COLOR="#000077"><B>Listing 14.4. The code for the
data module.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// File: DMod1.cpp
// Project:
KdAdd
// Copyright (c) 1997 by Charlie Calvert
//
#include <vcl.h>
#pragma hdrstop
#include "DMod1.h"
#pragma resource "*.dfm"
TDMod *DMod;
__fastcall TDMod::TDMod(TComponent* Owner)
: TDataModule(Owner)
{
NamesTable->Open();
AddressTable->Open();
PhoneTable->Open();
MemoTable->Open();
EMailTable->Open();
}
void __fastcall TDMod::NamesTableCalcFields(TDataSet *DataSet)
{
AnsiString Temp = NamesTableFirstName->Value+
" " +NamesTableLastName->Value;
if (Temp == " ")
NamesTableFirstLastCompany->Value = NamesTableCompany->Value;
else
NamesTableFirstLastCompany->Value = Temp;
}
void DoPost(TDataSet *Data)
{
if
((Data->State == dsInsert) || (Data->State == dsEdit))
Data->Post();
}
void TDMod::PostAll(void)
{
int i;
for (i = 0; i < ComponentCount; i++)
if (dynamic_cast<TTable*>(Components[i]))
DoPost((TDataSet*)(Components[i]));
}
void TDMod::CascadingDelete(void)
{
EMailDeleteQuery->ParamByName("NameCode")->AsInteger =
EMailTableNameCode->Value;
EMailDeleteQuery->ExecSQL();
MemoDeleteQuery->ParamByName("NameCode")->AsInteger =
MemoTableNameCode->Value;
MemoDeleteQuery->ExecSQL();
PhoneDeleteQuery->ParamByName("NameCode")->AsInteger =
PhoneTableNameCode->Value;
PhoneDeleteQuery->ExecSQL();
AddressDeleteQuery->ParamByName("NameCode")->AsInteger =
AddressTableNameCode->Value;
AddressDeleteQuery->ExecSQL();
NamesDeleteQuery->ParamByName("NameCode")->AsInteger =
NamesTableNameCode->Value;
NamesDeleteQuery->ExecSQL();
NamesTable->Refresh();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -