We help IT Professionals succeed at work.

ADOTable problem

Vrtnar
Vrtnar asked
on
Medium Priority
1,125 Views
Last Modified: 2012-08-14

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??
Comment
Watch Question

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;

Author

Commented:
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  ???
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;
procedure TForm2.ADOTable1AfterScroll(DataSet: TDataSet);
begin
 if AdoTable1.RecordCount>0 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;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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;
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.