Skip to main content

renameFields Function

Function: renameFields
Purpose: To rename fields within a file
Input:    sTempFilename: the name of the imd file that contains the fields to change
             sOldFieldName: The name of the field that is to be changed
             sNewFieldName: The new field name
Returns: True if fieldname was changed False if the field name could not be changed
Problems: Currently does not work for some Editable fields

Download

Snippet
Option Explicit
Sub Main
	If Not renameFields(sFile, OldFieldName, NewFieldName) Then
		MsgBox "The field could not be renamed"
	End If
	
End Sub


'*****************************************************************************************************************************************
'* Function: renameFields
'* Purpose: To rename fields within a file
'* Input:	sTempFilename: the name of the imd file that contains the fields to change
'* 	sOldFieldName: The name of the field that is to be changed
'*	sNewFieldName: The new field name
'* Returns: True if fieldname was changed False if the field name could not be changed
'* Problems: Currently does not work for some Editable fields
'*****************************************************************************************************************************************
Function renameFields(sTempFilename As String, sOldFieldName As String, sNewFieldName As String) As Boolean
	On Error GoTo ErrorHandler
	Dim db As database
	Dim table As table
	Dim task As task
	Dim field As field
	Dim newField As Object
	Dim iFieldType As Integer
	Dim sDescription As String
	Dim iDecimals As Integer
	Dim bImpliedDecimals As Boolean
	Dim iLen As Integer
	Dim sEqn As String
	
	Set db = client.openDatabase(sTempFilename)
	Set table = db.tabledef
	Set field = table.getfield(sOldFieldName)
	iFieldType = field.Type
	
	'obtain the old info
	sDescription = field.Description

	Select Case iFieldType
		Case WI_NUM_FIELD, WI_VIRT_NUM, WI_BOOL, WI_MULTISTATE  
			iDecimals = field.Decimals
			bImpliedDecimals = field.IsImpliedDecimal
			sEqn = field.Equation
		Case WI_CHAR_FIELD
			iLen = field.Length
		Case WI_VIRT_CHAR
			iLen = field.Length
			sEqn = field.Equation
		Case WI_DATE_FIELD, WI_TIME_FIELD, WI_VIRT_DATE, WI_VIRT_TIME
			sEqn = field.Equation
			
	End Select
	
	Set db = Nothing
	Set table = Nothing 
	Set field = Nothing

	'change the field name
	Set db = client.openDatabase(sTempFilename)
	Set task = db.TableManagement
	Set table = db.tabledef
	Set newField = table.NewField
	
	newField.Name = sNewFieldName
	newField.Description = sDescription

	Select Case iFieldType
		Case WI_NUM_FIELD, WI_VIRT_NUM
			If iFieldType = WI_NUM_FIELD Then
				newField.Type = WI_NUM_FIELD
			Else
				newField.Type = WI_VIRT_NUM
			End If
			newField.Decimals = iDecimals
			newField.IsImpliedDecimal = bImpliedDecimals
			newField.Equation = sEqn
		Case WI_CHAR_FIELD
			newField.Type = WI_CHAR_FIELD
			newField.Length = iLen
		Case WI_VIRT_CHAR
			newField.Type = WI_VIRT_CHAR
			newField.Length = iLen
			newField.Equation = sEqn
		Case WI_DATE_FIELD
			newField.Type = WI_DATE_FIELD
			newField.Equation = sEqn
		Case WI_VIRT_DATE
			newField.Type = WI_VIRT_DATE
			newField.Equation = sEqn
		Case WI_TIME_FIELD
			newField.Type = WI_TIME_FIELD
			newField.Equation = sEqn
		Case WI_VIRT_TIME
			newField.Type = WI_VIRT_TIME
			newField.Equation = sEqn
		Case WI_BOOL  
			newField.Type = WI_BOOL  
			newField.Decimals = iDecimals
			newField.IsImpliedDecimal = bImpliedDecimals
			newField.Equation = sEqn
		Case WI_MULTISTATE 
			newField.Type = WI_MULTISTATE 
			newField.Decimals = iDecimals
			newField.IsImpliedDecimal = bImpliedDecimals
			newField.Equation = sEqn

	End Select
	
	task.ReplaceField sOldFieldName, newField
	task.PerformTask
	
	renameFields = true
	
	GoTo ReleaseVariables
	
ErrorHandler:	
	renameFields = false
ReleaseVariables:
	Set task = Nothing
	Set db = Nothing
	Set table = Nothing
	Set field = Nothing
	Set newField = Nothing
	
	
End Function

 

Derek Wed, 08/16/2017 - 05:02

Hi ,
In IDEA 10, I try to used this function, but it seems can't rename the field for field type is Number type.

Brian Element Wed, 08/16/2017 - 07:38

