如何检查将使用delphi发布到数据库的编辑中的输入数据?

时间:2013-07-18 14:27:05

标签: delphi delphi-7 ado

我用Delphi7编写了一个数据库应用程序。我正在使用ADODatabase。我正在使用编辑来获取用户的输入,但程序不起作用,它检查输入数据但从不发布。我有三个表名为Clients Receivers and Packages,请帮帮我。这是代码:

 'procedure TNewDel.Button1Click(Sender: TObject);
  Var
 Name, Name2,LName,LName2, SMessage,Date, Date2, ClNum,PackNum, RcNum, email, email2,  address, address2, cell, Cell2, Cellnum, Cellnum2, Price  :string;
k, c, c2,CountNum, weight, Distance, size :integer;
rPrice:real;
cCheck:char;
begin

            Date:= FloatToStr(DateTP.Date);
            CountNum:= 0;
            CountNum:=CountNum + 1;
    Name:= edtName.text;
    LName:= edtLName.text;
    SMessage:= ' Wrong input: ';
    Address:= edtAdd.Text;
    Email:= edtEmail.Text;
    cell:= edtCellnum.Text;

    Name2:=edtName2.text;                                     
    LNAme2:=edtLName.text;
    CellNum2:=edtCellNum.text;
    Address2:= edtAdd2.text;
    email2:= edtemail2.text;
    Cell2:= edtCellNum2.text;

    Weight:=sedWeight.value;
    Distance:= sedDist.value;
    Size:= sedSize.Value;



For K := 1 to 4       do                                                                              
 Begin
  Date2:= Date2+date[k];
 End;

 If LName= '' then  
      Begin                                                                       
         ShowMessage(sMessage+ 'Last Name');
        End
 Else


 Begin
If Name= '' then
    begin
         ShowMessage(sMessage+ 'Name');
    End

Else


    Begin
If Address= '' then
    Begin
         ShowMessage(sMessage+ 'Address');
    End

Else
 Begin
If Email= '' then
    Begin
     ShowMessage(sMessage+ 'E-Mail');
End

 Else
    begin   

If Cell = '' then
     Begin
      ShowMessage(sMessage+ 'Cell Phone Number');
            End

    Else    

 Begin

If Cell2 = '' then
    Begin
     Showmessage(sMessage + 'Receiver Cell Number');
        End

Else
 Begin      
 If Name2 = '' then
    Begin
     Showmessage(sMessage + 'Receiver Name');
    End

Else
 Begin      

If LName2 = '' then
    Begin
     Showmessage(sMessage + 'Receiver Last Name');
    End 

Else
 Begin      

If Address2 = '' then
    BEGIN
     Showmessage(sMessage + 'Receiver Address');
    End

Else
Begin       

If Email2 = '' then
    Begin
     Showmessage(sMessage + 'Receiver Cell Number');
    End

Else
 Begin  


If Distance='' then
    Begin
     ShowMessage(sMessage+ 'Distance');
    End

Else
 Begin      
If Size='' then
    begin 
     ShowMessage(sMessage+ 'Size');
    End

Else
Begin       

If Weight > 35 Then


Begin
   Showmessage('Package too heavy');
end
  Else

        Begin
    If Distance <= 150 then
        begin
         rPrice:= Distance*3.5;
         Price:= FloatToStr(rPrice);
        End

               Else
    Begin
        If distance >150 then
            Begin
             rPrice:= (150*3.5)+((distance-150)*3.5*2);
             Price:=FloatToStr(rPrice);
            End

    Else
Begin 
    If distance = 0 then
    Begin
    ShowMessage(sMessage + 'Distance');
    END

Else
Begin
CCheck:= 't';
end;
 End;


If CCheck = 't' then
Begin
        For c:= 2 to 10 do
                    Begin
                       CellNum:= Cellnum+ Cell[c];
                      End;
                        CellNum:= '+27' + Cellnum;
        For c2:= 2 to 10 do
            Begin
             CellNum2:= Cellnum2 +Cell2[c2];
            end;
     CellNum2:= '+27' + CellNum2 ;

ClNum:= 'CL'+ Name[1]+LName[1]  + Date2 + IntToStr(CountNum);
PackNum:='PK' + Name[1]+Name2[1]+ Date2;
RcNum:= 'RC'+ Name2[1]+Lname2[1]+ Date2;


