How To Generate A Random Value ??? Follow

0
Avatar
Legacy Poster

Hello

I m new user of BarTender...

I would like to generate a text object with a value like this "ABC" then a random value between 25 to 99 and then "JKL". For example "ABC58JKL".

I think I must use VB script but without success.

Please help me.
Thanks.

6 comments

0
Avatar
Susan Chen
Moderator
Comment actions Permalink

Hi, Jun:

1) For random number part:

For random number generator, you can use VBScript Rnd function. For more VBscript usage, you can reference Microsoft VBscript http://msdn.microsoft.com/en-us/library/e566zd96(v=VS.85).aspx

In your case, you can set up

Dim upperbound, lowerbound

upperbound = 99
lowerbound = 25

value = Int( ( upperbound - lowerbound + 1 ) * Rnd + lowerbound )


2) For achieve: ABC58JKL in data source, you can setup substring
method 1: Double click on Data Source-> Choose "Advanced" tab to create substring lists
substring 1) ABC
substring 2) VBscript Random number
substring 3) JKL

method 2:
concatenate all three substrings in VBScript into one string.


Pleae give it a try to see if this is what you want. Thanks!
0
Avatar
Legacy Poster
Comment actions Permalink

Dear Susan,

That s working well, But :

- how to do in order that this value change at each printing, or each saving (like Excel) ? (I tried to put the script on "control events" and "onPrintStart", but no success).

- Also, if I create a new text, and put this script, he give me the same number, that s normal ?

Thanks so much for your help.
Regards,
0
Avatar
Shotaro Ito
Moderator
Comment actions Permalink

Hi Jun,
You need "Randomize" statement at the beginning.
Set script in event control script.

In AutoSelectedEvent event, set value shown design time.
[code]Value="00"
[/code]
In OnPrintStart event, randomize random sequence.
[code]Randomize[/code]
In OnNewRecord event (or other events like OnIdenticalCopies, OnSerialize), get next random number.
[code]upperbound = 99
lowerbound = 25
value = Int( ( upperbound - lowerbound + 1 ) * Rnd + lowerbound )[/code]

Reference:
http://msdn.microsoft.com/en-us/library/38d7ckek
0
Avatar
Legacy Poster
Comment actions Permalink

[quote name='Shotaro I -Seagull Support' timestamp='1324889620' post='1406']
Hi Jun,
You need "Randomize" statement at the beginning.
Set script in event control script.

In AutoSelectedEvent event, set value shown design time.
[code]Value="00"
[/code]
In OnPrintStart event, randomize random sequence.
[code]Randomize[/code]
In OnNewRecord event (or other events like OnIdenticalCopies, OnSerialize), get next random number.
[code]upperbound = 99
lowerbound = 25
value = Int( ( upperbound - lowerbound + 1 ) * Rnd + lowerbound )[/code]

Reference:
http://msdn.microsoft.com/en-us/library/38d7ckek
[/quote]

Hi Shotaro

Thanks for your answer, but your script is not working on my side,

This script is working:
[font="Arial Black"][font="Arial Black"]Dim MyValue, Response
Randomize ' Initialize random-number generator.
Do Until Response = vbNo
MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.
MsgBox MyValue
Response = MsgBox ("Roll again? ", vbYesNo)
Loop[/font][/font]


But How to delete the message box and set the value on the text box ?

Thanks for your help.
0
Avatar
Shotaro Ito
Moderator
Comment actions Permalink

Hi Jun - Please tell me what happened when you feel "not working". perhaps you don't like the way it always show "00" on design view.

Since you made the same step as 1st reply from Susan's, do the same step again in multiline script. then add "randomize" in first line like this
[code]
Randomize
upperbound = 99
lowerbound = 25
value = Int( ( upperbound - lowerbound + 1 ) * Rnd + lowerbound )
[/code]

It does basically the same thing as your sample code.
0
Avatar
Legacy Poster
Comment actions Permalink

Hi Shotaro

Sorry, I made a big mistake, your and Susan scripts are well working.

TKS!

Please sign in to leave a comment.