[急]关于新增,修改,保存的问题(20分)

  • 主题发起人 主题发起人 kofoun
  • 开始时间 开始时间
K

kofoun

Unregistered / Unconfirmed
GUEST, unregistred user!
我的平台为delphi7+sqlserver2000,开发一个简单的c/s数据库系统(ado的),无非就是新增,修改,保存,取消一些事件而已,但一按新增,然后按保存按钮后,就报:
Dataset not in edit or insert mode
请问怎么解决
---------------------------------------------------------
我的新增事件如下:
procedure Tform_main.addBtnClick(Sender: TObject);
begin
dm.adoq1.close;
dm.ADOQ1.SQL.Clear;
dm.adoq1.SQL.Text:=('select * from bdailytable where fdate='''+datetostr(DateTime1.date)+''' and fline = '''+linecombox.text+''' and fproductname = '''+productnamecombox.text+''' order by fdate desc');
dm.adoq1.open;
dm.ADOQ1.append;
end;
--------------------------------------------------------------
保存事件如下:(就是将各个文本框的值保存到数据中)
procedure Tform_main.savelBtnClick(Sender: TObject);
begin
try
dm.ADOQ1.FieldByName('fdate').AsDateTime:=strtodate(Formatdatetime('yyyy-mm-dd', DateTime1.Date));
dm.ADOQ1.FieldByName('fline').AsString:=linecombox.Text;
dm.ADOQ1.FieldByName('fmaster').AsString:=mastercombox.Text;
dm.ADOQ1.FieldByName('fproductname').AsString:=productnamecombox.Text;
dm.ADOQ1.FieldByName('fgoodscount').AsString:=goodscount.Text;
dm.ADOQ1.FieldByName('fprojectname').AsString:=projectnamecombox.Text;
dm.ADOQ1.FieldByName('fcheckcount').Asstring:=trim(checkcount.Text);
dm.ADOQ1.FieldByName('fcheckokcount').Asstring:=trim(checkokcount.Text);
dm.ADOQ1.FieldByName('fcheckproject').AsString:=checkprojectcombox.Text;
dm.ADOQ1.FieldByName('fcheckng').Asstring:=trim(checkng.Text);
dm.ADOQ1.FieldByName('foutcount').Asstring:=trim(outcount.Text);
dm.ADOQ1.FieldByName('fperson').AsString:=trim(person.text);

if checkbox1.Checked then
begin
dm.ADOQ1.FieldByName('fdailytime').Asstring:=floattostr(im1);
dm.ADOQ1.FieldByName('fovertime').AsString:=floattostr(im2);
dm.ADOQ1.FieldByName('ftotaltime').AsString:=floattostr(im3);
end
else
begin
dm.ADOQ1.FieldByName('fdailytime').Asstring:=floattostr(ia1); //正常时间
dm.ADOQ1.FieldByName('fovertime').Asstring:=floattostr(ia2); //加班时间
dm.ADOQ1.FieldByName('ftotaltime').AsString:=floattostr(ia3); //加班合计
end;
dm.ADOQ1.Post;
except
showmessage('保存失败!') ;
end;
end;
修改事件如下:(当我按修改按钮后,将dbgrid某行记录,赋给各个文本框)
------------------------------------------------
procedure Tform_main.modifyBtnClick(Sender: TObject);
begin
if dm.ADOQ1.RecordCount>0 then
begin
try
with dm.ADOQ1 do
begin
DateTime1.date := StrToDate(FieldValues['fdate']);
linecombox.Text := FieldValues['fline'];
mastercombox.Text := FieldValues['fmaster'];
productnamecombox.Text := FieldValues['fproductname'];

if (FieldValues['fgoodscount'])=null then
goodscount.text:=''
else
goodscount.Text := inttostr(FieldValues['fgoodscount']);

projectnamecombox.Text := FieldValues['fprojectname'];

if (FieldValues['fcheckcount'])=null then
checkcount.Text:=''
else
checkcount.Text := inttostr(FieldValues['fcheckcount']);

if (FieldValues['fcheckokcount'])=null then
checkokcount.Text:=''
else
checkokcount.Text := inttostr(FieldValues['fcheckokcount']);

if (FieldValues['fcheckng'])=null then
checkng.Text:=''
else
checkng.Text := inttostr(FieldValues['fcheckng']);

if (FieldValues['foutcount'])=null then
outcount.Text:=''
else
outcount.Text := inttostr(FieldValues['foutcount']);

if (FieldValues['fperson'])=null then
person.Text:=''
else
person.Text := inttostr(FieldValues['fperson']);

if(FieldValues['fdailytime'])=null then
dailytime1.Text:=''
else
dailytime1.Text:=floattostr(FieldValues['fdailytime']);

if(FieldValues['fovertime'])=null then
overtime1.Text:=''
else
overtime1.Text:=floattostr(FieldValues['fovertime']);
checkprojectcombox.Text := FieldValues['fcheckproject'];
edit;
end;
except
showmessage('编辑失败!');
end;
end
else
begin
showmessage('没有选中记录!');
end;
end;
---------------------------------------------------
取消事件如下:
procedure Tform_main.cancelBtnClick(Sender: TObject);
begin
dm.ADOQ1.Cancel;
end;
 
在保存按钮中 if adoq1.state in [dsinsert,dsedite] showmessage('编辑状态');
是否在其他的地方改变了adoq1 的状态啊
 
我试放一个DBNavigator在表单里
发现了这个问题,当我按新增后,然后点击某个文本框中,获得焦点时,好像adoquery1,移动了,即是(DBNavigator的保存好像是无效的)但不知怎么处理?????
 
新增应该这样修改
我的新增事件如下:
procedure Tform_main.addBtnClick(Sender: TObject);
begin
dm.adoq1.close;
dm.ADOQ1.SQL.Clear;
dm.adoq1.SQL.Text:=('select * from bdailytable where fdate='''+datetostr(DateTime1.date)+''' and fline = '''+linecombox.text+''' and fproductname = '''+productnamecombox.text+''' order by fdate desc');
dm.adoq1.open;
dm.ADOQ1.append;
dm.ADOQ1.edit;//关键。。。必须处于编辑状态

end;
 
和liubin44966意见一致
另外,你会不会DBNavigator控件重叠放了两个在上面哦.
 
建议在新增或修改的时候就把dm.ADOQ1.insert 或dm.ADOQ1.edit
 
同意liubin44966的,如果数据集不在编辑状态是不能修改的,至于DBNavigator,我想程序里面都是放到最后的话应该不要的吧
 
dm.ADOQ1.append;
放到
dm.ADOQ1.FieldByName('fdate').AsDateTime:=strtodate(Formatdatetime('yyyy-mm-dd', DateTime1.Date));
dm.ADOQ1.FieldByName('fline').AsString:=linecombox.Text;
dm.ADOQ1.FieldByName('fmaster').AsString:=mastercombox.Text;
dm.ADOQ1.FieldByName('fproductname').AsString:=productnamecombox.Text;
dm.ADOQ1.FieldByName('fgoodscount').AsString:=goodscount.Text;
dm.ADOQ1.FieldByName('fprojectname').AsString:=projectnamecombox.Text;
dm.ADOQ1.FieldByName('fcheckcount').Asstring:=trim(checkcount.Text);
dm.ADOQ1.FieldByName('fcheckokcount').Asstring:=trim(checkokcount.Text);
dm.ADOQ1.FieldByName('fcheckproject').AsString:=checkprojectcombox.Text;
dm.ADOQ1.FieldByName('fcheckng').Asstring:=trim(checkng.Text);
dm.ADOQ1.FieldByName('foutcount').Asstring:=trim(outcount.Text);
dm.ADOQ1.FieldByName('fperson').AsString:=trim(person.text);

前面就可以了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
952
SUNSTONE的Delphi笔记
S
后退
顶部