Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy Field to other Field

Status
Not open for further replies.

Movi28

Systems Engineer
Feb 12, 2021
5
CH
Hi Everybody,

I have a little project on a old ERP Software or you can execute scripts in foxpro.
I need to copy a value from a field to a other field.
My script:
Code:
tableOpen('documents')
REPLACE ALL DO_REF6 WITH DO_NODOC WHERE DO_TYPE = 12 AND DO_CLASSIF = 3 ;

Any idea how can I do that ?

thanks !
Vince
 
Are the two fields (the source field and the target field) in the same table? And do you want to copy from source to target in the same record? And are DO_TYPE and DO_CLASSIF also in that same record?

If the answers to all three questions is Yes, then the code that you posted should do what you want, except that you should change [tt]WHERE[/tt] to [tt]FOR[/tt], and also remove the [tt]ALL[/tt]. Also remove the semi-colon. So in other words:

Code:
REPLACE DO_REF6 WITH DO_NODOC FOR DO_TYPE = 12 AND DO_CLASSIF = 3

But if that's the case. why would you want to do that? You will end up with two copies of the same data in the same record. You might have a good reason for doing that, but off-hand I can't think of any.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I think we would need more information

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Hello

thanks !

Yes on the same Table the for field: DO_NODOC, DO_REF6, DO_CLASSIF and DO_Type
But I think that DO_NODOC and DO_REF6 is not the format maybe ? (Error msg is data type not compatible)
pDMgPzPaQL_po5jrr.png


Because we use an api that can modify only if the DO_REF6 field is filled in. The only way to fill DO_REF6 is to do it with a script


regards
Vince
 
Movi28,

You have unmatching field types. Do_ref6 is a character field, do_nodoc a numeric field. You well need the transform function, with parameters if you have decimal values in do_nodoc.

So you need something like “replace ... do_ref6 with transform(doc_nodoc) where ...

Regards, Gerrit
 
I am not sure whether ALL is wrong. By default, REPLACE only works in the current record. If you want to do this for all records that match the criteria, ALL is the correct scope. But then an SQL Update is the simpler alternative, as ALL records are its default scope and you usually limit this by a WHERE clause.

Chriss
 
Chris, I agree that ALL is not wrong, but it is misleading. In this case, the REPLACE won't apply to all the records, but only those specified in the FOR clause. The FOR overrides the ALL. But that might not be obvious to someone reading the code. That's why I suggested removing it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Ah, okay, Mike. I always think it's good to denote the possible scope of a command and use ALL and FOR in combination, to me that's unmistakably stating to check ALL records FOR the condition (I tend to think of 'all for good'), but a FOR implicates change of scope from the current record to all records fulfilling the conditions, right. So you only use ALL in case you mean literally ALL records?

Chriss
 
Hello,
Thank you very much for your answers! I will test this tomorrow and get back to you. Thanks
 
I'm with Mike here. Using ALL with FOR is confusing. FOR means every record that matches.

Tamar
 
Hello everybody,

I go testing and everything works perfectly! thank you thank you thank you for your help! [peace]

Code:
tableOpen('documents')
REPLACE documents.DO_REF6 WITH transform  (DO_NODOC) FOR DO_TYPE = 12 AND DO_CLASSIF = 3
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top