1 (edited by CDB 2020-02-22 08:32:28)

Topic: [SOLVED] How to copy text from combobox to another form

I'm wanting to select an item in a combobox, then click on an 'edit' button which will open up a second form with the edit box filled in with the text of the item selected in the combobox. Subsequently that item will be edited and then saved back to the database.

I've tried the code below, but that is using the 'item ID' in the database which is not in the same order as the combobox.

It works, but displays the wrong item in the edit box, which of course means the wrong item gets edited and updated back in the database.

Should I change this to a SQL statement ?

procedure frmMain_btnEditSupplier_OnClick (Sender: TObject; var Cancel: boolean);
var
    RecordID:integer;
begin

    RecordID := frmMain.combobox2.ItemIndex; //get item number from combobox list

// check item ID for debugging
    showMessage('item no ' + intToStr(RecordID));

   //ShowRecord(frmEdit.,'supplier',frmMain.ComboBox2.ItemIndex );
   ShowRecord(frmEdit,'supplier',RecordID ); // Open edtForm with item selected ready to be edited and saved back into database.
end;
On a clear disk you can seek forever

Re: [SOLVED] How to copy text from combobox to another form

After posting this cry for help, I carried on trying various things and solved it with this.

procedure frmMain_btnEditSupplier_OnClick (Sender: TObject; var Cancel: boolean);
var
    RecordID:string;
    RecordNo:integer;
    supName,recordString,test: string;
begin
// check item ID for debugging
    RecordNo := frmMain.combobox2.ItemIndex;   // database ID number for text selected 
    supName := frmMain.combobox2.Text;  // Text of item selected in combobox
  
  //SQL test
    recordNo := SQLExecute('SELECT id FROM supplier WHERE sup_name= "'+ frmMain.ComboBox2.text + '"');
    test:= SQLExecute('SELECT sup_name, id FROM supplier WHERE sup_name= "'+ frmMain.ComboBox2.text + '"');

//start DEBUG message
    showMessage('Test= ' + test + ' Record No: ' + intToStr(RecordNo) + ', recordstring ' + recordString + ', supplier ' + supName);
 //end of DEBUG message

   ShowRecord(frmEdit,'supplier',RecordNo); // show form EDIT using table supplier and database recorrd ID
end;
On a clear disk you can seek forever

Re: [SOLVED] How to copy text from combobox to another form

CDB,
You are actually making it more complicated than it needs to be. You can do it without any script. See attached.  Also, you may be getting dbItemID and ItemIndex mixed up. dbItemID is the actual id in the table record. itemIndex is the sequential number of the selected item in the combobox starting with 0.

Post's attachments

Attachment icon Combobox Edit Form.zip 335.46 kb, 398 downloads since 2020-02-23 

4 (edited by CDB 2020-02-23 05:30:39)

Re: [SOLVED] How to copy text from combobox to another form

Thanks for the reply EHWagner,


I did try that initially, but it never correlated  the item list ID in the combobox with the ID in the database.


Example:


Combobox  list item ID.


itemIndex_1.  supplier A
itemIndex_2.  supplier B
itemIndex_3.  supplier C
itemIndex_4.  supplier D


Database item ID by row.


dbID_1.  supplier A
dbID_2.  supplier D
dbID_3.  supplier C
dbID_4.  supplier B 

This is taken from the actual contents of my database and the item list in the combobox which is sorted by ascending name.

On a clear disk you can seek forever

Re: [SOLVED] How to copy text from combobox to another form

They will not correlate because they are two completely different properties with two distinct values used for different purposes. dbitemID is the actual id in the record whereas listindex is the number of the selection in the combobox. ListIndex has nothing to do with the record id (dbItemID) in the database.


So in your first example you should be using dbItemId and not ListIndex as follows:

procedure frmMain_btnEditSupplier_OnClick (Sender: TObject; var Cancel: boolean);
var
    RecordID:integer;
begin

    RecordID := frmMain.combobox2.dbItemID; //this gets the actual record id from the selected item in the combobox and needed for your ShowRecord.

