Converting Times

14 posts / 0 new
Last post
Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57
Hi Steve, here is some code that will perform what you are looking for. The first function will change your TIME_OF_ENTRY field to a character field (if it is already character then just comment this function out), the next step will add the virtual field and the final step will change the LOCAL_TIME field to a time field. I haven't added any varaibles to the script. I also changed the list separators to ; which I believe represent your regional settings. I have attached the script so you can download and modify to your liking.
Option Explicit

Sub Main
	Call changeTimeFieldToChar 'this will change the time field to a character field
	Call addVirtualField 'this will add a virtual field call LOCAL_TIME
	Call changeCharFieldToTime ' this will change the LOCAL_TIME field from char to time.
End Sub

Function changeTimeFieldToChar()
	Dim db As database
	Dim task As task
	Dim table As tabel
	Dim field As field
	
	Set db = Client.OpenDatabase("time test-Sheet1.IMD")
	Set task = db.TableManagement
	Set table = db.TableDef
	Set field = table.NewField
	field.Name = "TIME_OF_ENTRY"
	field.Description = ""
	field.Type = WI_CHAR_FIELD
	field.Length = 8
	task.ReplaceField "TIME_OF_ENTRY", field
	task.PerformTask
	Set task = Nothing
	Set db = Nothing
	Set table = Nothing
	Set field = Nothing

End Function

Function addVirtualField()
	Dim db As database
	Dim task As task
	Dim table As tabel
	Dim field As field
	Dim eqn As String
	
	Set db = Client.OpenDatabase("time test-Sheet1.IMD")
	Set task = db.TableManagement
	Set table = db.TableDef
	Set field = table.NewField
	eqn = "#LocalTime(TIME_OF_ENTRY; SUB_ADD; HOURS; MINUTES)"
	field.Name = "LOCAL_TIME"
	field.Description = ""
	field.Type = WI_CHAR_FIELD
	field.Equation = eqn
	field.Length = 10
	task.AppendField field
	task.PerformTask
	Set task = Nothing
	Set db = Nothing
	Set table = Nothing
	Set field = Nothing

End Function

Function changeCharFieldToTime()
	Dim db As database
	Dim task As task
	Dim table As tabel
	Dim field As field
	
	Set db = Client.OpenDatabase("time test-Sheet1.IMD")
	Set task = db.TableManagement
	Set table = db.TableDef
	Set field = table.NewField
	field.Name = "LOCAL_TIME"
	field.Description = ""
	field.Type = WI_TIME_FIELD
	field.Equation = "HH:MM:SS"
	
	task.ReplaceField "LOCAL_TIME", field
	task.PerformTask
	Set task = Nothing
	Set db = Nothing
	Set table = Nothing
	Set field = Nothing

End Function

 

Steve
Offline
Joined: 02/04/2014 - 11:25

Hi Brian,
thank  you again. Sorry for not replying yesterday. I had so much stuff going on that I haven't found the time.
I tested your Script and it worked. I only edited it a bit to increase the reusability of the code. Here is what I did:
 
<code>
Function ChangeTimeFieldToChar(IDM As String, Fieldname As String)
 On Error GoTo ErrHandler_ChangeTimeFieldToChar
 Dim db As database
 Dim task As task
 Dim table As tabel
 Dim field As field
 Set db = Client.OpenDatabase(IDM)
 Set task = db.TableManagement
 Set table = db.TableDef
 Set field = table.NewField
 field.Name = Fieldname
 field.Description = ""
 field.Type = WI_CHAR_FIELD
 field.Length = 8
 task.ReplaceField Fieldname, field
 task.PerformTask
 
 Client.CloseAll
 Set task = Nothing
 Set db = Nothing
 Set table = Nothing
 Set field = Nothing
 
 Exit Function
ErrHandler_ChangeTimeFieldToChar:
 If Err.Number = 0 Then
  Write #1, Now, " - ERROR - " , "ChangeTimeFieldToChar", Err.Number, Err.Description, "Error Not defined"
 Else
  Write #1, Now, " - ERROR - " , "ChangeTimeFieldToChar", Err.Number, Err.Description
 End If
 errorOccurred = TRUE
 'resume next
End Function
</code>
 
So thanks again for helping me so much:-)
Have a nice weekend!

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Not a problem Steve, making the code reusable is always a good thing.  You also have a great weekend.

Pages