# Vb Script On Calculation フォローする Legacy Poster

Hi.I'm currently having problem solving this kind of issue using vb script bartender.Below is the detail of my scenario.

Scenario:
I am using bartender to help me printing labels.
The label has many field data,but i think i would list out the main thing i'm going to do.

Box:<Empty> Of:<Empty>

Total quantity : <Empty>
Qty:<Empty>

When it prints,it will prompt out a dialog for the users to key in the info.
Let say that if it has 5 box, it will print out 5 labels with serialize data for box (i.e 1 of 5,2 of 5,3 of 5,4 of 5,5 of 5) (which i already done this part)
Problem is when the user put in the total quantity value,eg 18,
it should be able to print out 5 label with qty value 4 for each label except the last label value is 2 (i.e 4+4+4+4+2)

How can i use vb script to help me solve this? especially the last part.
Thanks for reading and any help would be really appreciate.

##### 14 コメント ###### Legacy Poster

Hi all, maybe my explaination isn't detail enough.

When the user enters the number of box and total quantity, it will auto-calculate how many quantity that would be stored in a box.For example,

If the user key in number of box = 4 ,total quantity = 20 ,it will print out 4 pieces of label with quantity=5 each,

If the user key in the number of box =6 and total quantity = 23, it will calculate 23/(6-1) = 4 with remainder 3
That means that it will print out 6 label,with the first 5 label 4 quantity and the last label with the remainder 3.(4+4+4+4+4+3)

Thank you.I hope i can get any help here. ###### Gene Henson

Hello,

Below I have prepared a VB script that will accomplish what you desire. First, I need to explain a couple of things that will be necessary on the label format in order for this to work correctly. Some of them you already have, but I will explain everything as if you were creating it from scratch.

You will need to create three label objects and a data entry prompt for the number of serialized labels.
[list=1]
[*]The first object is just a regular text object that is set to serialize. I've given it a name called "SerializedNumber". (This can be placed off the label so it doesn't print.)
[*]You will need a text object to hold the total quantity. I've given it a name called "TotalQty" and I've set up a data-entry prompt for it. (It can also be placed off the label if desired.)
[*]You will need a text object for the VB script that will display the box quantity. This should be set to an Event Control Script. (This could also be placed off the label, but I imagine you want it to be visible.)
[*]You will need to setup a data-entry prompt for the Number of Serialized copies.
[/list]
Once you have all of that setup, you can put the following script in the VB Script for the object you created in step 3 above. You will place this in the "OnSerialize" event in the Event List.
[code]
IF Format.NamedSubStrings("TotalQty").Value MOD Format.NumberSerializedLabels = 0 THEN
Value = Format.NamedSubStrings("TotalQty").Value / Format.NumberSerializedLabels
ELSE
IF CInt(Format.NamedSubStrings("SerializedNumber").Value) < CInt(Format.NumberSerializedLabels) THEN
Value = Int(Format.NamedSubStrings("TotalQty").Value / (Format.NumberSerializedLabels - 1))
Else
Value = Format.NamedSubStrings("TotalQty").Value - ((Format.NumberSerializedLabels - 1) * Int(Format.NamedSubStrings("TotalQty").Value / (Format.NumberSerializedLabels - 1)))
End If
End If
[/code]
I'll walk you through the code a little bit so you can see what it's doing.
[list]
[*]First, if the TotalQty is evently divisible by the NumberOfSerializedLabels, then we just divide the two numbers and place it on each label.
[*]However, if it is not evenly divisible, then we make a check to see what label we are currently creating (1 of 3, 2 of 3, 3 of 3, etc).
[*]If we are NOT creating the last label, then we do the same division that we did above, and then we strip off the decimal (by setting it to an Integer data type).
[*]If we ARE creating the last label, then we subtract the total of all of the labels that we already created from the TotalQTY, and place that remainder on the last label.
[/list]
This script worked for all of the examples that you mentioned above. Let us know if you have trouble setting it up, and whether or not it actually gives you what you are looking for. ###### Legacy Poster

Hi Gene H,

Thanks for the reply and i am really eager to try it out.

I have followed the steps and tried to put the VB script on the "OnSerialize" event just as you mentioned.But it prompts out Bartender Error Message.

OnSerialize(Line 3)::Type mismatch:'[string:""]'

Is it i miss something on the previous steps? The name for the object (i.e SerializedNumber and TotalQty),we name it at the share/name(beside the screen data tab at advanced) at data source there,is it?

Thanks. ###### Gene Henson

