Link to home
Create AccountLog in
Avatar of Vrtnar
Vrtnar

asked on

ADOTable problem


Here:

procedure TForm2.ADOTable1AfterOpen(DataSet: TDataSet);
var AutoIncCount : Integer;
begin
ADOTable1.Sort := 'ID DESC' ;
AutoIncCount := ADOTable1.Fieldbyname('Member_ID').Asinteger;
cxTextEdit1.Text :=  inttostr(AutoIncCount + 1) ;
cxTextEdit2.SetFocus;
end;

procedure TForm2.ADOTable1AfterScroll(DataSet: TDataSet);
begin
wwdblookupcombo1.text := ADOTable1.FieldByName('Member_ID').AsString  ;
StatusBar1.Panels[0].Text := ' All records :' +IntToStr(ADOTable1.recordcount);
StatusBar1.Panels[1].Text:= ' Current record :'  +inttostr(ADOTable1.RecNo);
end;

Problem is this:
How do I prevent the program from crashing down if the adotable1 is empty
at runtime??
Avatar of pcsentinel
pcsentinel

procedure TForm2.ADOTable1AfterOpen(DataSet: TDataSet);
var AutoIncCount : Integer;
begin
 if not ADOTable1.Eof then
 begin
    ADOTable1.Sort := 'ID DESC' ;
   AutoIncCount := ADOTable1.Fieldbyname('Member_ID').Asinteger;
   cxTextEdit1.Text :=  inttostr(AutoIncCount + 1) ;
  cxTextEdit2.SetFocus;
 end;
end;

procedure TForm2.ADOTable1AfterScroll(DataSet: TDataSet);
begin
 if not (ADOTable1.Bof) and (ADOTable1.Eof) then
 begin
    wwdblookupcombo1.text := ADOTable1.FieldByName('Member_ID').AsString  ;
    StatusBar1.Panels[0].Text := ' All records :' +IntToStr(ADOTable1.recordcount);
    StatusBar1.Panels[1].Text:= ' Current record :'  +inttostr(ADOTable1.RecNo);
  end;
end;
Avatar of Vrtnar

ASKER

It does not work right...
The only way it seems to work is :

procedure TForm2.ADOTable1AfterScroll(DataSet: TDataSet);
begin
if not ADOTable1.Bof then
 begin ....

Why is it when I delete last record from table
StatusBar1.Panels[1].Text:= ' Current record :'  +inttostr(ADOTable1.RecNo);
statusbar displays  -1  ???
Avatar of Amir Azhdari
Vrtnar
use  ADOTable1.RecordCount in the events i mean :

procedure TForm2.ADOTable1AfterOpen(DataSet: TDataSet);
var AutoIncCount : Integer;
begin
 if ADOTable1.RecordCount>0 then
 begin
    ADOTable1.Sort := 'ID DESC' ;
   AutoIncCount := ADOTable1.Fieldbyname('Member_ID').Asinteger;
   cxTextEdit1.Text :=  inttostr(AutoIncCount + 1) ;
  cxTextEdit2.SetFocus;
 end;
end;


procedure TForm2.ADOTable1AfterScroll(DataSet: TDataSet);
begin
 if AdoTable1.RecordCount then
 begin
    wwdblookupcombo1.text := ADOTable1.FieldByName('Member_ID').AsString  ;
    StatusBar1.Panels[0].Text := ' All records :' +IntToStr(ADOTable1.recordcount);
    StatusBar1.Panels[1].Text:= ' Current record :'  +inttostr(ADOTable1.RecNo);
  end;
end;
ASKER CERTIFIED SOLUTION
Avatar of Amir Azhdari
Amir Azhdari
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I think the reason is that because its an empty recordset then there is no current record number so it returns a default '-1', use the code below

procedure TForm2.ADOTable1AfterScroll(DataSet: TDataSet);
begin
 if not (ADOTable1.Bof) and (ADOTable1.Eof) then
 begin
    wwdblookupcombo1.text := ADOTable1.FieldByName('Member_ID').AsString  ;
    StatusBar1.Panels[0].Text := ' All records :' +IntToStr(ADOTable1.recordcount);
    StatusBar1.Panels[1].Text:= ' Current record :'  +inttostr(ADOTable1.RecNo);
  end
  else
  begin
    StatusBar1.Panels[0].Text := ' All records : 0';
    StatusBar1.Panels[1].Text:= '';
  end;
end;