Val Function

1. Senior Member
Join Date
Dec 1969
Posts
450

## Val Function

I am trying use the following function. It works just fine in VB but in VbScript the Val Function is not supported<BR><BR>project="PZ2002102"<BR><BR>Functi on CutYear(project)<BR>Dim st, i, ch<BR>If IsNull(project) Then<BR>Exit Function<BR>End If<BR>st = ""<BR>i = 1<BR>ch = Mid(project, i, 1)<BR>While (Val(ch) = 0) And i &#060;= Len(project)<BR> st = st + ch<BR> i = i + 1<BR> ch = Mid(project, i, 1)<BR>Wend<BR>If i + 1 &#060; Len(project) Then<BR> st = st + Mid(project, i + 2, Len(project))<BR>End If<BR>CutYear= st<BR>End Function<BR><BR>I want project to equal="PZ02102"<BR>So the first 2 digits of the year must be eleminated

2. Senior Member
Join Date
Dec 1969
Posts
16,931

## Yeah, VBScript don't support that.

Maybe I&#039;m over-simplifying this...<BR><BR>But why not:<BR><BR>&#060;%<BR>Function CutYear(project)<BR> CutYear = Left(project, 2) & Mid(project, 4)<BR>End Function<BR>%&#062;<BR><BR>Assuming, of course, that the characters you want to remove are always numbers 1 and 2.<BR><BR>Craig.

3. Senior Member
Join Date
Dec 1969
Posts
16,931

## Correction....

...the numbers you want to remove are 3 and 4...<BR><BR>Not woken up yet!<BR><BR>Craig.

4. Senior Member
Join Date
Dec 1969
Posts
148

## RE: Val Function

You can use CInt(ch) to convert "2" into 2.<BR>However, it will generate an error trying to convert "P" so you&#039;ll need error trapping eg<BR><BR>On Error Resume Next<BR> myVar = CInt(ch)<BR> If Err.Number = 0 Then<BR>etc etc<BR><BR>This is clumsier than Val() .. perhaps you can trim off extraneous letters with one of the string functions.

5. Senior Member
Join Date
Dec 1969
Posts
450

## Hope it was just that Simple!

Well, the value of project is coming from a database table , I do not know if it will always be like that or not, because users will have the permission to insert new values<BR><BR>any other clue

6. Senior Member
Join Date
Dec 1969
Posts
450

## RE: Val Function

Cint, also not supported in vbscript as the only datatype you can use is Variant<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
16,931

## Rather than...

...encouraging the use of On Error Resume Next, you could always:<BR><BR>&#060;%<BR>Function Val(strString)<BR> if IsNumeric(myVar) then<BR> Val = CInt(myVar)<BR> else<BR> Val = 0<BR> end if<BR>End Function<BR>%&#062;<BR><BR>Craig.

8. Senior Member
Join Date
Dec 1969
Posts
16,931

## No, not true....

CInt() is supported. It is a variant with subtype of integer.<BR>

9. Senior Member
Join Date
Dec 1969
Posts
16,931

## That's an over-simplification..

...of exactly what Val() does, but it&#039;s ok for your situation, because you&#039;re only passing a single-letter string to it.<BR><BR>Craig.

10. Senior Member
Join Date
Dec 1969
Posts
450

## RE: Rather than...

I have just tryed IsNumeric,<BR>It works just fine

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•