1st numerical field in a string

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

Is there a VBA function that will return the
1st numerical value in a string?

E.g.,

val = FirstNo("ClassID = 8 AND bla bla bla")
val = 8
 
Bill said:
Is there a VBA function that will return the
1st numerical value in a string?

E.g.,

val = FirstNo("ClassID = 8 AND bla bla bla")
val = 8


Nope. It would be easy enough to write one, though.
 
The function:

Function FirstNum(StringToParse)
Dim NumVal As Integer
For i = 1 To Len(StringToParse)
If IsNumeric(Mid(StringToParse, i, 1)) Then
FirstNum = (Mid(StringToParse, i, 1))
Exit Function
End If
Next
End Function

To call it, where the field you're searching is named FieldToParse and the
field to hold the number is NumericalValue

Me.NumericalValue = FirstNum(Me.FieldToParse)

BTW, ***VAL*** is the name of an Access function and should not be used as a
variable name.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via AccessMonster.com
 
Back
Top