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
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
Hi brian,
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
Hi 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
Hi
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.
This is getting strange. The
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
Hi Derek,
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
Hi 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
Hi ,
Hi ,
In IDEA 10, I try to used this function, but it seems can't rename the field for field type is Number type.