⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editmain.pas

📁 用delphi编写的针对商品房销售合同格式
💻 PAS
字号:
unit EDITMAIN;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, LbSpeedButton, StdCtrls, Mask, DBCtrls, DB, ADODB;

type
    TIDForm = class(TForm)
        Label1: TLabel;
        DBEdit1: TDBEdit;
        LbSpeedButton1: TLbSpeedButton;
        LbSpeedButton2: TLbSpeedButton;
        LbSpeedButton3: TLbSpeedButton;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure LbSpeedButton1Click(Sender: TObject);
        procedure LbSpeedButton2Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure LbSpeedButton3Click(Sender: TObject);
        procedure FormShow(Sender: TObject);
    private
        { Private declarations }
        procedure AppendAnotherTabel(sID: string);
        procedure UpdateAnotherTabel(sID: string);
        procedure AppendAnotherTabelByMB(sID: string);

        function ExtRec: boolean;
        function IsInputNil: Boolean;

    public
        { Public declarations }
        bAppend: Boolean;
        bCreateByMB: Boolean;
        bSUCC: Boolean;
    end;

var
    IDForm: TIDForm;
    sOldID: string;
implementation
uses dm, mainUnit;
{$R *.dfm}

procedure TIDForm.AppendAnotherTabel(sID: string);
var
    I: Integer;
    ADOQ: TADOQUERY;

begin
    ADOQ := TADOQUERY.Create(SELF);
    with ADOQ do begin
        CONNECTION := dm.DataModule2.ADOConnection1;
        dm.DataModule2.ADOConnection1.BeginTrans;
        try
            for I := sTablename.Count - 1 downto 0 do begin
                if sTablename[i] = 'TID' then
                    Continue;

                SQL.Text := 'INSERT into ' + sTablename[i] +
                    ' (合同编号) values (''' + DBEdit1.text + ''')';

                ExecSQL;

            end;
            DM.DataModule2.ADOConnection1.CommitTrans;
        except
            DM.DataModule2.ADOConnection1.RollbackTrans;
        end;
        free;
    end;

end;

procedure TIDForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    ACTION := CAFREE;
    IDFORM := nil;
end;

procedure TIDForm.LbSpeedButton1Click(Sender: TObject);
begin
    FORM1.ADOQuery1.Cancel;
    Close;
end;

procedure TIDForm.LbSpeedButton2Click(Sender: TObject);
begin
    BSUCC := FALSE;
    if IsInputNil then begin
        Application.MessageBox('记录添加失败!合同编号不能为空!请修改合同编号!',
            '出错信息!', MB_ICONERROR);
        DBEdit1.SetFocus;
        Exit;
    end;
    if bAppend then begin
        if ExtRec then begin
            Application.MessageBox('记录添加失败,记录已存在!请修改合同编号!',
                '出错!', MB_ICONERROR);
            DBEdit1.SetFocus;
            Exit;
        end;
    end
    else begin
        if DBEdit1.Text <> sOldID then begin
            if ExtRec then begin
                Application.MessageBox('记录修改失败,记录已存在!请修改合同编号!',
                    '出错信息', MB_ICONERROR);
                DBEdit1.SetFocus;
                Exit;
            end;
        end;
    end;

    dm.DataModule2.ADOConnection1.BeginTrans;
    try
        FORM1.ADOQuery1.Post;
        DM.DataModule2.ADOConnection1.CommitTrans;
    except
        DM.DataModule2.ADOConnection1.RollbackTrans;
    end;
    if bAppend then
        AppendAnotherTabel(DBEdit1.Text)
    else
        UpdateAnotherTabel(DBEDIT1.Text);
    BSUCC := TRUE;
    if not bCreateByMB then
        Close;
end;

procedure TIDForm.FormCreate(Sender: TObject);
begin
    bCreateByMB := False;
    sOldID := Form1.ADOQuery1.fieldbyname('合同编号').Text;

end;

procedure TIDForm.UpdateAnotherTabel(sID: string);
var
    I: Integer;
    ADOQ: TADOQUERY;

begin
    ADOQ := TADOQUERY.Create(SELF);
    with ADOQ do begin
        CONNECTION := dm.DataModule2.ADOConnection1;
        dm.DataModule2.ADOConnection1.BeginTrans;
        try
            for I := sTablename.Count - 1 downto 0 do begin
                if sTablename[i] = 'TID' then
                    Continue;

                SQL.Text := 'UPDATE ' + sTablename[I] + ' SET 合同编号=''' +
                    DBEdit1.Text + ''' where  合同编号=''' + sOldID + '''';
                //ShowMessage(sql.Text );
                ExecSQL;

            end;
            DM.DataModule2.ADOConnection1.CommitTrans;
        except
            DM.DataModule2.ADOConnection1.RollbackTrans;
        end;
        free;
    end;

end;

function TIDForm.ExtRec: boolean;
var
    ADOQ: TADOQuery;
begin
    ADOQ := TADOQuery.create(self);
    with ADOQ do begin
        connection := DM.DataModule2.ADOConnection1;
        sql.Text := 'SELECT 合同编号 FROM TID' +
            ' WHERE 合同编号=''' + DBEdit1.Text + '''';
        open;
        result := recordcount > 0;
        Free;

    end;
end;

function TIDForm.IsInputNil: Boolean;
begin
    result := (Trim(DBEdit1.Text) = '');
end;

procedure TIDForm.LbSpeedButton3Click(Sender: TObject);
var
    adotemp: TADOQuery;
begin
    if trim(DBEdit1.Text) = '' then begin
        Application.MessageBox('合同号未填写!请先填写上合同号!', '提示', MB_ICONERROR);
        Exit;
    end;
    adotemp := TADOQuery.Create(self);
    with adotemp do begin
        Connection := dm.DataModule2.ADOConnection1;
        SQL.text := 'select * from TID where 合同编号=''MD2005''';
        OPEN;
        if RECORDCOUNT = 0 then begin
            Application.MessageBox('当前没有可使用的模板!' +
                '请先自行建立一个合同数据用以生成模板!', '提示', MB_ICONERROR);
            free;
            Exit;
        end;

        free;
    end;
    bCreateByMB := True;
    LbSpeedButton2.Click;
    bCreateByMB := False;
    AppendAnotherTabelByMB(DBEdit1.Text);
    if BSUCC then
        Close;
