有一个单元文件,用来做部门资料管理,在程序刚编译完成时,可以正确添加部门信息,修改也没有问题,但在多次运行以后,就发现添加部门信息时,只能保存部门编号,而不能保存部门的其它信息,但如果编辑部门信息,则还是一切正常。不知道是我程序代码的问题还是D7本身的问题,我已经把这个单元重做过了一次,结果还是一样,所以请教!下面是单元文件、窗体文件、以及SQL脚本
单元文件:
unit Bmzl;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Grids, DBGrids, DB, ADODB, DBControl, XPMenu,
  ComCtrls, dxtree, dxdbtree,dmunit;
{DBControl是一个定义的控件,类似TDBNavigator,只是把TDBNav的事件拿出来,在程序中完成;XPMenu,dxtree,dxdbtree都是第三方控件,一个是做XP界面效果,一个是根据数据库的内容做一个TreeView;dmunit是一个程序的数据单元。
type
  TfrmBmzl = class(TForm)
    D1: TADODataSet;
    DS: TDataSource;
    DG: TDBGrid;
    P: TPanel;
    L5: TLabel;
    L4: TLabel;
    L3: TLabel;
    L2: TLabel;
    L1: TLabel;
    E5: TEdit;
    E4: TEdit;
    E3: TCombobox;
    E2: TEdit;
    E1: TEdit;
    XP: TXPMenu;
    DB: TDBControl;
    TV: TdxDBTreeView;
    T: TADODataSet;
    procedure E1KeyPress(Sender: TObject; var Key: Char);
    procedure DBClick(Sender: TObject; Button: TDBBtn);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TVChange(Sender: TObject; Node: TTreeNode);
    procedure E3DropDown(Sender: TObject);
  private
    { Private declarations }
    procedure GetCurrentRec;
    procedure TSelect(S:String);
  public
    { Public declarations }
  end;procedure CreateBmzl(C:String);stdcall;var
  frmBmzl: TfrmBmzl;implementationuses Pub_info;{$R *.dfm}
//创建是主程序中的调用函数,来创建这个窗体
procedure CreateBmzl(C:String);
var
  F:TfrmBmzl;
begin
  F:=TfrmBmzl.Create(Application);
  F.Caption:=C;
  F.Show;
end;procedure TfrmBmzl.E1KeyPress(Sender: TObject; var Key: Char);
begin
  case Key of
    '0'..'9',char(VK_back),Char(vk_delete):;
    else  Key:=#0;
  end;
end;//取当前记录的各字段值,以赋给相应的控件
procedure TfrmBmzl.GetCurrentRec;
begin
  if D1.FieldByName(L1.Caption).IsNull then E1.Clear else  E1.Text:=D1.FieldValues[L1.Caption];
  if D1.FieldByName(L2.Caption).IsNull then E2.Clear else  E2.Text:=D1.FieldValues[L2.Caption];
  if D1.FieldByName(L3.Caption).IsNull then E3.Clear else  E3.Text:=D1.FieldValues[L3.Caption];
  if D1.FieldByName(L4.Caption).IsNull then E4.Clear else  E4.Text:=D1.FieldValues[L4.Caption];
  if D1.FieldByName(L5.Caption).IsNull then E4.Clear else  E5.Text:=D1.FieldValues[L5.Caption];
end;//自定数据导航控件的单击事件
procedure TfrmBmzl.DBClick(Sender: TObject; Button: TDBBtn);
begin
  case Button of
  dbInsert://单击添加按钮
    begin
      P.Enabled:=true;
      D1.Append;
      GetCurrentRec;
    end;
  dbEdit://单击编辑按钮
    begin
      P.Enabled:=True;
      D1.Edit;
    end;
  dbPost://单击更新按钮
    begin
      P.Enabled:=False;
      D1.FieldByName(L1.Caption).AsString:=E1.Text;
      D1.FieldByName(L2.Caption).AsString:=E2.Text;
      D1.FieldByName(L3.Caption).AsString:=E3.Text;
      D1.FieldByName(L4.Caption).AsString:=E4.Text;
      D1.FieldByName(L5.Caption).AsString:=E5.Text;
      D1.Post;
    end;
  dbCancel://单击取消按钮
    begin
      P.Enabled:=true;
      D1.Cancel;
    end;
  dbDelete://单击删除按钮,先判断选择的部门是否包含子部门及员工,包含则停止删除
    begin
      TSelect('Select 序号 From P_bmzl Where 上级部门='+D1.FieldValues['部门编号']);
      if T.RecordCount>0 then
        ShowInfo('指定部门有下级部门存在,请先将其清空!')
      else
      begin
        TSelect('Select 序号 From P_ygzl Where 部门编号='+D1.FieldValues['部门编号']);
        if T.RecordCount>0 then
          ShowInfo('指定部门包含有多个员工,请先将其清空!')
        else
          D1.Delete;
      end;
    end;
  dbRefresh:E3.Tag:=0;
  dbReturn:Close;
  end;
end;procedure TfrmBmzl.FormCreate(Sender: TObject);
begin
  D1.Connection:=DM.DAdoCnt;
  T.Connection:=DM.DAdoCnt;
  D1.CommandText:='Select * From P_bmzl Order By 上级部门,序号';
  D1.Prepared:=true;
  D1.Active:=true;
end;procedure TfrmBmzl.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;procedure TfrmBmzl.TVChange(Sender: TObject; Node: TTreeNode);
begin
  GetCurrentRec;
end;
//填充下接列表框
procedure TfrmBmzl.E3DropDown(Sender: TObject);
begin
  If E3.Tag=0 then
  begin
    E3.Items.Clear;
    TSelect('Select 部门编号,部门名称 From P_bmzl Order By 部门编号');
    with T do
    while Not Eof do
    begin
      E3.Items.Add(FieldValues['部门编号']+'  '+Trim(FieldValues['部门名称']));
      Next;
    end;
    E3.Tag:=1;
  end;//End of If
end;procedure TfrmBmzl.TSelect(S: String);
begin
  T.Active:=False;
  T.CommandText:=S;
  T.Prepared:=True;
  T.Active:=True;
end;end.

解决方案 »

  1.   


    //窗体文件
    object frmBmzl: TfrmBmzl
      Left = 213
      Top = 140
      Width = 444
      Height = 386
      Color = clBtnFace
      Font.Charset = ANSI_CHARSET
      Font.Color = clWindowText
      Font.Height = -12
      Font.Name = '宋体'
      Font.Style = []
      FormStyle = fsMDIChild
      OldCreateOrder = False
      Position = poMainFormCenter
      Visible = True
      OnClose = FormClose
      OnCreate = FormCreate
      PixelsPerInch = 96
      TextHeight = 15
      object DG: TDBGrid
        Left = 0
        Top = 0
        Width = 436
        Height = 128
        Align = alTop
        DataSource = DS
        TabOrder = 0
        TitleFont.Charset = ANSI_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -12
        TitleFont.Name = '宋体'
        TitleFont.Style = []
      end
      object P: TPanel
        Left = 146
        Top = 128
        Width = 290
        Height = 184
        Align = alRight
        Enabled = False
        TabOrder = 2
        object L5: TLabel
          Left = 30
          Top = 165
          Width = 48
          Height = 15
          Caption = '部门说明'
        end
        object L4: TLabel
          Left = 30
          Top = 130
          Width = 60
          Height = 15
          Caption = '部门负责人'
        end
        object L3: TLabel
          Left = 30
          Top = 95
          Width = 48
          Height = 15
          Caption = '上级部门'
        end
        object L2: TLabel
          Left = 30
          Top = 60
          Width = 48
          Height = 15
          Caption = '部门名称'
        end
        object L1: TLabel
          Left = 29
          Top = 24
          Width = 48
          Height = 15
          Caption = '部门编号'
        end
        object E5: TEdit
          Left = 106
          Top = 155
          Width = 152
          Height = 23
          MaxLength = 128
          TabOrder = 4
        end
        object E4: TEdit
          Left = 106
          Top = 120
          Width = 152
          Height = 23
          MaxLength = 14
          TabOrder = 3
        end
        object E3: TComboBox
          Left = 106
          Top = 85
          Width = 152
          Height = 23
          ItemHeight = 15
          MaxLength = 3
          TabOrder = 2
          OnDropDown = E3DropDown
          OnKeyPress = E1KeyPress
        end
        object E2: TEdit
          Left = 106
          Top = 50
          Width = 152
          Height = 23
          MaxLength = 12
          TabOrder = 1
        end
        object E1: TEdit
          Left = 106
          Top = 14
          Width = 152
          Height = 23
          MaxLength = 3
          TabOrder = 0
          OnKeyPress = E1KeyPress
        end
      end
      object DB: TDBControl
        Left = 0
        Top = 312
        Width = 436
        Height = 47
        Align = alBottom
        BevelOuter = bvNone
        FullRepaint = False
        TabOrder = 3
        OnClick = DBClick
        DataSource = DS
      end
      object TV: TdxDBTreeView
        Left = 0
        Top = 128
        Width = 146
        Height = 184
        ShowNodeHint = True
        DataSource = DS
        KeyField = '部门编号'
        ListField = '部门名称'
        ParentField = '上级部门'
        SeparatedSt = ' - '
        RaiseOnError = True
        ReadOnly = True
        Indent = 19
        OnChange = TVChange
        Align = alClient
        ParentColor = False
        Options = [trDBCanDelete, trDBConfirmDelete, trCanDBNavigate, trSmartRecordCopy, trCheckHasChildren]
        SelectedIndex = -1
        TabOrder = 1
      end
      object D1: TADODataSet
        CacheSize = 100
        CursorType = ctStatic
        CommandText = 'select * from P_bmzl'
        Parameters = <>
        Left = 102
        Top = 64
      end
      object DS: TDataSource
        DataSet = D1
        Left = 139
        Top = 64
      end
      object XP: TXPMenu
        DimLevel = 30
        GrayLevel = 10
        Font.Charset = DEFAULT_CHARSET
        Font.Color = clMenuText
        Font.Height = -12
        Font.Name = '宋体'
        Font.Pitch = fpVariable
        Font.Style = []
        Color = clBtnFace
        IconBackColor = clBtnFace
        MenuBarColor = clBtnFace
        SelectColor = clHighlight
        SelectBorderColor = clHighlight
        SelectFontColor = clMenuText
        DisabledColor = clInactiveCaption
        SeparatorColor = clBtnFace
        CheckedColor = clHighlight
        IconWidth = 24
        DrawSelect = True
        UseSystemColors = True
        OverrideOwnerDraw = False
        Gradient = False
        FlatMenu = True
        AutoDetect = True
        Active = True
        ControlUseTrueXPStyle = True
        BtnRoundArc = 5
        BtnOutLineBorderColor = 7552000
        BtnInnerBorderMoveColor = 3257087
        BtnInnerBorderFocusColor = 15183500
        BtnSurfaceNormalColor = 16251903
        BtnSurfaceDownColor = 14608359
        BtnSurfaceBottomLineColor = 14608359
        BtnSurfaceDownBottomLineColor = 15199215
        RdoChkControlChkColor = 41472
        ComboBoxChkColor = 9201994
        ComboboxSurfaceMoveColor = 16771030
        ControlDisabledBorderColor = 11913158
        Left = 176
        Top = 65
      end
      object T: TADODataSet
        Parameters = <>
        Left = 104
        Top = 97
      end
    end
    //创建部门表的SQL脚本(MS_Sql 2000桌面版)
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[P_bmzl]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[P_bmzl]
    GO/****** Object:  User dbo    Script Date: 03-8-23 10:45:06 ******/
    /****** Object:  Table [dbo].[P_bmzl]    Script Date: 03-8-23 10:45:18 ******/
    CREATE TABLE [dbo].[P_bmzl] (
    [序号] [int] IDENTITY (1, 1) NOT NULL ,
    [部门编号] [char] (3) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [部门名称] [char] (24) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [上级部门] [char] (3) COLLATE Chinese_PRC_CI_AS NULL ,
    [部门负责人] [char] (14) COLLATE Chinese_PRC_CI_AS NULL ,
    [部门说明] [text] COLLATE Chinese_PRC_CI_AS NULL 
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GOALTER TABLE [dbo].[P_bmzl] WITH NOCHECK ADD 
    CONSTRAINT [PK_P_bmzl] PRIMARY KEY  CLUSTERED 
    (
    [序号]
    )  ON [PRIMARY] 
    GO CREATE  UNIQUE  INDEX [UK_P_bmzl] ON [dbo].[P_bmzl]([部门编号], [部门名称]) ON [PRIMARY]
    GO