ADOTable1.edit;
  ADOtable1Package_Number.Value:= PackNum;
  ADOTable1client_Number.Value:=ClNum;
  ADOTable1Name:=Name;
  ADOTable1Last_Name.Value:= LName;
  ADOTable1Address.Value:=Address;
  ADOTable1e_mail.Value:=email;
  ADOTable1Cell_Number.Value:=CellNUm;
  ADOTable1Date.Value:=Date;
Post;
End;

With ADOTable2 do
Begin
 Insert;
  FieldByName('Package_Number').Value:= PackNum;
  FieldByName('Receiver_Number').Value:=RCNum;
  FieldByName('Name').Value:=Name2;
  FieldByName('Last_Name').Value:=LNAme2;
  FieldByName('Address').Value:=Address2;
  FieldByName('E_mail').Value:=email2;
  FieldByName('Date').Value:=Date;
Post;
End;


with ADOTable3 do
    begin
     Insert;
      FieldByname('Price').Value:= Price;
      FieldByName('Package_Number').Value:= PackNum;
      FieldByName('Client_Number').Value:= ClNum;
      FieldByName('Receiver_Number').Value:=RcNum;
  FieldByName('Weight').Value:= Weight;
  FieldByName('Size').Value:=Size;
  FieldByName('Price').Value:= Price;
  FieldByName('Distance').Value:= Distance;
Post;
    end;

end;

end;
end;
 end;
 end;
  end;
end;
  end;
    end;
      end;
        end;
          end;
            end;
              end;
                end;
                  end;

我知道这可能不是最好的方式,我是一个大人物,所以这就是我要问的原因。谢谢。

2 个答案:

答案 0 :(得分:0)

我喜欢

function CheckValues(const aValue, aConst: Array of string): string;
var
  a_Index: integer;
  a_Const: String;
begin
  Result := '';
  Assert(High(aValue)= High(aConst), 'Programmer Error CheckValues: Value and Const should have the same amount of elements');
  for a_Index := Low(aValue) to High(aValue) do
  begin
    if (aValue[a_Index] = '') then
    begin
      a_Const := aConst[a_Index];
      if Result = '' then
        Result := a_Const
      else
        Result := Result + ', ' +  a_Const;
    end;
  end;
end;


procedure TForm.AllowSave;
var
  a_Value: string;
begin
  if FInit then
  begin
    a_Value := CheckValues([cbChain.Text, edName.Text, edTaxId.Text, edCustomerNo.Text, ReversalWindow.Text, editAddress1.Text,
    editCity.Text, cbState.Text, editZip.Text, editPhone.Text, editFax.Text, editEmail.Text, cbPricingTypeLU.Text],
   ['Chain','Name',  'Tax Id', 'Customer Number', 'Reversal Window', 'Address1',
    'City', 'State', 'Zip', 'Phone', 'Fax', 'Email','Pricing Type']);


    btnSave.Enabled := a_Value = '';
    if btnSave.Enabled then
    begin
      StatusBar1.SimpleText := '';
      if not (qryEdit.State in [dsInsert, dsEdit]) then
        qryEdit.Edit;
    end else
      StatusBar1.SimpleText := 'Missing: ' + a_Value;
  end;
end;

答案 1 :(得分:0)

数据库应用程序编码的一些提示(考虑到你是初学者):

  1. 使用DB感知控件总是更好。 (例如:使用TDBEdit而不是TEdit)
  2. 对于数据验证,请使用事件处理程序。
  3. 在您的情况下,您可以这样做:

    1. 使用数据库感知控件。
    2. Handel所有TADOTable实例的OnBeforePost事件

      procedure TForm1.ADOTable1BeforePost(DataSet: TDataSet);

      begin

      if SameText(ADOTable1Last_Name.AsString, EmptyStr) then

      begin

       Showmessage(sMessage + 'Receiver Last Name');
      
       Abort;//here you can also set the focus back to the ADOTable1Last_Name control.
      

      end;

      end;

    3. 渐渐地,您可以添加所有验证。通过这样做,如果您进一步对控件进行任何更改,则永远不需要更改验证代码。希望这会有所帮助。