Hello,

I suspect one of your objects has text as the default data. This causes the script to fail since it can't convert text (something like "Sample Text") to an Integer. You can look through your objects to see if one of the shared values is actually passing a string instead of a number.

Do you get this error at print time? Or is it only in the designer?

Also, what version of BarTender are you using? I can create more specific instructions if I know your version.

One last thing, it sounds like you did create the names correctly, so I don't think that is the problem. ###### Legacy Poster

Hi,

Nope.I didn't put any default data for my text object. I left it blank and it shows <Empty> at Bartender's label designing.

The error message pops up at when i am trying to test the vb script that i put at "OnSerialize" event in the Event List. So, i didn't able to check it out at print time.

I'm using Bartender Automation Enterprise 9.4.

I will try work on my own while waiting for your reply,sir.
Thanks!! ###### Legacy Poster

[quote name='Gene H - Seagull Support' timestamp='1332181881' post='2011']

[list]
[*]If we are NOT creating the last label, then we do the same division that we did above, and then we strip off the decimal (by setting it to an Integer data type).
[*]If we ARE creating the last label, then we subtract the total of all of the labels that we already created from the TotalQTY, and place that remainder on the last label.
[/list]

[/quote]

Hi.I think there have been some problems here.

Let say that we take 17 for total quantity and 5 for the number of box.
If follow what i understanding above,
first it will 17/5, since it's not even divided,it will calculate the answer with decimal which is 3.4 and strip off become 3.
If the quantity is 3, the last label for quantity will be 17-(3*5),which is 2,but that would be the 6th label,not the 5th

By using 17/(5-1),which is 4.25,we discard the decimal become 4.And when it print out the last label,which is 5th label,it will calculate 17-(4*4),which is 1 for the quantity.That's what i'm looking for. Is it possible to do?

Thank you. ###### Gene Henson

You are correct, the script I attached above does just that, but I failed to mention that in my explanation.

So, when I use the above script with the numbers 17 and 5 I get the following output:

[attachment=147:Output.bmp]

So, I think we are OK so far, but it would probably be best if we tested some more scenarios.

Does that work for you, and can you tell us some of the other number combinations you are likely to see? ###### Legacy Poster

Maybe you can create more specific instructions for me from the beginning.Since i still have the error message now at the vb script there.I think that i have just missed something else.

"You can look through your objects to see if one of the shared values is actually passing a string instead of a number."
Actually,how i can carry out that? The shared values here refers to the SerializedNumber and TotalQty? Both i have did validation on the prompt dialog which will only accept the number input.

Thanks. ###### Gene Henson

What version of BarTender are you using? I'll attach a sample label that you can look at to see how it all comes together. ###### Legacy Poster

Hi Gene H,

I'm using Bartender Enterprise Automation version 9.4

Thank you. ###### Gene Henson

OK, I've attached a label format here that is saved in version 9.4. It should do everything that we've discussed so far.

Take and look and let me know if you have any questions, and let me know how it works for you.

[attachment=150:VBScript.btw]

Thanks! ###### Legacy Poster

[quote name='Gene H - Seagull Support' timestamp='1332535695' post='2063']
OK, I've attached a label format here that is saved in version 9.4. It should do everything that we've discussed so far.

Take and look and let me know if you have any questions, and let me know how it works for you.

[attachment=150:VBScript.btw]

Thanks!
[/quote]

Thanks!! I finally found out what my mistake was.I need to set an integer instead of leaving it blank for my object.If not,the vb script will be not working because it couldn't convert it to int. I have integrated the method with my current label and now it's working ok.
Thanks again for the kind assistance and being patience with me these few days. ###### Legacy Poster

Hi Gene H,

Soory but i found that there is some logic problem in the vb script that i requested.

If Let say total quantity = 27
Box = 4

Using current formula, will become (27/(4-1))=9
Then it will print out 4 labels, with 9,9,9,0 quantity which is not quite good enough.

I'm thinking that maybe we can modify the script again.I have tried but i'm not good enough at vb script but i do have the logic.

If total quantity/ (box-1) = an integer without remainder,
maybe we can make the integer -1,
and make up the total number.

For example,
total quantity = 27, box =4

(27/(4-1))=9
9 is an integer without remainder
we minus one become 8 and
8,8,8,3 total is 27.

I will really appreciate with this help.This is the final stage of my work. Thanks you very much. ###### Legacy Poster

I have already solved the above problem by my own. Thanks anyway.

ログインしてコメントを残してください。