SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 5 of 5
  1. #1
    New Member
    Join Date
    Feb 2014
    Location
    NJ, USA
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Increment an ID by 1 issue

    I am trying to create a unique user ID for each test run on a web site. I would like to do the following, but it does not work:

    name = Luxin27

    name = name + 1

    MsgBox name - should display Luxin28, but displays Luxin27.

    Any thoughts on how to accomplish this? I tried splitting the text from the number on a database. I can get the number to increment, but when I send the output to a MsgBox is displays "Luxin 28" instead of "Luxin28".

    Any thoughts on this?

    Thanks!

  2. #2
    Advanced Member
    Join Date
    Mar 2007
    Location
    Salt Lake City, UT, USA
    Posts
    606
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0
    Your variable, 'name', is untyped (because this is VBS and pretty much everything is untyped). That means 'name' can contain a string, a number, a date, whatever you want. You can "append" things to strings, such as:

    x = "Luxin"
    y = "27"
    name = x & y

    And now 'name' will contain "Luxin27". The two variables were simply attached together by that ampersand. It will not add any spaces between the variables unless you include them inside the quotation marks when you define the variables.

    You can add numbers together, but only if every variable contains numbers. Only numbers. So:

    x = 27
    y = 1
    z = x + y

    And now 'z' will contain 28, and QTP will think it's a number because it recently did math on that variable.

    What you cannot do is what you tried to do. "Luxin27" MUST be treated as a string because part of it is alphabetical. You cannot add a number to a string unless that string is ONLY numerical.

    The good news is that there is an answer, it just takes a bit more work. We must first split your 'name' variable into two parts, the alphabetical part and the numeric part. There are many ways to do this, but I'll keep it simple as long as you ALWAYS know that the first 5 characters of 'name' will be "Luxin" - if that is not true, then we'll want to look for a more tricky solution. Try this:

    (side note, I'm changing the name of your variable to make it clear that it contains a string AND to make it less likely that you will accidentally use a "reserved word"; i suggest you do this with all your variables, but that's up to you)

    Code:
    strName = "Luxin27"
    strNameAlpha = Left(strName, 5)
    intNameNumeric = Right(strName, Len(strName) - 5)
    'Now we have two variables that each contain part of the original strName string
    'We add +1 to the numeric part. It doesn't matter that this variable contains a string right now, it will still do the math because that string is all numeric characters
    intNameNumeric = intNameNumeric + 1
    'Now we put them together with the & sign. It doesn't matter that intNameNumeric contains a number; it will concatenate the two variables just like they are both strings.
    strName = strNameAlpha & intNameNumeric 
    
    msgbox strName 'Not necessary, but this will demonstrate that the math worked.
    That wasn't the cleanest solution. For example, I didn't really need strNameAlpha at all; I only used it so it would be perfectly clear what I was doing. Also, I could have used this line: intNameNumeric = Right(strName, 2) and it would have been the same, but that simpler version would fail if the numeric part is ever longer than two digits (e.g. "Luxin100"). Also remember, if the first part of your string is not always exactly 5 characters long, this code won't work and we'll have to get a little more tricky.

    Hopefully that little code snippet shows you several basic concepts of VBS data manipulation.
    Last edited by BlakeWalker84120; 02-13-2014 at 07:36 AM.
    "The last 10% of any software project will take 90% of the budgeted time. The first 90% will take the other 90%"

  3. #3
    New Member
    Join Date
    Feb 2014
    Location
    NJ, USA
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0
    Thank you Blake. The first solution is working well, and it will be easier to maintain for the next person who looks at the code. Plus it will be a lot easier to work on the database side.

  4. #4
    Advanced Member
    Join Date
    Mar 2007
    Location
    Salt Lake City, UT, USA
    Posts
    606
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0
    In case you're interested, here's the tricky version, with somewhat more efficient coding:

    Code:
    strName = "Luxin27"
    i = 0
    Do
        i = i + 1
    Loop While isNumeric(right(strName, i)) and i <= Len(strName) 
    strName = left(strName, len(strName) - i + 1) & (right(strName, i - 1) + 1)
        
    msgbox strName
    This will work with any string that ends in a number. I didn't check to be certain the last character of strName is a number; I preferred to take that much for granted, but you can add a validation for that if you want.

    This version will work with:
    strName = "Luxin27"
    strName = "Luxin1234567890"
    strName = "123456"
    strName = "L12u34x56i78n90"

    And any other string as long as it ends in one or more numeric digits.
    "The last 10% of any software project will take 90% of the budgeted time. The first 90% will take the other 90%"

  5. #5
    Moderator
    Join Date
    Jan 2007
    Location
    Pittsburgh, PA
    Posts
    2,497
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0
    Generate unique string with just 1 line of QTP code, you just need to maintain at least 1 sec delay between each call to that code. I hope length of the string is not an issue in your database if you use date and timestamps.


    uniqueString = "Test"&DotNetFactory.CreateInstance("System.DateTi me").Now.ToString("MMddHHmmss")
    Print uniqueString

    -Reddy
    Last edited by TmReddy; 02-13-2014 at 09:08 AM.

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 10.71%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 07:56 AM.

Copyright BetaSoft Inc.