如果给个例程code也可以。偶要往一个access库里的表的末尾添条记录,库里主键是ID(integer),代码如下,
procedure Tdaily.BitBtn1Click(Sender: TObject);
var
newid:integer;
begin
table1.Open;
newid:=0;
with table1 do
begin
first;
if not eof then
if newid <= fieldbyname('id').AsInteger then
newid:= fieldbyname('id').AsInteger;
next;
end;
newid := newid+1;
table1.Append;
table1.AppendRecord([newid,combobox2.Text,memo1.Text,strtodate(edit3.Text),combobox1.Text]);
table1.Close;就是总出错,说是我主索引重复。可是我每次都是取最大的然后+1的啊!!!!
procedure Tdaily.BitBtn1Click(Sender: TObject);
var
newid:integer;
begin
table1.Open;
newid:=0;
with table1 do
begin
first;
if not eof then
if newid <= fieldbyname('id').AsInteger then
newid:= fieldbyname('id').AsInteger;
next;
end;
newid := newid+1;
table1.Append;
table1.AppendRecord([newid,combobox2.Text,memo1.Text,strtodate(edit3.Text),combobox1.Text]);
table1.Close;就是总出错,说是我主索引重复。可是我每次都是取最大的然后+1的啊!!!!
改为
while not eof do
begin
...
...
如果ID定义的容量比较小,比如说是0到255,那么到了最后无论你的NEWID是多少,都只能插入255,当然是重复索引啦!
begin //这里加个begin..end看
if newid <= fieldbyname('id').AsInteger then
newid:= fieldbyname('id').AsInteger;
end;
begin
first;
if not eof then
if newid <= fieldbyname('id').AsInteger then
newid:= fieldbyname('id').AsInteger;
next;
end;
改成
table1.last;
newid:= fieldbyname('id').AsInteger;
这样效率要比你的高点;
另外还要判断一下table1中记录是否为空;我没调试,不知道行不行,你自己看看吧。
table1.Append;
table1.AppendRecord([newid,combobox2.Text,memo1.Text,strtodate(edit3.Text),combobox1.Text]);
table1.Append;
table1.FieldByName(字段名).asstring:=值
...
table1.Post;
var
newid:integer;
begin
table1.Open;
newid:=0;
with table1 do
begin
first;
while not eof then
begin
if newid <= fieldbyname('id').AsInteger then
newid:= fieldbyname('id').AsInteger;
next;
end;
end;
newid := newid+1;
table1.Append;
table1.AppendRecord([newid,combobox2.Text,memo1.Text,strtodate(edit3.Text),combobox1.Text]);
table1.Close;
我刚刚改完。调试米问题。再看你最后给我的code 。。一样的。:)大家都有分。再次感谢
table1.AppendRecord([newid,combobox2.Text,memo1.Text,strtodate(edit3.Text),combobox1.Text]);
table1.AppendRecord([combobox2.Text,memo1.Text,strtodate(edit3.Text),combobox1.Text]);
append的时候不用newid
table1.AppendRecord([combobox2.Text,memo1.Text,strtodate(edit3.Text),combobox1.Text]);