首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

If Then Else 問題解决办法

2012-03-26 
If Then Else 問題procedureTForm1.Button6Click(Sender:TObject)beginif(edit1.text)and(edit2.text)a

If Then Else 問題
procedure   TForm1.Button6Click(Sender:   TObject);

begin
  if   (edit1.text= ' ')     and   (edit2.text= ' ')   and   (edit3.text= ' ')   and   (edit4.text= ' ')
        and   (edit5.text= ' ')and   (edit6.text= ' ')   and   (edit7.text= ' ')   and   (edit8.text= ' ')
        and   (edit11.text= ' ')   and   (edit12.text= ' ')   then
  messagedlg( '對不起沒有這筆記錄 ',mterror,[mbcancel],0)
  else
  begin
  with   ka_date.adopublic   do
    begin
    Close;
    SQL.Clear;
    SQL.Add( 'select   k_emp_code,k_dept_code,k_emp_name,k_sex,k_edu,k_skill,k_in_date, ');
    SQL.Add( 'k_teacher,k_mode,k_task,k_date,k_grade,k_add   from   lita_xlka   where   ');
  if   (edit1.text <> ' ')   then
                sql.add( 'k_emp_code   =   :e ');
                parameters.ParamByName( 'e ').Value:=trim(edit1.Text);
    if   (   ComboBox1.text <> ' ')   and   (edit1.text= ' ')   then
                SQL.Add( 'k_dept_code=:e1 ');
                parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text)
              else
          begin
        if   (   ComboBox1.text <> ' ')   and   (edit1.text <> ' ')   then
              sql.add( 'and   k_dept_code=:e1 ');
              parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text);
            end;

    execsql;
    end;

end;
    ka_date.ADOpublic.Active:=true;
end;
總是出以下錯誤
[Error]   l_main.pas(307):   'END '   expected   but   'ELSE '   found
[Error]   l_main.pas(308):   Missing   operator   or   semicolon
[Error]   l_main.pas(314):   Undeclared   identifier:   'execsql '
[Error]   l_main.pas(317):   Declaration   expected   but   identifier   'ka_date '   found
[Error]   l_main.pas(318):   '. '   expected   but   '; '   found
[Fatal   Error]   Project1.dpr(7):   Could   not   compile   used   unit   'l_main.pas '


[解决办法]
if ( ComboBox1.text <> ' ') and (edit1.text= ' ') then
SQL.Add( 'k_dept_code=:e1 ');
parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text)
else
begin
if ( ComboBox1.text <> ' ') and (edit1.text <> ' ') then
sql.add( 'and k_dept_code=:e1 ');
parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text);
end;
改为
if ( ComboBox1.text <> ' ') and (edit1.text= ' ') then
begin
SQL.Add( 'k_dept_code=:e1 ');


parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text);
end
else
begin
if ( ComboBox1.text <> ' ') and (edit1.text <> ' ') then
sql.add( 'and k_dept_code=:e1 ');
parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text);
end;

[解决办法]
procedure TForm1.Button6Click(Sender: TObject);

begin
if (edit1.text= ' ') and (edit2.text= ' ') and (edit3.text= ' ') and (edit4.text= ' ')
and (edit5.text= ' ')and (edit6.text= ' ') and (edit7.text= ' ') and (edit8.text= ' ')
and (edit11.text= ' ') and (edit12.text= ' ') then
messagedlg( '對不起沒有這筆記錄 ',mterror,[mbcancel],0) //这里少一个 分号
else
begin
with ka_date.adopublic do
begin
Close;
SQL.Clear;
SQL.Add( 'select k_emp_code,k_dept_code,k_emp_name,k_sex,k_edu,k_skill,k_in_date, ');
SQL.Add( 'k_teacher,k_mode,k_task,k_date,k_grade,k_add from lita_xlka where ');
if (edit1.text <> ' ') then
sql.add( 'k_emp_code = :e ');
parameters.ParamByName( 'e ').Value:=trim(edit1.Text);
if ( ComboBox1.text <> ' ') and (edit1.text= ' ') then
SQL.Add( 'k_dept_code=:e1 ');
parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text)
else //这个else 上面要放一个 end
begin
if ( ComboBox1.text <> ' ') and (edit1.text <> ' ') then
sql.add( 'and k_dept_code=:e1 ');
parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text);
end;

execsql; //不知道你这个过程是从那里来的 但是提示没有定义
end;

end;
ka_date.ADOpublic.Active:=true;
end;


//================最后初来D区=====望楼主结贴
[解决办法]
最好把注释加上,否则看不出来你的意图是什么。另外注意符合语句使用begin 和end的配对,比如
if ( ComboBox1.text <> ' ') and (edit1.text <> ' ') then
begin
sql.add( 'and k_dept_code=:e1 ');
parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text);
end;
类似这样的语句应注意缩进格式,否则程序太难读了。
[解决办法]
then 后面如要处理多条代码应该用begin end ,比如这一句
if ( ComboBox1.text <> ' ') and (edit1.text= ' ') then
SQL.Add( 'k_dept_code=:e1 '); //如果不用begin end ,这个地方已经执行完这个条件语句了
parameters.ParamByName( 'e1 ').Value:=trim(ComboBox1.Text) //而这个地方不用判别绝对会执行的
else//这样这个else就没有if then 的前提了

具体应该改成楼上所说的那个比如一样

热点排行