How to copy data fields in Postgresql lazarus / 16-Databases ---------------------------------------------------------------------- jptferreira · 2013-10-20 19:49 I'd like to know the best way to copy a data field into another... the best way without having to declare variables to read and then write. The purpose is to be able to not have to re-type the same content from the shipping address field into the billing address field if it is the same. Still learning the best way to have optimized code and expect for some experts help! Thanks! ---------------------------------------------------------------------- howardpc · 2013-10-20 20:48 Would something like this work for you? Code: [Select] uses SysUtils;  if not SameText(ShippingAddressField.AsString, BillingAddressField.AsString) then    BillingAddressField.AsString:=ShippingAddressField.AsString; ---------------------------------------------------------------------- jptferreira · 2013-10-21 13:43 That makes sense and I like it. Lets see how it work and will reply with the result. Thanks! JP ---------------------------------------------------------------------- jptferreira · 2013-10-21 16:34 That works but forces me to assign variables because  AsString works directly with the memory and not with the database field. Any other ideas? Thanks ---------------------------------------------------------------------- jptferreira · 2013-10-21 16:56 This is more like what I want to be able to get but because I'm a novice with Pascal I'm now doing research and asking for help:  billaddr := CustSQLQuery.Fields.FindField ('address1'); CustSQLQuery.FieldByName('billaddress1').AsString := ('billaddr'); However this assigns the "billaddr" into the billaddress1 database field.. and not the data contained in the address1 Any help on this would very appreciated! Thanks JP ---------------------------------------------------------------------- BigChimp · 2013-10-21 17:11 Quote from: jptferreira on October 21, 2013, 06:56:52 pm billaddr := CustSQLQuery.Fields.FindField ('address1'); CustSQLQuery.FieldByName('billaddress1').AsString := ('billaddr'); However this assigns the "billaddr" into the billaddress1 database field.. and not the data contained in the address1 Haven't tested this... Code: [Select]billaddr := CustSQLQuery.Fields.FieldByName ('address1').AsString; CustSQLQuery.FieldByName('billaddress1').AsString := billaddr; ---------------------------------------------------------------------- jptferreira · 2013-10-21 19:57 Good information, however I got another issue. The Incompatible types : got "AnsiString" expected "TField" Should this be a procedure setting? I've the procedure set for TObject and understand we are using AsString. This is the only thing at the moment that is messing with my intellect... however I'm enjoying pascal and mostly Lazarus! Thanks for any help on this! JP ---------------------------------------------------------------------- BigChimp · 2013-10-22 09:19 Quote from: jptferreira on October 21, 2013, 09:57:44 pmGood information, however I got another issue. The Incompatible types : got "AnsiString" expected "TField" Quote from: jptferreira on October 21, 2013, 09:57:44 pmShould this be a procedure setting? I've the procedure set for TObject and understand we are using AsString. I have no idea what this means... a "procedure setting", or "a procedure set for TObject"? Always best to show relevant details - useful in other posts as well: 1. Show the error message as is (you can right click on the error in the messges window, and copy it) 2. Show the code, including variable declarations, that belongs to the line you got from 1. However, my guess from your quotes and previous code would be that you had something like: Code: [Select]var   billaddr:TField; // or even TObject?? ... billaddr := CustSQLQuery.Fields.FieldByName ('address1').AsString; CustSQLQuery.FieldByName('billaddress1').AsString := billaddr; while you would need Code: [Select]var   billaddr: string; ... billaddr := CustSQLQuery.Fields.FieldByName ('address1').AsString; CustSQLQuery.FieldByName('billaddress1').AsString := billaddr; Of course, if you are using billaddr somewhere else and assign a TField object to it, it's probably best to split things out and use another string variable. I'd like to leave you with something to ponder though: What does your field address1 represent? I presume customer's physical address, presumably shipping address? Billaddress1 presumably is the billing address? You are copying data to the billaddress1 field more or less by default. Instead of doing that, why not leave billaddress1 NULL unless the 2 addresses differ and use a PostgreSQL view to represent billing address that selects either address depending on which one is NULL? This way, whenever an address changes, you don't need to change billaddress1 as well, make sure it is the same, deal with typos etc. (The 1s at the end of these field names make me a bit queasy too... but I design my relational databases quite normalized and not everybody prefers that) ---------------------------------------------------------------------- dgaspary · 2013-10-22 21:24 Quote from: jptferreira on October 20, 2013, 09:49:17 pmThe purpose is to be able to not have to re-type the same content from the shipping address field into the billing address field if it is the same. If the data is already saved at the database, you can use INSERT / SELECT: INSERT INTO table1 (field1, field2, address) SELECT field1_new_value, field2_new_value, old_address_value FROM table1 WHERE ... ---------------------------------------------------------------------- jptferreira · 2013-10-23 13:07 BigChimp, You are correct about your questions. I apologize for not being as clear as I would like to. I'm Portuguese and sometimes my realtime translation (in my brain) doesn't go to the keyboard as I plan. To answer the first question:  "procedure setting", or "a procedure set for TObject" - I meant to say procedure set for TObject. I'm not using the billaddr anywhere else I commited a huge distraction (mistake), I didn't pay attention to the var, I didn't declare it as a string type and that was my problem. It was declared as a TField. dgaspary, thanks a lot for your tip. Good info to keep! Now it works great!  You guys helped this tired mind and I'm very grateful. Thanks for your constant care to help. Best regards.