T_change_c是一个adotalbe,连接计数表counter
计数表有两列:id(char)和counter_value(samllint)当点击添加按钮时,实现纪录的保存
代码如下:
procedure TForm1.Button2Click(Sender: TObject);
 var
  counter:integer;
begin
  T_stud_info_1.Open;
  if T_stud_info_1.RecordCount>0 then //确认学号是否存在
  begin
    T_counter_c.Open;
    counter:=T_counter_c['COUNTER_VALUE'];
    Inc(counter); //累加学籍变更计数器
    T_counter_c.Edit;
    T_counter_c['COUNTER_VALUE']:=counter;
    T_counter_c.Post;//修改计数器值
    T_change.Open;//打开变更表追加纪录 
    T_change.AppendRecord([counter, i_stud_id_1.text, i_change.ItemIndex, Now, i_descrip_1.text]);
    Button1Click(nil);
  end
  else
    Application.MessageBox('请确认输入的学号是否正确!', '错误', MB_OK);
end;程序运行时提示'could not convert variant of type(null) into type(integer)
好像是计数器没有值,怎么回事?
盼回复.

解决方案 »

  1.   

    应该是这里出错:counter:=T_counter_c['COUNTER_VALUE'];
    你打开数据集时没有确保T_counter_c['COUNTER_VALUE']是非空的,一般习惯访问字段值用FieldByName要好一些:
    counter := 0;
    if (T_counter_c.FieldByName('COUNTER_VALUE').IsNull = False) then
      counter:=T_counter_c['COUNTER_VALUE'];
      

  2.   

    并且T_counter_c为空时,要注意保存记录时用的是T_counter_c.Append而不是T_counter_c.Edit;
      

  3.   

    从错误提示来看应该类型转换时出错,T_counter_c['COUNTER_VALUE']是空值!