Q&A

  • 처음 짠 프로그램인데 "코딩 점검"과 "버그" 좀 잡아주세요
// 델파이로 처음 만든 프로그램입니다.

// Insert와 Update시에 Error발생되는데 왜나는지 모르겠습니다.

// 그리구 일반적인 코딩 양식에 맞는지도 궁금합니다.

// 프로잭트에 전체적인 Sample로 사용할거라서요..

// 많은 지도 편달 부닥 드려요.



unit Unit1;



interface



uses

SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,

Forms, Dialogs, DB, DBTables, Buttons, StdCtrls, DBCtrls, Grids, DBGrids,

ExtCtrls, ComCtrls;



type

TForm1 = class(TForm)

DataSource1: TDataSource;

Query2: TQuery;

StringField1: TStringField;

StringField2: TStringField;

StringField3: TStringField;

StringField4: TStringField;

StringField5: TStringField;

StringField6: TStringField;

StringField7: TStringField;

StringField8: TStringField;

StringField9: TStringField;

StringField10: TStringField;

Query1: TQuery;

StringField11: TStringField;

StringField12: TStringField;

StringField13: TStringField;

StringField14: TStringField;

StringField15: TStringField;

StringField16: TStringField;

StringField17: TStringField;

StringField18: TStringField;

StringField19: TStringField;

StringField20: TStringField;

DeleteQuery: TQuery;

BitBtn6: TBitBtn;

BitBtn7: TBitBtn;

BitBtn8: TBitBtn;

BitBtn9: TBitBtn;

BitBtn10: TBitBtn;

Database1: TDatabase;

DBGrid1: TDBGrid;

GroupBox2: TGroupBox;

Label4: TLabel;

Label5: TLabel;

Label6: TLabel;

Edit3: TEdit;

ComboBox1: TComboBox;

ComboBox2: TComboBox;

Panel1: TPanel;

SpeedButton2: TSpeedButton;

Label14: TLabel;

Label15: TLabel;

Label16: TLabel;

Label17: TLabel;

Label19: TLabel;

Label20: TLabel;

Edit1: TEdit;

Edit2: TEdit;

Edit6: TEdit;

Edit11: TEdit;

Edit7: TEdit;

Edit5: TEdit;

Edit4: TEdit;

Label21: TLabel;

Edit8: TEdit;

Label22: TLabel;

Edit9: TEdit;

Edit10: TEdit;

Label24: TLabel;

Label25: TLabel;

Query3: TQuery;

Button1: TButton;

Splitter1: TSplitter;

StatusBar1: TStatusBar;

procedure ComboBox1Change(Sender: TObject);

procedure FormActivate(Sender: TObject);

procedure SpeedButton2Click(Sender: TObject);

procedure Btn_insertClick(Sender: TObject);

procedure BitBtn2Click(Sender: TObject);

procedure BitBtn5Click(Sender: TObject);

procedure BitBtn3Click(Sender: TObject);

procedure Database1Login(Database: TDatabase; LoginParams: TStrings);