Hi Derek,

I think I found the problem.  Redownload the file and try again.  I just had it working for me in 10.2 for a numeric field.  Let me know if you find anything else.

Thanks

Brian

Derek Wed, 08/16/2017 - 11:22

In reply to by Brian Element

Hi Brian,
Thanks for so quick reply my question.
I redownload the file and found what you modify. But the problem still not solve in my script when used the function. Btw, my IDEA version is 10.1.6.3 (X86 Unicode).
Thanks again
Derek
 

Brian Element Wed, 08/16/2017 - 18:08

That is strange, unfortunately I don't have the unicode version of IDEA so I can't ask you for a copy of your code.  Can you do a screen capture of the error and post it here for me to see, maybe that will tell me something.

Thanks

Brian

Derek Wed, 08/16/2017 - 23:26

Hi brian,
I screen capture some pictures with simple descriptipn, hope those information could help to you find my problems.
PS: Because here can't uploade word or pdf file, so you can download the pdf file form http://gofile.me/29Mug/KJ1U10oCZ
Thanks,
Derek

Brian Element Thu, 08/17/2017 - 08:43

In reply to by Derek

Hi Derek,

Thanks for the pdf file, that helps out.  So from what I can tell it does work on OGA00 numeric field changing it to FIELD01 and it works on the character fields.  So where I would first look is to see if there is a problem with capturing the FIELD05 and FIELD06 drop-downs. Did you try this on other files and did it work properly for FIELD05 and FIELD06?

Thanks

Brian

Derek Thu, 08/17/2017 - 09:19

Hi Brian,
 
FIELD05 & FIELD06 could work, if the field type is not numeric.
Thanks,
Derek

Brian Element Thu, 08/17/2017 - 09:45

In reply to by Derek

Hi

Derek,

Right now I am at a loss why it is not working.  I just tried it out again.  I used the Sample-Detailed Sales.IMD filed.  This is the Field Manipulation before the script:

There are 5 numeric fields.

I created a script to loop through the fields and if they are numeric to change their name.

In the field manipulation it shows that all the numeric fields have been renamed:

When you are running the script does it give any errors or it just doesn't rename the numeric fields?  Also can you rename one of the fields and make a cope of the IDEAScript in the history, I want to see if there is anything that looks different from my code.

Derek Thu, 08/17/2017 - 10:02

In reply to by Brian Element

Hi Brian,
When I run my Script, it does not show any error, it just doesn't rename the numeric fields.
and below is history about I changed the numeric field name OGA24 to OGA24X
Thanks,
Derek

Derek Thu, 08/17/2017 - 09:42

Hi Brian,
I find a strange thing. If I change the field name that the field type is numeric and run my program it could work in numeric field.
Thanks,
Derek

Brian Element Thu, 08/17/2017 - 10:09

This is getting strange.  The next thing is to try and figure out what is going on in the function and why it is not changing the code.  Can you add the following line after the iFieldType = field.Type in the renameFields() function.  This way you can see if the proper field names are being sent to the function and what type IDEA thinks it is.  If you could let me know what it shows for the ones that don't get changed it might help out to understand what is going on with the code.

MsgBox "Old Field Name: " & sOldFieldName & " New Field Name: " & sNewFieldName & " Field Type: " & iFieldType

Brian Element Thu, 08/17/2017 - 10:41

Hi Derek,

I am really at a loss to what is going on here.  The script works on one field but not on the other but will work if you change the name, that doesn't make sense.

If you try changing the name of one of the problem fields using your modify field script above does it work.  Now I am wondering if directly modifying the field name will work or not.

Thanks

Brian

Derek Thu, 08/17/2017 - 10:51

In reply to by Brian Element

Hi Brian,
No matter I used above script (ModifyField) or directly modifying the field name , it is work.
Thanks,
Derek

Brian Element Thu, 08/17/2017 - 11:01

In reply to by Derek

Hi Derek,

I am really at a loss right now about why it is not working.  It works on some fields not but others, it works when you change the field name, etc.  Unfortunately you have the unicode file so I can't ask you for a copy of a portion of it to play with it to see if I can figure it out.  So right now I am at a loss how to get it working.

Brian

Derek Thu, 08/17/2017 - 11:12

Hi Brian,
Thanks for your help again. For now, I think maybe it's my source file cause this problem, I will try to find out what happened. If I find out what happen, I will let you know.
Thanks again,
Derek

Brian Element Thu, 08/17/2017 - 11:15

In reply to by Derek

If you find anything out I would appreciate it.  I don't like mysteries like this :-)

Good luck on your project.

Brian

stephen Tue, 09/19/2017 - 14:43

Hi Brianm
Please I have many files i want to append but the field types  are not different.  Please help with script that with append the files irrespective of the field types.