I'm using TADODataSet
(D7).
I do not create persistent fields in design-mode with my DataSet.
dsItems.CommandText := 'select ID, ItemName, UserIDCreate from Items';
dsUsers.CommandText := 'select ID, LoginName from Users'; // lookup dataset
If I double-click on dsItems
and "Add all fileds" and then click "New filed" and define my lookup Field, everything is working fine.
The resulting dsItems
should contain: ID, ItemName, UserIDCreate, LoginName_Lookup
How can I avoid doing all this in design-time and add a Lookup Field after/or before (? not sure) the DataSet is opened.
In other words: How do I emulate "Add all fileds" and then "New filed" to add lookup filed in run-time?
NOTE:
I get an Exception when running John's code from the IDE. The Exception occurs when trying to open the DataSet: EOleException with message 'An unknown error has occured'
.
function TCustomADODataSet.LocateRecord (ADODB.pas)
in line if FieldCount = 1 then FLookupCursor.Find...
I'm accepting the answer becouse the complied program is working.
It would be nice if someone could verify getting (or not) an Exception when runing form inside the IDE.
The dataset cannot be opened to add a lookup field at runtime.
You'll also need to add whatever other fields you'll need to access as persistent fields as well, otherwise, they will be inaccessible. The procedures below should work. However, I recommend that if you can, use queries and join your tables - it's a lot less coding, and much cleaner in my opinion.