procedure DBGrid1DblClick(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure FormKeyPress(Sender: TObject; var Key: Char);

procedure Edit1KeyPress(Sender: TObject; var Key: Char);

procedure Edit2KeyPress(Sender: TObject; var Key: Char);

procedure Edit11KeyPress(Sender: TObject; var Key: Char);

procedure Edit4KeyPress(Sender: TObject; var Key: Char);

procedure Edit5KeyPress(Sender: TObject; var Key: Char);

procedure Edit6KeyPress(Sender: TObject; var Key: Char);

procedure Edit7KeyPress(Sender: TObject; var Key: Char);

procedure Edit8KeyPress(Sender: TObject; var Key: Char);

procedure Edit9KeyPress(Sender: TObject; var Key: Char);

procedure Edit10KeyPress(Sender: TObject; var Key: Char);

private

{ Private declarations }

public

{ Public declarations }

end;



var

Form1: TForm1;



implementation

//uses unit2,unit3;



{$R *.DFM}

// Sort순 지정

procedure TForm1.ComboBox1Change(Sender: TObject);

begin

if (comboBox1.ItemIndex = 0) then

begin

Query1.Close;

Query1.SQL.Clear;

Query1.SQL.ADD('select * From pmfile01 order by pm01_code');

DataSource1.Dataset := Query1;

Query1.Open;

end;

if (ComboBox1.ItemIndex = 1) then

begin

Query1.Close;

Query1.SQL.Clear;

Query1.SQL.ADD('select * From pmfile01 order by pm01_company');

DataSource1.Dataset := Query1;

Query1.Open;

end;

end;



procedure TForm1.FormActivate(Sender: TObject);

begin

Query1.Close;

Query1.SQL.Clear;

Query1.SQL.ADD('select * From pmfile01 order by pm01_code');

DataSource1.Dataset := Query1;

Query1.Open;

end;



procedure TForm1.SpeedButton2Click(Sender: TObject);

begin

// 검색문자열 '' Check

if (Edit3.Text = '') then

begin

MessageDlg('검색값을 입력하십시요!!',mtError,[mbOK],0)

end;

// 검색문자열 = 거래처 코드

if (comboBox2.ItemIndex = 0) then

begin

Query2.Close;

Query2.SQL.Clear;

Query2.SQL.ADD('select * From pmfile01 where pm01_code = "'

+ Edit3.Text + '"');

DataSource1.Dataset := Query2;

Query2.Open;

end;

// 검색문자열 = 거래처명

if (ComboBox2.ItemIndex = 1) then

begin

Query2.Close;

Query2.SQL.Clear;

Query2.SQL.ADD('select * From pmfile01 where pm01_company = "'

+ Edit3.Text + '"');

DataSource1.Dataset := Query2;

Query2.Open;

end;

// 검색문자열 = 대표자명

if (ComboBox2.ItemIndex = 2) then

begin

Query2.Close;

Query2.SQL.Clear;

Query2.SQL.ADD('select * From pmfile01 where pm01_name = "'

+ Edit3.Text + '"');

DataSource1.Dataset := Query2;

Query2.Open;

end;

end;



procedure TForm1.Btn_insertClick(Sender: TObject);

begin

// 필수항목체크 루틴

Query2.Close;

Query2.SQL.Clear;

Query2.SQL.ADD('select * From pmfile01 where pm01_code = "' + Edit1.Text + '"');

Query2.Open;

// Insert, Update의 판단

if Query2.RecordCount > 0 then

begin

StatusBar1.Panels[1].Text := ' Update 중입니다' ;

// Update

ShowMessage('해당자료를 저장합니다 /Update');

Query2.Close;

Query2.SQL.Clear;

Query2.SQL.Add(' UPDATE pmfile05 ');

Query2.SQL.Add(' SET');

Query2.SQL.Add(' pm01_company =: pm01_company, ');

Query2.SQL.Add(' pm01_name =: pm01_name, ');

Query2.SQL.Add(' pm01_tel =: pm01_tel, ');

Query2.SQL.Add(' pm01_saupnum =: pm01_saupnum, ');

Query2.SQL.Add(' pm01_fax =: pm01_fax, ');

Query2.SQL.Add(' pm01_postnum =: pm01_postnum, ');

Query2.SQL.Add(' pm01_addr =: pm01_addr, ');

Query2.SQL.Add(' pm01_bank =: pm01_bank, ');

Query2.SQL.Add(' pm01_banknum =: pm01_banknum');

Query2.SQL.Add(' WHERE pm01_code =: pm01_code ');

//

Query2.ParamByName('pm01_company').AsString := edit1.Text;

Query2.ParamByName('pm01_name').AsString := edit1.Text;

Query2.ParamByName('pm01_tel').AsString := edit1.Text;

Query2.ParamByName('pm01_saupnum').AsString := edit1.Text;

Query2.ParamByName('pm01_fax').AsString := edit1.Text;

Query2.ParamByName('pm01_postnum').AsString := edit1.Text;

Query2.ParamByName('pm01_addr').AsString := edit1.Text;

Query2.ParamByName('pm01_bank').AsString := edit1.Text;

Query2.ParamByName('pm01_banknum').AsString := edit1.Text;

Query2.ParamByName('pm01_code').AsString := edit1.Text;

Query2.ExecSQL;

Query2.Open;

// Update된 내용을 Grid에 Display 루틴 예정

end

Else

begin

// Insert

StatusBar1.Panels[1].Text := ' Insert 중입니다' ;

ShowMessage('해당자료를 저장합니다 /Insert');

Query2.Close;

Query2.Sql.Clear;

Query2.Sql.Add(' Insert Into pmfile01 '); //테이블명//

Query2.Sql.Add(' (pm01_code, pm01_company, pm01_name, pm01_tel, pm01_saupnum,)');

Query2.Sql.Add(' pm01_fax, pm01_postnum, pm01_addr, pm01_bank, pm01_banknum)');

Query2.Sql.Add(' Values ');

Query2.Sql.Add(' (pm01_code, pm01_company, pm01_name, pm01_tel, pm01_saupnum,');

Query2.Sql.Add(' pm01_fax, pm01_postnum, pm01_addr, pm01_bank, pm01_banknum)');

//

Query2.ParamByName('pm01_code').AsString := edit1.Text;

Query2.ParamByName('pm01_company').AsString := edit2.Text;

Query2.ParamByName('pm01_name').AsString := edit11.Text;

Query2.ParamByName('pm01_tel').AsString := edit4.Text;

Query2.ParamByName('pm01_saupnum').AsString := edit5.Text;

Query2.ParamByName('pm01_fax').AsString := edit6.Text;

Query2.ParamByName('pm01_postnum').AsString := edit7.Text;

Query2.ParamByName('pm01_addr').AsString := edit8.Text;

Query2.ParamByName('pm01_bank').AsString := edit9.Text;

Query2.ParamByName('pm01_banknum').AsString := edit10.Text;

Query2.ExecSQL;

Query2.Open;

// Insert된 내용을 Grid에 Display 루틴 예정

end

end;



procedure TForm1.BitBtn2Click(Sender: TObject);

//var

// Marker : Tbookmark;

begin

//Marker := DataSource1.Dataset.GetBookmark;

//Form3.edit(Datasource1.Dataset.Fields[0].Asstring);

//DataSource1.Dataset.Close;

//DataSource1.Dataset.Open;

//DataSource1.Dataset.GotoBookmark(Marker);

//DataSource1.Dataset.FreeBookmark(Marker);

DBGrid1.Setfocus;

end;



procedure TForm1.BitBtn5Click(Sender: TObject);

begin

close;

end;



procedure TForm1.BitBtn3Click(Sender: TObject);

var

key :string;

begin

key := DBGrid1.Fields[0].Asstring;

if MessageDlg(Format('거래처테이블에서 "%s"를 삭제할까요?',[key]),

mtconfirmation,mbOKCancel,0) = mrOK then

begin

DeleteQuery.Params[0].Asstring := DbGrid1.Fields[0].Asstring;

DeleteQuery.Prepare;

DeleteQuery.ExecSQL;

DataSource1.Dataset := Query1;

Query1.Close;

Query1.Open;

end;

end;





procedure TForm1.Database1Login(Database: TDatabase;

LoginParams: TStrings);

begin

LoginParams.Clear;

LoginParams.Add('USERNAME=informix');

LoginParams.Add('PASSWORD=vinus1683');

end;



procedure TForm1.DBGrid1DblClick(Sender: TObject);

begin

Edit1.Enabled := False;

Edit1.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_code').asstring;

Edit2.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_company').asstring;

Edit11.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_tel').asstring;

Edit4.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_addr').asstring;

Edit5.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_postnum').asstring;

Edit6.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_name').asstring;

Edit7.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_saupnum').asstring;

Edit8.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_fax').asstring;

Edit9.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_bank').asstring;

Edit10.Text := dbgrid1.DataSource.DataSet.fieldbyname('pm01_banknum').asstring;

end;



procedure TForm1.Button1Click(Sender: TObject);

begin

Edit1.Enabled := True;

Edit1.Text := '';

Edit2.Text := '';

Edit11.Text := '';

Edit4.Text := '';

Edit5.Text := '';

Edit6.Text := '';

Edit7.Text := '';

Edit8.Text := '';

Edit9.Text := '';

Edit10.Text := '';

Edit1.SetFocus;

end;



// 다음 항목으로 이동 (아래의 부분 함수화 예정)

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit11KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit4KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit5KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit6KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit7KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit8KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit9KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

procedure TForm1.Edit10KeyPress(Sender: TObject; var Key: Char);

begin

if key = #13 then

begin

key := #0;

Perform(CM_DialogKey, VK_TAB,0);

end;

end;

{

// IP 알아내기 루틴

function GetLocalIP: string;

type

TaPInAddr = array [0..10] of PInAddr;

PaPInAddr = ^TaPInAddr;

var

phe: PHostEnt; // HostEntry 구조체

pptr: PaPInAddr;

Buffer: array [0..MAX_PATH] of char;

i: Integer;

begin

LocalIP := '';

// GetHostName()은 지금 호스트(PC)의 호스트 이름을 Buffer에 얻어낸다

// 호스트 이름은 0으로 끝나는 문자열이 된다

GetHostName(Buffer, SizeOf(Buffer));



// GetHostByName 함수는 인자에 해당하는 호스트의 이름을 가진 호스트에 대한

// hostent구조에 대한 포인터를 리턴한다.

// GetHostByName 함수는 보통 네트워크 데이터베이스의 hosts파일을 참조하거나,

// 도메인 네임 서버(domain name server)에다 주소에 해당하는 호스트를 찾는

// 메시지를 보내고 그에 대한 응답을 기다린다.

phe := GetHostByName(buffer);

if phe = nil then

begin

LocalIP := '127.0.0.1';

System.Exit;

end;



pptr := PaPInAddr(Phe^.h_addr_list);

i := 0;

while pptr^[i] <> nil do // Multi-Homed 일 경우는 여러개의 IP가 있을 수 있으므로

begin

LocalIP := StrPas(inet_ntoa(pptr^[i]^));

Inc(i);

end;

end;

}

end.

0  COMMENTS