Bear with me here, I'm not that proficient in ilogic or any sort of coding for that matter... hopefully I've got the terminology correct or at least correct enough to be understood.
Also, I do have a work-around for what I want to achieve but I'm curious to see if there is a better way of getting what I want.
My new work place has a semi-smart numbering system that looks like this: xx-xxxxxx
The two digit prefix is for category/type of part and the six digit number after the hyphen is simply a dumb sequential part number.
But recently I've found a snag when trying to separate the two parts of the part number, so that the can be used in other ways that we don't need to cover here.
In the past when using ilogic I've used Val when needed to return the first numerical value from a string.
For example:
Dim Category = Val(iProperties.Value("Project", "Part Number"))
This will return the numerical value to the left of the hyphen. No problem ... usually ....
.... I have now discovered that Val drops all proceeding zeros.
The prefixes of the part number starts at "01" and any numbers starting with the prefix "01" through "09" are returned as 1, 2, 3, 4 ... etc and not 01, 02, 03, 04 ....
I could of course use Left:
Dim Category = Left ((iProperties.Value("Project", "Part Number")),2)
But using Val provides a numerical value which in a way provides a kind of built in check that what ever has been entered as the part number is actually a number.
For example, if someone had mistakenly entered "p3" as the prefix, using Val will simply return 0.
Using Left will return "p3" but I'm not interested in using p3 as that is incorrect.
I can add a check by using IsNumeric but this adds an extra step and just doesn't seem as tidy as using just Val.
So, my question is, can Val be told to return proceeding zeros? Or is there some other way to return proceeding zeros as part of a numerical value instead of returning a string and then checking if it is numerical.
TIA, Ben
Hi @Anonymous
You can use ToString to format the value 🙂
See example:
Dim pNum As String = "05-123456" Dim Category As String = Val(pNum).ToString("00") MsgBox(Category)
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
Hi @JhoelForshav
Thanks for taking the time to answer.
I tried your code and confirm that it does indeed return proceeding zeros and even two zeros if both digits of the prefix are zeros. Which is good. 🙂
But I also tried adding in letters to the prefix, too. Unfortunately that too returned zeros.
I tried "k0" "kk" and "0k" and and all three of those variations returned "00"
I also tried single digits and single letters in the prefix, and that too returns "00"
So as a built in checking function, that doesn't help so much.
I know I can add checking by adding some extra lines of code, I was just wondering if there was a tidier way of doing it with less code.
I like how Val can be used to only return numerical values from a string. But still, relying on Val to return those numerical values isn't perfect.
In my original post I wrote the "p3" returns "0". So, if "0" was returned I could tell my ilogic rule that a single 0 is to be treated as an error.
But "3p" returns "3". And that still isn't correct. So I'd have to tell the ilogic rule that all single digits values are incorrect and treat them as an error.
And this is a weird one ... if the string consists of "3d-xxxxxx" then "0" will be returned. Why? I don't know.
Anyone know why "3d" returns 0 instead of 3?
I'd like to add checking to catch any errors in part numbers. It'll never pick up a part number errors that use a valid category prefix but at least any obvious ones will be picked up. Hopefully there isn't any, but you never know.
I have previously worked for company that had auto sequential numbering via Vault and even with that turned on errors could occasionally be found in part numbers. It's amazing what people can do to stuff up automation. A good ol' case of PEBKAC.
If you Use CInt with some error handling, it will kick out invalid part numbers and can be converted into 2 character string:
Dim pNum As String = "05-123456"
Try
Dim Category As String = CInt(Left(pNum, 2)).ToString("00")
MsgBox(Category)
Catch
MsgBox("Invlaid PartNumber")
End Try
Can't find what you're looking for? Ask the community or share your knowledge.