VBScripting Problem

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
pfischer
Junior Coder
Posts: 21
Joined: Wed Jul 23, 2014 2:07 pm

VBScripting Problem

Post by pfischer » Sun Jun 05, 2016 9:58 am

Dear Forum,
I have a curious problem with a VBSchript. I have a file with water temperatures for several days. These temperatures are measurerd every 4 seconds and logged in a single columns in the file. I excactly know when the first value was logged and now simply write a time stamp to each temperature value. For this, I start with the known time in the firts row and simply add 4 seconds to this date-time value to get the next timestamp. This works perfectly until the day switches from lets say the 4.july to the 5 of July. My script then gives me a runtimerrror VBScript 13, Typeconflict CDate.

My script looks pretty simple:
Let>startdatetime_new=%year%-%mon%-%day% %hour%:%min%:%sec%
VBSTART
VBEND
VBEval>CDate("%startdatetime_new%"),d
VBEval>DateAdd("s",4,"%d%"),enddate_endtime

I hardly cannot believe that the function CDate cannot handle a switch in a day?
Has anybody am idea what I am doing wrong?
Thanks a lot
Philipp

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: VBScripting Problem

Post by hagchr » Sun Jun 05, 2016 10:29 am

Hi, should not be a problem with the date change, eg

Code: Select all

Let>startdatetime_new=2016-05-31 23:59:58
VBSTART
VBEND
VBEval>CDate("%startdatetime_new%"),d
VBEval>DateAdd("s",4,"%d%"),enddate_endtime
MDL>enddate_endtime
IsDate can be used to check if valid date or not. Problem somewhere else in the script? How do you loop?

pfischer
Junior Coder
Posts: 21
Joined: Wed Jul 23, 2014 2:07 pm

Re: VBScripting Problem

Post by pfischer » Sun Jun 05, 2016 12:02 pm

Dear Marco,
thanks for the fast reply. It get even more curious with this script. I figured out that the problem only occurred when my AddTime value is 4 seconds. There is no problem in switching days when I use 1, 3, 5 or 10 sec. Only with 4 sec the problem occurs every time I run the script. I now made a small adaptation to the program that changes the AddTime value to 3 at 23:59:56 hours to manage the day switch and as soon as I am on the next day I change to 5 seconds for one time to compensate. With this small modification, the script is doing fine. I have no idea whats behind but it works.

However, I now test with IsDate if there is a problem with a wrong date format as soon as we switch the day. I do not see how this might happen but you never know. Thanks for that.

All the best
Philipp

pfischer
Junior Coder
Posts: 21
Joined: Wed Jul 23, 2014 2:07 pm

Re: VBScripting Problem

Post by pfischer » Sun Jun 05, 2016 12:03 pm

Dear Marco,
thanks for the fast reply. It get even more curious with this script. I figured out that the problem only occurred when my AddTime value is 4 seconds. There is no problem in switching days when I use 1, 3, 5 or 10 sec. Only with 4 sec the problem occurs every time I run the script. I now made a small adaptation to the program that changes the AddTime value to 3 at 23:59:56 hours to manage the day switch and as soon as I am on the next day I change to 5 seconds for one time to compensate. With this small modification, the script is doing fine. I have no idea whats behind but it works.

However, I now test with IsDate if there is a problem with a wrong date format as soon as we switch the day. I do not see how this might happen but you never know. Thanks for that.

All the best
Philipp

An add on: I now figured out the problem. The runtime error ocurresd when the time is summed up to 00:00:00 hours. When I adapt the script so that the case 00:00:00 hours cannot occur, then all is fine. Whe using the the AddDate function at time 23:59:56 hours and adding 4 seconds, VB obviously creates a time 00:00:00 hours which it cannot handle. When I avoid this data_time value all is fine.

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: VBScripting Problem

Post by hagchr » Sun Jun 05, 2016 3:19 pm

Hi, I was curious to see if I get a problem when looping through the temperatures, so came up with this code, which seems to work w/o problems. Not sure if an issue but the date/time stamp you are using only shows a date for midnight (time=0). If you instead split it using DateValue and TimeValue and put together again, then you will get a consistent date/time stamp also showing 00:00:00 (AM). Still a mystery though, but as they say, if it works don't fix it.

Code: Select all

//Read Temperatures into array arrTemp
LabelToVar>Temp,Temp
Separate>Temp,%CRLF%,arrTemp

//Get number of temperatures
Let>nm={%arrTemp_COUNT%-1}

//Get date/time for first data point into variable datetime
Let>datetime=2016-05-31 23:59:56
VBEval>CDate("%datetime%"),datetime

Let>res=
Let>ctr=0
While>ctr<nm
    Add>ctr,1
    
    //Create Date/Time Stamp
    VBEval>DateValue("%datetime%"),tmpdate
    VBEval>TimeValue("%datetime%"),tmptime
    Let>tmpStamp=%tmpdate% %tmptime%
    
    //Create String and add to result
    Let>tmpTemp=arrTemp_%ctr%
    Let>res=%res%%tmpTemp%    %tmpStamp%%CRLF%
    VBEval>DateAdd("s",4,"%datetime%"),datetime
EndWhile

MDL>res

/*
Temp:
68.3
67.9
73.2
80.5
100.8
90.8
102.5

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts