Q&A

  • 소스좀 봐주세요...필드관련
안냐세요

먼저 소스 좀 봐주세요...

아래에 보면 나이계산과 남녀구별하는 부분이 있습니다.

이 두부분을 변경하고 싶은데요.... 소스를 좀 도와주세요...



지금 Combobox를 이용하여 선택하면 DBGrid에 뿌려주는 형식입니다.

그런데 DB에는 Age필드와 Sex필드는 없어서 현디비 뒤어 추가로 만들었습니다.

나이와 성별을 계산후 자동으로 새로운 필드인 Sex나 Age필드에 각각의 레코드의 값을 넣어두고 싶습니다.

즉, 구한 값을 Age,Sex필드에 넣어서 그것을 Combobox에서 선택하면 되지 않을까 싶은데요....



소스좀 보시고, 쉽게 아주 쉽게 너무 쉬워서 초보라도 한 눈에 보고 이해할수 있을정도로 가르쳐 주시면 넘 감사하겠습니다.



unit Unit27;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

Grids, DBGrids, StdCtrls, ExtCtrls, Db, DBTables, Mask, DBCtrls, ComCtrls,

TeEngine, Series, TeeProcs, Chart, DBChart;



type

TForm27 = class(TForm)

Panel1: TPanel;

Label1: TLabel;

Button25: TButton;

Button26: TButton;

Button1: TButton;

DBNavigator1: TDBNavigator;

Button2: TButton;

Edit1: TEdit;

Panel2: TPanel;

Label3: TLabel;

Label5: TLabel;

Label6: TLabel;

Label7: TLabel;

Label8: TLabel;

Label9: TLabel;

Label10: TLabel;

Label11: TLabel;

Label12: TLabel;

Label13: TLabel;

Label14: TLabel;

DBEdit9: TDBEdit;

PageControl1: TPageControl;

TabSheet1: TTabSheet;

Label2: TLabel;

Label4: TLabel;

Edit2: TEdit;

Panel3: TPanel;

Label16: TLabel;

ComboBox1: TComboBox;

TabSheet2: TTabSheet;

Label15: TLabel;

Label17: TLabel;

Label18: TLabel;

Label19: TLabel;

Edit4: TEdit;

Edit5: TEdit;

TabSheet3: TTabSheet;

Label21: TLabel;

Label22: TLabel;

Panel4: TPanel;

Label20: TLabel;

Edit6: TEdit;

Edit7: TEdit;

TabSheet4: TTabSheet;

Label24: TLabel;

Label25: TLabel;

Panel5: TPanel;

Label23: TLabel;

Edit8: TEdit;

Edit9: TEdit;

TabSheet5: TTabSheet;

Label27: TLabel;

Label28: TLabel;

Panel6: TPanel;

Label26: TLabel;

Edit10: TEdit;

Edit11: TEdit;

DBGrid1: TDBGrid;

DataSource1: TDataSource;

DBEdit10: TDBEdit;

DBEdit11: TDBEdit;

DBEdit1: TDBEdit;

DBEdit2: TDBEdit;

DBEdit3: TDBEdit;

DBEdit4: TDBEdit;

DBEdit5: TDBEdit;

DBEdit6: TDBEdit;

DBEdit7: TDBEdit;

DBEdit8: TDBEdit;

Table1: TTable;

Edit3: TEdit;

Panel8: TPanel;

Label30: TLabel;

ComboBox3: TComboBox;

ComboBox4: TComboBox;

ComboBox5: TComboBox;

ComboBox6: TComboBox;

Table1StringField: TStringField;

Table1StringField2: TStringField;

Table1StringField3: TStringField;

Table1StringField4: TStringField;

Table1StringField5: TStringField;

Table1StringField6: TStringField;

Table1StringField7: TStringField;

Table1StringField8: TStringField;

Table1StringField9: TStringField;

Table1StringField10: TStringField;

Table1StringField11: TStringField;

Table1StringField12: TStringField;

Table1Sex: TStringField;

Edit12: TEdit;

Edit13: TEdit;

Button3: TButton;

Button4: TButton;

procedure Button25Click(Sender: TObject);

