Link to home
Start Free TrialLog in
Avatar of inthe
inthe

asked on

sort adotables

hi,
i have many tables and when i open them in msaccess they are sorted ascending though opening them in delphi they arent.(note theres no index as such )
i just wish to sort the table ascending via the field named combobox1.text +' Title'.
(the active table in delphi is set via a combobox)

i looked at adotab1e.sort := combobox1.text +' Title');
but got various errors using it..though looking at the help file it should work..

Cheers Barry
ASKER CERTIFIED SOLUTION
Avatar of wimmeyvaert
wimmeyvaert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wimmeyvaert
wimmeyvaert

I tried to use an ADOTable and play a bit with the Sort-property.
I am able to sort the data on every field of the table.
I have a DBGrid on my form, linked to a ADOTable.
In the OnTitleClick-Event I use the following code :

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  ADOTable1.Sort := Column.FieldName;
end;


This works fine for every field of my DBGrid.
What errors do you get ?

The Mayor.
Or even better,
(First declare a private unit-variable strPevSortField) :

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  if Column.FieldName = strPevSortField then
    ADOTable1.Sort := Column.FieldName + ' ASC'
  else
    ADOTable1.Sort := Column.FieldName + ' DESC';
  strPevSortField := Column.FieldName;
end;


This way you give the user the possibility to sort the data DESC or ASC :
- First time he clicks a column the sortorder is ASC.
- Second time he clicks a column the sortorder is DESC.


The Mayor.
Avatar of inthe

ASKER

hi,
im not using a dbgrid only dbmemos and dbedits etc..
i tried it to see what happens and i get the following error:

Eoleexception with message arguments are of the wrong type, are out of
acceptable range,or are in conflict with one another.

the exception comes from ADODB.TCustomAdoDataSet.InternalSetSort function
i though because the field name is two words it caused it so i tried
ADOTable1.Sort := '"'+Column.FieldName+'"';  
ADOTable1.Sort := '['+Column.FieldName+']';
as i have used before in querys to get around spaces in field names but it produced same error.
if i add a showmessage(Column.FieldName); it shows correctly the fieldname.

i was thinking about it last night to change to adoquery mainly because the searching capibility of adotable is really useless for me..now it seems more like the thing to do.

(ie i want to search tables and return all results so i can step through them so if i had dbmemos only the search results would show when stepping through using dbnavigator etc..adotable1.Locate is only good at finding first instance.)


so i will change to adoquery and see how it goes.
cheers Barry