// check item ID for debugging
    showMessage('item no ' + intToStr(RecordID));

   //ShowRecord(frmEdit.,'supplier',frmMain.ComboBox2.ItemIndex );
   ShowRecord(frmEdit,'supplier',RecordID ); // Open edtForm with item selected ready to be edited and saved back into database.
end;

But you really do not need this code at all  because you can do it without any script whatsoever.

Re: [SOLVED] How to copy text from combobox to another form

ehwagner,


I'll give it another try, and see if I can see why it didn't work for me the first time around. Less code is always better! smile

On a clear disk you can seek forever

7 (edited by CDB 2020-02-24 08:41:07)

Re: [SOLVED] How to copy text from combobox to another form

I see how my table options are different to the demo program you kindly provided.


I have 3 tables currently and  the table that links to the supplier table has a foreign key in it and is marked as a dictionary. When I  open the 'ForeignKey' property I only get the option of the linked ID, unlike your program that access the 'name' field directly.


I think this might be why I can't get my design to work the same as your design.

Post's attachments

Attachment icon dbschema.PNG 79.06 kb, 135 downloads since 2020-02-24 

On a clear disk you can seek forever

8 (edited by derek 2020-02-24 18:22:22)

Re: [SOLVED] How to copy text from combobox to another form

Hi CDB,
I had a look at your project and there seems to be a lot of script that is replicating functionality that you can achieve using standard MVD.  It may be that you are intentionally doing this so you can familiarise yourself with the scripting language (which is fair enough) or perhaps it's because there are some standard features that you might not be aware of.
With that in mind, I took your project, tried to simplify it, fixed a couple of things that weren't working and then replicated as much of the functionality that (I think) you're after with standard MVD whilst using as few lines of script as possible (in the end, all my script does is colour alternative lines of the grid and pre-populate the 'copy order' fields).
As with most things in MVD, there are a few different ways of doing the same thing, so the attachment is very much some suggestions rather than the 'definitive' way.
Hopefully, it might give you some ideas.
Regards,
Derek.

Post's attachments

Attachment icon prq2.zip 343.11 kb, 393 downloads since 2020-02-24 

9 (edited by ehwagner 2020-02-24 22:26:06)

Re: [SOLVED] How to copy text from combobox to another form

CDB,
In the ForeignKey field property, do not select anything from the dropdown. Just type in the table name Supplier, then you can select the Sup_Name from the FieldName dropdown.


EDIT - I am updating my post to support Derek's approach. I was wondering where he got your project and then I saw you had it in another post. After looking at your original project, in my opinion, it would better serve you to use Derek's approach. It is easier, more straightforward and less scripting. It appears you are trying to use tabs rather than forms for various updates and it can become cumbersome with scripting to try to do a lot of things on one form. Not that you can't do it, but it can be complex. In Derek's project, you could almost simulate tabs by placing the buttons above the tablegrid on the first form.  Just a footnote, I typically only use tabs if I have a lot of fields in a record and there are too many to fit on a form. I can spread them in a logical way on separate tabs, but on the same form. Obviously it is not the only use for the tabs, but I just wanted to show you one way for its use.


Hope this helps with your development.

10 (edited by CDB 2020-02-25 10:52:36)

Re: [SOLVED] How to copy text from combobox to another form

Derek and ehwagner,

Thank you both for your help.


1. I hadn't realised  I didn't have to accept the foreign key offered by MVD and could substitute my own entry from a database table.


2. I rather like tabs instead of forms popping up, but Derek's solution is much simpler and less hassle. So, that will save me from writing some code to auto-adjust the column widths of the table grid if the user resizes the form.


This project is in fact the precursor to a far more enhanced version.


Sorry for the extra work I have put you and other others too, but I'm the sort of person that has to understand how a program works, plus I'm more used to from a coding point of view  writing in ASM or Pascal/C for embedded microcontrollers and I tend to make all my own code functions, hence procedural coding rather than objective coding.


I shall take Derek's suggestion, make a couple of slight alterations and then add in a report form so I can email  all parts ordered grouped by supplier, though I suppose I could do that using the print function and print to PDF.


I'll have to ponder on that.

On a clear disk you can seek forever