procedure Button26Click(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure ComboBox3Change(Sender: TObject);

procedure Table1AfterOpen(DataSet: TDataSet);

procedure ComboBox4Change(Sender: TObject);

procedure ComboBox6Change(Sender: TObject);

procedure ComboBox1Change(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure Button4Click(Sender: TObject);

procedure Table1BeforePost(DataSet: TDataSet);





private

{ Private declarations }

public

{ Public declarations }

end;



var

Form27: TForm27;



implementation



uses Unit4, Unit1;



{$R *.DFM}



procedure TForm27.Button25Click(Sender: TObject);

begin

form27.hide;

form4.show;

end;



procedure TForm27.Button26Click(Sender: TObject);

begin

form27.hide;

form1.show;

end;



procedure TForm27.Button1Click(Sender: TObject);

begin

if Table1.Locate('이름',Edit1.Text,[lopartialkey])

=False then

showMessage('검색한 인원의 찾을 수 없습니다');

end;



procedure TForm27.Button2Click(Sender: TObject);

begin

close;

end;







procedure TForm27.ComboBox3Change(Sender: TObject);

var

i,t:integer;

begin

Table1.Filtered :=false;

t:=table1.RecordCount ;

edit4.text:= IntTostr(Table1.RecordCount);

if t>0then begin

case combobox3.ItemIndex of

0 : begin

table1.Filtered := false;

edit4.text:= IntTostr(Table1.RecordCount);

edit5.text:='';

end;



1 : begin

table1.Filtered := false;

table1.Filter := '세대주 = ''세''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit4.text:=IntTostr(Table1.RecordCount);

edit5.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

end;

end;

end;







procedure TForm27.Table1AfterOpen(DataSet: TDataSet);

begin

Edit4.text:=IntTostr(Table1.RecordCount);

end;



procedure TForm27.ComboBox4Change(Sender: TObject);

var

i,t:integer;

begin

Table1.Filtered :=false;

t:=table1.RecordCount ;

edit6.text:= IntTostr(Table1.RecordCount);

if t>0then begin

case combobox4.ItemIndex of

0 : begin

table1.Filtered := false;

edit6.text:= IntTostr(Table1.RecordCount);

edit7.text:='';

end;



1 : begin

table1.Filtered := false;

table1.Filter := '직업 = ''상업''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit7.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

edit6.text:= IntTostr(Table1.RecordCount);

end;

2 : begin

table1.Filtered := false;

table1.Filter := '직업 = ''수산업''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit6.text:= IntTostr(Table1.RecordCount);

edit7.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

3 : begin

table1.Filtered := false;

table1.Filter := '직업 = ''서비스업''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit6.text:= IntTostr(Table1.RecordCount);

edit7.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

4 : begin

table1.Filtered := false;

table1.Filter := '직업 = ''공무원''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit6.text:= IntTostr(Table1.RecordCount);

edit7.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

5 : begin

table1.Filtered := false;

table1.Filter := '직업 = ''회사원''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit6.text:= IntTostr(Table1.RecordCount);

edit7.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

6 : begin

table1.Filtered := false;

table1.Filter := '직업 = ''학생''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit6.text:= IntTostr(Table1.RecordCount);

edit7.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

7 : begin

table1.Filtered := false;

table1.Filter := '직업 = ''기타''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit6.text:= IntTostr(Table1.RecordCount);

edit7.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

end;

end;

end;





procedure TForm27.ComboBox6Change(Sender: TObject);

var

i,t:integer;

begin

Table1.Filtered :=false;

t:=table1.RecordCount ;

edit10.text:= IntTostr(Table1.RecordCount);

if t>0then begin

case combobox6.ItemIndex of

0 : begin

table1.Filtered := false;

edit10.text:= IntTostr(Table1.RecordCount);

edit11.text:='';

end;

1 : begin

table1.Filtered := false;

table1.Filter := '분석 = ''A''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit10.text:= IntTostr(Table1.RecordCount);

edit11.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

2 : begin

table1.Filtered := false;

table1.Filter := '분석 = ''B''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit10.text:= IntTostr(Table1.RecordCount);

edit11.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%'; end;

3 : begin

table1.Filtered := false;

table1.Filter := '분석 = ''C''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit10.text:= IntTostr(Table1.RecordCount);

edit11.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

4 : begin

table1.Filtered := false;

table1.Filter := '분석 = ''D''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit10.text:= IntTostr(Table1.RecordCount);

edit11.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

5 : begin

table1.Filtered := false;

table1.Filter := '분석 = ''E''';

table1.Filtered := true;

edit10.text:= IntTostr(Table1.RecordCount);

i:=Table1.RecordCount;

edit11.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

6 : begin

table1.Filtered := false;

table1.Filter := '분석 = ''F''';

table1.Filtered := true;

edit10.text:= IntTostr(Table1.RecordCount);

i:=Table1.RecordCount;

edit11.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

end;

end;

end;





procedure TForm27.ComboBox1Change(Sender: TObject);

var

i,t:integer;

begin

Table1.Filtered :=false;

t:=table1.RecordCount ;

edit2.text:= IntTostr(Table1.RecordCount);

if t>0then begin

case combobox1.ItemIndex of

0 : begin

table1.Filtered := false;

edit2.text:= IntTostr(Table1.RecordCount);

edit3.text:='';

end;



1 : begin

table1.Filtered := false;

table1.Filter := 'sex = ''남자''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit2.text:=IntTostr(Table1.RecordCount);

edit3.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

2 : begin

table1.Filtered := false;

table1.Filter := 'sex = ''여자''';

table1.Filtered := true;

i:=Table1.RecordCount;

edit2.text:=IntTostr(Table1.RecordCount);

edit3.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

end;

end;

end;



end;



procedure TForm27.Button3Click(Sender: TObject);

var

reply:String;

s:string;

begin

reply:=copy(Table1.fieldByName('주민등록번호').AsString,8,1);

if (reply='1')or(reply='3') then

begin

Table1.edit;

Table1.FieldByName('sex').AsString:='남자';

Table1.Post;

end

else if (reply='2') or(reply='4') then

begin

Table1.Edit;

Table1.FieldByName('sex').AsString:='여자';

Table1.Post;

end

else

showMessage('오류^^;');

end;



procedure TForm27.Button4Click(Sender: TObject);

var

yy,mmdd,nowyy,nowmmdd:string;

reply:string;

a,yy1,mm1,dd1:integer;

begin

yy:=copy(Table1.fieldByName('주민등록번호').AsString,1,2);

mmdd:=copy(Table1.FieldByName('주민등록번호').AsString,3,4);

reply:=copy(Table1.FieldByName('주민등록번호').AsString,8,1);

if (reply='1')or(reply='2')then

yy:='19'+yy

else if(reply='3')or(reply='4') then

yy:='20'+yy

else begin

ShoWmessage('오류^^;');

exit;

end;

nowyy:=formatDateTime('yyyy',Date);

nowmmdd:=FormatDateTime('mm',Date)+FormatDateTime('dd',Date);

if nowmmdd<=mmdd then

a:=InttoStr(StrtoInt(nowyy)-StrtoInt(yy)-1)

Table1.FieldByName('age').AsInteger:=a;

else

a:=InttoStr(StrtoInt(nowyy)-Strtoint(yy));

Table1.FieldByName('age').AsInteger:=a;

end;



procedure TForm27.Table1BeforePost(DataSet: TDataSet);

var

reply:String;

s:string;

begin

reply:=copy(Table1.fieldByName('주민등록번호').AsString,8,1);

if (reply='1')or(reply='3') then

begin

Table1.edit;

Table1.FieldByName('sex').AsString:='남자';

Table1.Post;

end

else if (reply='2') or(reply='4') then

begin

Table1.Edit;

Table1.FieldByName('sex').AsString:='여자';

Table1.Post;

end

else

showMessage('오류^^;');

end;



END.



1  COMMENTS
  • Profile
    박성훈 2000.02.12 17:29
    저도 초짜입니다만, 따로 필드를 주지마시고 TTable에 계산필드를 추가하셔서 TTable의

    OnCalc Event에 코딩을 해주셔도 좋을 것 같은데요.

    그리고 소스중에 Case문이 자주 나오는데 그부분은 배열을 이용하면 라인수가 많이 줄것 같은데 어떨런지요.



    const

    Job:Array[1..7] of String=('상업','수산업','서비스업','공무원','회사원','학생','기타');

    Analisys:Array[1..6] of String=('A','B','C','D','E','F');







    > procedure TForm27.ComboBox4Change(Sender: TObject);

    > var

    > i,t:integer;

    > begin



    with Table1 do

    begin



    > Filtered :=false;

    > t:=RecordCount ;

    > edit6.text:= IntTostr(t);

    > if t>0then begin

    > if combobox4.ItemIndex=0 then

    > begin

    > //table1.Filtered := false;

    > edit6.text:= IntTostr(t);

    > edit7.text:='';

    > end

    > else begin

    > //table1.Filtered := false;

    > Filter := '직업 = '''+Job[Combobox4.ItemIndex]+''';

    > Filtered := true;

    > i:=RecordCount;

    > edit7.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

    > edit6.text:= IntTostr(i);

    > end;

    end;





    > procedure TForm27.ComboBox6Change(Sender: TObject);

    > var

    > i,t:integer;

    > begin



    with Table 1 do

    begin

    > Filtered :=false;

    > t:=RecordCount ;

    > edit10.text:= IntTostr(t);

    > if t>0 then begin

    > if combobox6.ItemIndex=0 then

    > begin

    > //Filtered := false;

    > edit10.text:= IntTostr(t);

    > edit11.text:='';

    > end

    //요부분을 배열상수를 이용해서...

    else begin

    > //Filtered := false;

    > Filter := '분석 = '''+Analisys[Combobox6.ItemIndex]+''';

    > Filtered := true;

    > i:=RecordCount;

    > edit10.text:= IntTostr(i);

    > edit11.text:= FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

    end;

    end;

    > end;

    >

    >

    > procedure TForm27.ComboBox1Change(Sender: TObject);

    > var

    > i,t:integer;

    > begin

    > Table1.Filtered :=false;

    > t:=table1.RecordCount ;

    > edit2.text:= IntTostr(Table1.RecordCount);

    > if t>0then begin

    > case combobox1.ItemIndex of

    > 0 : begin

    > table1.Filtered := false;

    > edit2.text:= IntTostr(Table1.RecordCount);

    > edit3.text:='';

    > end;

    >

    > 1 : begin

    > table1.Filtered := false;

    > table1.Filter := 'sex = ''남자''';

    > table1.Filtered := true;

    > i:=Table1.RecordCount;

    > edit2.text:=IntTostr(Table1.RecordCount);

    > edit3.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

    > end;

    > 2 : begin

    > table1.Filtered := false;

    > table1.Filter := 'sex = ''여자''';

    > table1.Filtered := true;

    > i:=Table1.RecordCount;

    > edit2.text:=IntTostr(Table1.RecordCount);

    > edit3.text:=FloatTostr(trunc((i/t*100)*100+0.5)/100)+'%';

    > end;

    > end;

    > end;

    >

    > end;

    >

    procedure TForm27.Button3Click(Sender: TObject);

    var

    reply:char;

    s:string;

    begin

    with Table1 do

    begin

    reply:= FieldByName('주민등록번호').AsString[8];



    case reply of

    '1','3': s:= '남자';

    '2','4': s:= '여자';

    else

    ShowMessage('성별이 잘못되었습니다!');

    end;

    end;

    end;



    > procedure TForm27.Button4Click(Sender: TObject);

    > var

    > yy,mmdd,nowyy,nowmmdd:string;

    > reply:string;

    > a,yy1,mm1,dd1:integer;

    > begin

    > yy:=copy(Table1.fieldByName('주민등록번호').AsString,1,2);

    > mmdd:=copy(Table1.FieldByName('주민등록번호').AsString,3,4);

    > reply:=copy(Table1.FieldByName('주민등록번호').AsString,8,1);

    > if (reply='1')or(reply='2')then

    > yy:='19'+yy

    > else if(reply='3')or(reply='4') then

    > yy:='20'+yy

    > else begin

    > ShoWmessage('오류^^;');

    > exit;

    > end;

    > nowyy:=formatDateTime('yyyy',Date);

    > nowmmdd:=FormatDateTime('mm',Date)+FormatDateTime('dd',Date);

    > if nowmmdd<=mmdd then

    > a:=InttoStr(StrtoInt(nowyy)-StrtoInt(yy)-1)

    > Table1.FieldByName('age').AsInteger:=a;

    > else

    > a:=InttoStr(StrtoInt(nowyy)-Strtoint(yy));

    > Table1.FieldByName('age').AsInteger:=a;

    > end;

    >

    > procedure TForm27.Table1BeforePost(DataSet: TDataSet);

    > var

    > reply:String;

    > s:string;

    > begin

    > reply:=copy(Table1.fieldByName('주민등록번호').AsString,8,1);

    > if (reply='1')or(reply='3') then

    > begin

    > Table1.edit;

    > Table1.FieldByName('sex').AsString:='남자';

    > Table1.Post;

    > end

    > else if (reply='2') or(reply='4') then

    > begin

    > Table1.Edit;

    > Table1.FieldByName('sex').AsString:='여자';

    > Table1.Post;

    > end

    > else

    > showMessage('오류^^;');

    > end;

    >

    > END.

    >



    그리고 Table1의 OnCalcField 이벤트에 Button3,4Click과 Table1BeforePost부분을 집어넣어주는 겁니다.

    한가지 의문이 가는 것은 Combobox에 직업이나 분석항목을 집어넣을실텐데 항목구성이

    코딩상에 나타나는 문자열들로 구성되지 않았는지요. 만약 직업선택하는 Combobox의

    항목들이 수산업,공무원... 이런식으로 되어 있다면



    Table1.Filter:= '직업='''+Combobox4.Text+''';



    이런 식으로 해주시면 될 것 같은데요. 왜 그렇게 하셨는지 궁금하군요. 그리고 가능하면 with do 구문으로 중복되는 코드를 묶어주시는 편이 속도를 쬐금이라도 빠르게 해주는 걸로 알고 있습니다.



    짧은 소견으로 주절주절 대었습니다. 중간에 고친 부분들은 기분나쁘게 생각하지 마시고

    도움이 된다면 참고해주셨으면 합니다. 그럼, 즐프하십시오.