VB Script Editor CDate
I'm trying to write a script to do a calcuation of expiration date inside a label.
The data file (.bt) file I'm provided with has the MfgDate and the shelf life in months on it. The fields are strings. The calculation is MfgDate + Shelf life in months = Expiration date. The Expiration date will be printed on the label.
When trying to use multi-line script I'm lost. I can't seem to get the value of the field into a variable to manipulate.
This will get the value of the field on the label so I know the field is populated with a valid date string.
Since I know it's a string, I need to parse the date out of it for the calculation. The date happens to be in the format of MM/DD/YYYY
If I type in something like "2023-04-26" then CDate works fine! Why can't CDate parse the date out of my field?
ultimately, I need to extract the date, add the months to it with the DateAdd function and then format it back to a format I can use such as nQYY where N is the quarter, 'Q' is the literal character Q and then YY is the two digit year. Something like 2Q33 for second quarter 2033 as expiration.
-
Peter Thane
★ BarTender Hero ★
Have you tried adding your database MfgDate onto the label as a text field, then set the Data Type to date and if that works then use the Transforms>Offset and link the offset to the field in the Database that has the months in it?
So you get something like this (I am using fixed fields rather than Database fields but the theory is the same). Note: if you dont need to actually see this date on the label, just drag the field off to onside so that it can be referenced by other fields later.
For the VB field to get the Q value you will need to set this as VB Script>Event Controlled Script I would suggest probably OnPostPrompt or OnIdenticalCopies to make sure the VB is processed after the Offset has been applied.
I would suggest making the modified date field a Named Data Source (via the button shown below)
The VB Script I used is as follows
Value = CDate(Format.NamedSubStrings("TheDate").Value)
Value1 = Month(Value)
If Value1 < 4 then
Value1 = 1
elseif Value1 < 6 then
Value1 = 2
elseif Value1 < 9 then
Value1 = 3
else Value1 = 4
end if
Value2 = Year(Value)
Value2 = Right(Value2,2)
Value = Value1 & "Q" & Value2
And for the first line I typed in the Value = CDate( and then double clicked on the Named Data Source from the column on the right to get BarTender to add this link and then added a ) to the end.
0
Please sign in to leave a comment.
Comments
1 comment