Delphi Firemonkey ComboBox insert in AdoTable - delphi

So, I've been using LiveBindings between two tables at a combobox to get the Id and Description of a foreign key(with the Item.LookupData and Item.text properties) and assign the key to a field in another table with the SelectedValue propertie. This is working fine i guess but i'm using custom dbnavigator controls to make a "register form".
I'm using methods like this to make the inserts: adotablealuno.FieldValues['Nome']:=editnomeAluno.Text;
But I'cant find how to use the combobox in this way, i've already tried the ItemIndex and Selected properties, but none of this work (I'm using a Access DB btw). How can I use my foreign key in ComboBox for this?

Actually I was already stating this question and there was no good answer for a long time. I found a way out that I am currently using. At least I know that I'll get reliable data.
You need to handle OnFillingListItem event of the LinkFillControlToField link in alike way and store id number in ComboBox Items. I use Tag property for this purpose though it is not actually good.
procedure TForm1.LinkFillControlToField1FillingListItem(Sender: TObject;
const AEditor: IBindListEditorItem);
begin
(AEditor.CurrentObject as TListBoxItem).Tag :=
YourLookuptable.FieldByName('id').AsInteger;
end;
And then fetch the item id from ListBox1.Selected.Tag. The text value can be accessed via ComboBox1.Selected.Text.
Appended.
You make an alike LinkFillControlToField link.
Then you select this link and create an OnFillingListItem event handler to the link (select events tab in the Object Inspector and double click on OnFillingListItem ComboBox). The event handler (empty procedure) will appear. It will be given a name like TForm1.LinkFillControlToField1FillingListItem(... Then you write the code setting the id property to the items' Tags.

Related

TDBLookupComboBox shows nothing

I am trying to list of my users table's username in combobox. I find a dblookupcombobox for this. But it does not show anything.
I set the datasource to my dsUser and datafield to username. But it does not show anything. Also i open ADQueryUser.Open in formcreate.
If i use TDBCombobox, this time it shows only first row and i cannot search or etc.
I checked the my TFDQuery which is work. I can see my data in DBGrid.
Also i tried to refresh it:
procedure TFormMain.FormShow(Sender: TObject);
begin
ADQueryUser.Open;
DBLookupComboBox1.Refresh;
end;
I want to use combobox to search and select using keyup any selecting option. But which combobox is fitting my requirements ? And how can i use ?
DataSource and DataField are the datasource and field you would be modifying using the control. Similar to the DataSource and DataField properties of a plain TDBEdit, they only declare where the value of your control is stored in, not what is listed.
Use ListSource, ListField and KeyField to define the datasource, visible values (e.g. user name) and keyfield (e.g. userid) that is used to populate the list.

FireMonkey ComboBox assigning Selected. Text from query

I need to allow my users to view a previously selected item from a ComboBox. A query provides the previous choice to the ComboBox.
I was hoping Live Bindings would get me through it, but it is so slow, I can't use it.
I am able to complete a similar action using a ComboEditBox, but can't seem to sort out what needs to be done with the ComboBox.
For the ComboEditBox, the following code works fine:
ComboInspector1.Text := FDQueryGetInspectionInspector_1.Text;
However, the following code does not work for the ComboBox:
ComboStationLocated.Selected.Text := FDQueryGetInspectionStation_Found.Text;
Any Ideas?
I believe you are trying to select an item based on some text, so below will select an item in the combobox if it already exists from the text of YourObject (whatever this may be)
ComboStationLocated.ItemIndex:=ComboStationLocated.Items.IndexOf(YourObject.Text);
If this is not what your are trying to do I will edit my answer.

Cannot assign to a read only property

I have a form which has a TDBLookupComboBox on it.
The TDBLookupComboBox is displaying a list of records from within a database table.
At the point of the forms OnShow event I would like the TDBLookupComboBox to already display one of the strings in the list.
I have done this so far...
procedure TfrmMain.FormShow(Sender: TObject);
begin
dblucbox.Text := Username;
end
Username is a string for one of the records already in the list.
At the point of compiling, I get an error saying
Cannot assign to a read only property
I'm a bit stuck with this so any help would be appreciated.
Don't try to modify the Text property, instead if you want set the TDbLookUpComboBox in a particular item you must use the the KeyValue property which will try to locate the record in the underlying TDataSet.
So you if you have Key Value of the user you can use something like this
dblucbox.KeyValue := UserId;
Otherwise you can use the Locate method of the underlying TDataSet, to find the match and the LookUp control will be refreshed automatically
You're going about this backwards. To be more precise, the DBLookupCombo is reflecting the state of the database table. So you want to be manipulating the table, not the combobox.
In other words, the OnShow event needs to open the table that's the object of the DBLookupCombo (if it's not already open) and then position the current record to be the one you want displayed as the default.

Is there a ComboBox that has Items like a TcxRadioGroup?

The TcxRadioGroup component of DevExpress has a very nice way to specify items. You can specify a Caption and a Value (and a Tag) for each TcxRadioGroupItem.
The TcxComboBox and the normal TComboBox of Delphi on the other hand use TStrings to store its items.
While TStrings can have a Name and an Object, there is no easy way to hook up a name and a value using the form designer of the Delphi IDE.
Is there a ComboBox control (preferably from DevExpress) that allows to visually design its items with a Caption and a Value?
PS: I'm not looking for a DB aware control.
Try a TcxImageComboBox. See here - you don't have to assign images despite the name. You can also edit the items visually.
(I use it as cell editor in cxGrids because of the separation Description/Value.)
Raize Components have TRzComboBox which introduces a Values property as an addition to the existing Items.
ESBPCS for VCL has an enhanced Lookup ComboBox. It stores 2 Lists, the ones normally in TCombobox's Items as well as the new Values list. These two StringLists are in a 1-1 relationship. Use AsItem to retrieve the string currently displayed and AsValue to retrieve the "related" string from Values.
Use a standard Delphi TComboBox, it can store a string (for visualization, and an object of any TObject descendant that you implement yourself, i.e you can store anything associated to a string in the dropdown).

AutoID Field in Many-to-Many, Insert Using TDataSet / Query (Delphi / MSAccess)

I have a database of "Items", which are assigned to multiple "Categories". Categories can have multiple items, and vice versa. The relevant portion of the database structure is as follows:
[tblItem]
ItemID (AutoNumber)
MainText (Text)
[tblCategory]
CategoryID (AutoNumber)
Name (Text)
[tblItemCategory]
ItemID (Long Integer)
CategoryID (Long Integer)
I want to build a panel component which shows a category name at the top, with a databound grid of items belonging to that grid below. There will be many instances of this panel component, and the end-user should be able to create a new item and simultaneously assign it to the category in question from any one of them.
In MS Access, it's possible to create a nested form, with the "child" one databound to a query which is "MasterFields" linked to a databound "Category" field on the "parent" form, such that the grid of items changes as the Category field is changed. This Items grid can also easily have new records added to it, with both the ItemID (in tblItem AND table tblItemCategory) and the linked CategoryID field (in tblItemCategory) being populated automatically.
The query for that Access form's grid is:
SELECT tblItemCategory.CategoryID, tblItem.*
FROM tblItemCategory LEFT JOIN tblItem ON tblItemCategory.ItemID = tblItem.ItemID
ORDER BY tblItemCategory.CategoryID;
If I try the same thing in Delphi, the ItemID AutoNumber field doesn't get populated, resulting in the following error:
..exception class EOleException with message 'The field 'tblItemCategory.ItemID' cannot contain a Null value because the Required property for this field is set to True. Enter a value in this field'.
..and the ItemID field is accordingly blank in the grid.
Is there a way to get Delphi/ADO to handle the behind-the-scenes two-table ItemID population as easily/neatly as Access does, without manually handling it programmatically? If not, what's the best/most elegant way to handle it programmatically?
I'd like to keep whatever solution I end up with as closely tied to the conventional TDataSet / TDataSource approach as possible, as I use a number of different kinds of databound controls, all of which will have to deal with this same data structure.
(Note: I'm using Delphi 2007 and an MSAccess 2000 format MDB file.)
Pretty much the same way. Theres's master source and master fields properties, so you just chain detail to master.
So mastersource would be Customer, Detail Source, Orders linked by CustomerID.
Dead easy to show this, but hard to explain.
Some other bloke wrote it all out though.
Master Detail Forms And Delphi
If you want to add new records to the tables via the grid, then you will have to use the BeforePost method of the underlying query in order to obtain the new key, which you then insert manually into the link table along with the category id.
I admit that I never write code with editable (and insertable) grids so my answer may need a little tweaking.

Resources