1 (edited by papafrankc 2020-10-09 04:28:37)

Topic: Allow user to add data to a combobox

Hi All,
I have an Equipment Type combobox with a number of 'Types' that a user can choose from.
One of my users would like to be able to add his own 'Types' to my list.
I've been looking to see if a person can add to a combobox but haven't found anything so far.
-
Do you have any ideas to help me?
-
Thanks, Frank

Re: Allow user to add data to a combobox

Hello papafrankc

Beside  Equipment Type, combobox place a button (with as icon agreen cross) .

Clicking on this button displays a form from which the user enters a new type that he saves.
This supports a base for the types of equipment (i.e. Equipments), an EditBox to enter the new value and a Save button.

Can this help you ?

JB

Re: Allow user to add data to a combobox

Jean,
It sounds good to me.  I'll see about implementing it tomorrow and let you know how it goes.
Thanks, Frank

4 (edited by CDB 2020-10-09 07:54:16)

Re: Allow user to add data to a combobox

You could also try this.

{function to allow selected comboboxes to have new data added to them directly}

function add2CategoryBox (Sender: String;  indx: Integer): Integer;
begin
   if (indx = -1)  AND (Sender <> '') then
       begin
         SQLExecute('INSERT INTO table_name (field) VALUES ("'+Sender+'")');
         UpdateDatabase('table_name');
         Result := Last_Insert_id;
         showmessage('Categories updated with '+Sender )
      end
      else begin
       Result := indx;
   end;


end;

Example calling code: Substitute your form and combox names.

frmEditPart.cmbEditCategory.dbItemID := add2CategoryBox(frmEditPart.cmbEditCategory.text, frmEditPart.cmbEditCategory.dbItemID)
On a clear disk you can seek forever

5 (edited by papafrankc 2020-10-10 05:58:36)

Re: Allow user to add data to a combobox

Jean,

I followed your suggestion and it's working OK.
I put an Add button on a small form and it lets the user add a new entry.
I thought about mistakes - spelling...etc so I added an edit button and it allows me to edit a chosen entry.
-
And using Edit button I can even erase an entry if I don't want it.
However when I erase the combobox entry, it leaves a Blank record. And if I do that a few times then I wind up with a number of Blank records at the top of my combobox.
I added a Delete button to my Edit Form and it will let me delete some records but not others???
Here's the error I'm getting when it won't let me delete a record:
FOREIGN KEY constraint failed
Component: frmEquipType.TypeTableGrid

-
Do you have any ideas on how to remove the blank record(s)?
-
CDB,
I've only had a chance to take a quick look at your suggestion.
I'll check it out & see if it might help with my Blank record issue.
-
Thanks, Frank

Re: Allow user to add data to a combobox

Hello papafrankc

Glad to have been able to help you

About empty entry in the combobox ::

In my projects, when I delete an item (mostly when I use SQL statement, I always add a Form1.ComboBox1.dbUpdate;(your form and you combobox names).

JB

7 (edited by derek 2020-10-10 15:44:57)

Re: Allow user to add data to a combobox

Hi Frank, JB, CDB,
If you erase the text of a combobox entry, you aren't deleting it, you're just amending it.  However, if you want to prevent blanks from appearing in your combobox dropdown list,  you can always add a filter to your combobox (see screenshot1 in the attachment).
The 'foreign key constraint' error message is because you are trying to delete an equipment type that is being referenced by data in another table.  You can always add a query somewhere in your program to show you which equipment types are not being used at and can therefore safely be deleted (see the 'equipment type usage' option in the attachment as a suggestion).
As a general question, is there a reason why you can't just call a form with an editable grid when you want your user to be able to maintain the list of equipment types (again, see the attachment - cliick on the 'Equipment Type' label in Form2) or is that an over-simplification)?.  It has the advantage of being applicable irrespective of whether you have your combobox configured to be 'searchable' and/or 'first item empty', and MVD takes care of updating the combobox dropdown list when you return to the calling form.
Regards,
Derek.

Post's attachments

Attachment icon equipment01.zip 481.82 kb, 235 downloads since 2020-10-10 

Re: Allow user to add data to a combobox

Hi All,
Thanks to everyone I've got it working pretty much the way I want.
Here's what I've implemented;
- I put an ADD and EDIT button next to my field label
- The ADD button calls a one field form where the user can add a new TYPE
- The EDIT button calls a form with a GRID where they can edit the entries.
   If they erase an entry it will leave a blank field.  But that's OK because I used the filter to prevent any Blank fields from showing up in the TYPE combobox.
-
In my application I think most folks will just go with the default TYPES I put into the program. So I don't think the Editing will be used much.
-
I will probably add this feature to my COUNTRIES combobox dropdowns because I don't have ALL of the possible countries in my default list.
-
Thank you all for your help. It's been another good learning experience for me smile
Frank

9 (edited by derek 2020-10-10 22:58:26)

Re: Allow user to add data to a combobox

Hi Frank,
Just read your post and thought I'd mention that you don't actually need to use a tablegrid to select the record for editing - the 'edit' button (show record) can use the combobox instead (see form2 button3 in the attached).   That way, you can then use the same one field form for both adding and editing so the interface is more consistent for the user.
Derek.

Post's attachments

Attachment icon equipment02.zip 336.79 kb, 250 downloads since 2020-10-11 

Re: Allow user to add data to a combobox

Derek,
Thanks for the suggestion, it works great.  Much simpler than what I was doing.
-
I didn't know that the SHOW RECORD could use a combobox.  It asks me to choose a tablegrid so I thought that was all it could use.  Geeeez, so much to learn!
-
Thanks, Frank