Numbers Only

mlr0911

Registered User.
Local time
Today, 11:33
Joined
Oct 27, 2006
Messages
155
Is there a way to write a query where it will only capture numbers? For example, if I have the following:

"Testing transactions 11100202020 in the following order"

What I would like to do is capture the numbers that is in the middle of the sentence. I have tried using the len, left, mid, right functions in my expressions, but the sentence length may change from time to time. Is ther a way to tell access to only capture the numbers?

Thanks in advance
mlr0911
 
That's Great!!!!

Thanks for your help.
 
1 more thing, how can I modify the code to pick up my leading zero's? I have some numbers that begin with a "0" and need to capture that as well.

This code is wonderful!!!!!!! Thank you so much for your help.
 
Are the numbers always the same number of digits?
 
No, some contain 10 numbers, other 13, etc. but no less than 8 and no more than 13.

Thanks
 
I hope Jon does not mind but I made a change so the function will return the string you wanted.
 

Attachments

Last edited:
I just replaced the uploaded db on my previous post because it had a problem that I didn't catch initially.
 
Wow. My head hurts interpreting how that short piece of code works. :) Way to go both of you.
 
neat that RG

i like the way it slices off words at a time, so it ignores numbers embedded in strings

however just checking it, theres still a slight catch

a word beginining with a number eg 12items with no spaces returns the whole string 12items in the function result, because val 12items would return 12 and stop the loop
 
Good point Gemma but it may still satisfy the OP's requirement.
 
The following modification should be able to pick up the number 12 from 12items (as well as retain leading zeros, if any).
Code:
Public Function getNum(Mixed As String) As String
   Dim Num As Double
   
   Num = Val(Mixed)
   Do While Num = 0
     If InStr(Mixed, " ") Then
       Mixed = Mid(Mixed, InStr(Mixed, " ") + 1)
       Num = Val(Mixed)
     End If
   Loop

   [b]getNum = Trim(Left(Mixed, InStr(Mixed, Num) + Len(Str(Num)) - 2))[/b]

End Function
.
 
Keep in mind that searching for "NumbersOnly" would've returned this (not that you knew to search for this, but still :P). This will return all the numbers in a string, regardless of where they are, if there are leading zeroes, etc. For example, this string:

ABC 000123 DEF

would return 000123. This string

ABC 000123 DEF45609 43H I

would return 0001234560943. The original reason I wrote this was to remove formatting from phone numbers and SSNs before storing that information in a table. In other words, this string:

(123)456-7890

would return 1234567890, which is how you'd store that information.

Code:
Function NumbersOnly(strConvert As String) As String
'Returns only the numbers from a passed string. 

    Dim curChar As String
    Dim ctr As Integer
    
    If IsNull(strConvert) Then
        NumbersOnly = ""
        Exit Function
    End If
    
    For ctr = 1 To Len(strConvert)
        curChar = Mid(strConvert, ctr, 1)
        If IsNumeric(curChar) Then
            NumbersOnly = NumbersOnly & curChar
        End If
    Next

End Function
 
Originally posted by Moniker
The original reason I wrote this was to remove formatting from phone numbers and SSNs before storing that information in a table. In other words, this string:

(123)456-7890

would return 1234567890, which is how you'd store that information.

Not that it would matter much given our computer speeds nowadays, but to remove "(", ")" and "-" from (123)456-7890, you need to iterate only three times instead of 13 times if you use Replace()

^
 
Last edited:
Very true, but you also get people that put in phone numbers in a lot of different ways:

(123)456-7890
123-456-7890
123/456-7890
123.456.7890

etc., not to mention foreign (non-US/Canada phone numbers), etc. Really, you'd think that some people doing the data entry part of things have zero training on the basics. ;)
 

Users who are viewing this thread

Back
Top Bottom