Results 1 to 4 of 4
  1. #1
    webisti is offline The Wisher
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    115

    Extract a number from a string

    Hi
    I do have a module to extract a number from a string :
    Public Function ExtractedValue(strSent) As Double
    If IsNull(strSent) Then
    Exit Function
    End If
    Dim strTemp As String
    Dim intLoop As Integer
    'This function extracts a numeric values from a string and returns it
    ' in ExtractedValue
    'Return value of 0 if no numbers found
    ExtractedValue = 0
    strTemp = ""


    'Perform the extraction
    For intLoop = 1 To Len(strSent)
    If Asc(Mid(strSent, intLoop, 1)) >= 48 And Asc(Mid(strSent, intLoop, 1)) <= 59 Then
    strTemp = strTemp & Mid(strSent, intLoop, 1)
    End If
    Next intLoop
    If Len(strTemp) > 0 Then
    ExtractedValue = Val(strTemp)
    End If
    End Function

    My problem is that the string I do have is like:

    19.0 EA
    195 PAC
    42 M
    150 L
    the problem is that when the number is like 19.0 EA the value I get is 190, in reality it should be 19.
    The rest is fine as there are no dot`s.
    Any ideas on how to solve this issue?

    Thanks

    Webisti

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    You may want to include ascii 46 (.) in your criteria--that should give you the decimal point.

  3. #3
    mrojas is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Sep 2013
    Location
    Concord California
    Posts
    72
    It appears to me that you need to include, in your If statement that validates the ASCII values, the corresponding ASCII code for the period, 46, otherwise your code strips the period off your string.

  4. #4
    webisti is offline The Wisher
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    115
    Quote Originally Posted by mrojas View Post
    It appears to me that you need to include, in your If statement that validates the ASCII values, the corresponding ASCII code for the period, 46, otherwise your code strips the period off your string.
    Thanks Guys it worked.

    webisti

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 05-15-2013, 01:24 PM
  2. Extract SUB String using SELECT Statement
    By riaarora in forum Queries
    Replies: 1
    Last Post: 09-04-2012, 10:15 AM
  3. Extract Value from Variable in String
    By nguyenak in forum Programming
    Replies: 3
    Last Post: 05-24-2012, 03:50 PM
  4. Extract String From Between 2 Values
    By kathleencampbell in forum Queries
    Replies: 5
    Last Post: 03-23-2012, 10:52 AM
  5. Convert Number to String in .csv extract
    By CindyR19 in forum Import/Export Data
    Replies: 3
    Last Post: 08-17-2011, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums