Skip to main content

Set a field as variant in an equation

Hi,
I am trying to automate some analysis and I don't know how to fix the following code in order to include the field as variant because I have several fields in where I need to adjust the format. Can someone help me to insert the variable field in my equation editor? This is where my code stop working, I have copied it here! Thanks
Function ModifyAllDateField (DateField, WorkingDatabase) As Variant
            Dim db As Object
            Dim task As Object
            Dim field As Object 
           
            Set db = Client.OpenDatabase(WorkingDatabase)
            Set task = db.TableManagement
            Set field = db.TableDef.NewField
           
            'Users DateField variable passed to the function to set which field is checked
            field.Name = DateField
           
            ' Checks if the field is already a date field, if it is, do nothing
            ' If the field is not a date field, then change it to a date field (saves processing time)
            If field.IsDate Then
                        Set Task = Nothing
                        Set db = Nothing
                        Set field = Nothing
            Else
                        field.Name = DateField & "_CORRECT"
                        field.Description = ""
                        field.Type = WI_VIRT_DATE
                        field.Equation = "@ctod(DateField, ""DD/MM/YYYY"")"
                        task.AppendField field
                        task.PerformTask
                        Set task = Nothing
                        Set db = Nothing
                        Set field = Nothing
                       
            End If
           
 
End Function
 

Brian Element Fri, 03/13/2020 - 12:39

You need to change the equation as you are using the variable inside of the equaiton so the equation is seeing sDate and not the contents of the variable.

Change it to:

field.Equation = "@If(FECHA_PAGAR<=""" & sDate & """;@Age(FECHA_PAGAR;""" & sDate & """);0)"

Remember that dates need to be within double quotes in the equation, if there are no double quotes then the equation editor will think it is a number and not a date.