end;

procedure TIDForm.AppendAnotherTabelByMB(sID: string);
    function getfields(stn: string): string;
    var
        j: integer;
        adotemp: TADOQuery;
    begin
        result := '';

        adotemp := TADOQuery.Create(self);
        with adotemp do begin
            Connection := dm.DataModule2.ADOConnection1;
            SQL.Text := 'select * from ' + stn + ' WHERE 合同编号=''MD2005''';
            open;
            for j := 0 to FieldCount - 1 do begin
                if adotemp.Fields[j].DisplayName = '合同编号' then
                    Continue;
                result := Result + adotemp.Fields[j].DisplayName + '=' +
                    '''' + VarToStr(adotemp.Fields[j].AsVariant) + '''' + ',';
            end;
            free;
            result := Copy(result, 0, Length(Result) - 1);

        end;

    end;
var
    I: Integer;
    sf: string;
    ADOMBSQL: TADOQuery;
begin
    ADOMBSQL := TADOQUERY.Create(SELF);
    with ADOMBSQL do begin
        Connection := DM.DataModule2.ADOConnection1;
        dm.DataModule2.ADOConnection1.BeginTrans;
        try
            for I := sTablename.Count - 1 downto 0 do begin
                Application.ProcessMessages;
                if sTablename[I] = 'TID' then
                    Continue;

                sf := getfields(sTablename[i]);

                SQL.Text := 'update ' + sTablename[I] +
                    ' set ' + sf + ' where 合同编号=''' + DBEdit1.Text + '''';

                //ShowMessage(SQL.Text);
                ExecSQL;

            end;

            dm.DataModule2.ADOConnection1.CommitTrans;
        except
            dm.DataModule2.ADOConnection1.RollbackTrans;
        end;
        FREE;
    end;
end;

procedure TIDForm.FormShow(Sender: TObject);
begin
    if bAppend then begin
        caption := '添加新合同编号';
    end
    else begin
        caption := '修改当前合同编号';
    end;
    LbSpeedButton3.Visible := bappend;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -