hi there,
Change your add method as following:
procedure TRoomForm.AddRoomClick(Sender: TObject);
begin
//currentroom := 12; not needed
if adoquery3.Active then adoquery3.close;
adoquery3.SQL.Clear;
ADOQuery3.SQL.Add('Select max(roomnumber) + 1 as RoomNumber from Rooms');
adoquery3.Open;
currentroom := adoquery3.FieldByName('RoomNumber').asInteger;
showmessage(inttostr(currentroom));
end;
Imagine a dataset(table, query) as a matrix. You have access to any field in dataset through FieldByName('columnname').asInteger (or asString, or asBoolean depending on the field type in database). FieldByName will get the item for the current record(row), so for more processing you can loop through dataset (for this use query.First, query.Next).
Good luck,
Ionut
Ionelul
Junior Poster in Training
94 posts since Dec 2009
Reputation Points: 17
Solved Threads: 27
If you change the select as
'Select roomnumber from Rooms where roomnumber = (select max(roomnumber) from rooms)'
do you still get the error?
Ionelul
Junior Poster in Training
94 posts since Dec 2009
Reputation Points: 17
Solved Threads: 27
I did some tests myself and the problem is as I thought:
in your initial query you had
'Select max(roomnumber) from Rooms'
if you put this query in a mssql's query analyzer, you will see that the result of this query is like
(No column name)
aValue
on the form create event, you put
DbEdit2.DataField := 'RoomNumber';
Now, when you open the query, dbedit, being binded with adoquery3 through datasource3, tries to get roomnumber field from dataset, but your field is marked as Column1(or "(No column name)") and the application doesn't find it( and raises error).
the solution for this issue is to put a name for the column in your sql query
select max(columnname) as columnname from table
This applies to all sql aggregate functions.
I suggest to find a way to insert this type of select(with naming the columns) in your application because your current way is kinda slow. you query the table twice in a single statement and that's not a good practice.
Good luck,
Ionut
Ionelul
Junior Poster in Training
94 posts since Dec 2009
Reputation Points: 17
Solved Threads: 27