UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Remove All Characters That Are Not Numeric    
 
   
accesshawaii
post Jun 23 2011, 01:12 PM
Post #1

UtterAccess VIP
Posts: 4,703
From: From Hawaii - Now in Wisconsin...Am I Nuts?



I have a field where I need to just capture the numeric values. The field will pretty much always contain 10 numeric characters but the non-numeric characters are not the same for each field. Below are a few samples of how data appears in the field.

123.456.6789
123-456-7891
333 451 6789

Does anyone know if theres a syntax that I can use, so with the above examples, it would return the below.

1234566789
1234567891
3334516789

Any assistance would be appreciated.
Go to the top of the page
 
+
doctor9
post Jun 23 2011, 01:17 PM
Post #2

UtterAccess VIP
Posts: 9,430
From: Wisconsin



Dan,

Here's a quick-n-dirty function for you:

CODE
Public Function strNumbersOnly(strInput As String) As String

'   Takes a string, strips out the non-numeric characters, then returns only the
'   digits in string form.

    Dim i As Integer
    
    strNumbersOnly = ""
    For i = 1 To Len(strInput)
        If IsNumeric(Mid(strInput, i, 1)) Then
            strNumbersOnly = strNumbersOnly & Mid(strInput, i, 1)
        End If
    Next i

End Function

Hope this helps,

Dennis
Go to the top of the page
 
+
accesshawaii
post Jun 23 2011, 01:51 PM
Post #3

UtterAccess VIP
Posts: 4,703
From: From Hawaii - Now in Wisconsin...Am I Nuts?



Thanks, Dennis. I'm trying to iterate this through a recordset to get the values in the field and I cannot get it to work. I'm sure I'm probably doing something wrong on my end. Below is the code that I'm trying. Could you tell me what I'm doing wrong? Thanks.

CODE
Do While Not rst.EOF
    strPhone = rst.Fields("MyField1").Value
    For x = 1 To Len(strPhone)
        If IsNumeric(Mid(strPhone, x, 1)) Then
            strPhoneSQL = strPhoneSQL & Mid(strPhone, x, 1)
        End If
    Next x
    rst.MoveNext
Loop
Go to the top of the page
 
+
doctor9
post Jun 23 2011, 01:59 PM
Post #4

UtterAccess VIP
Posts: 9,430
From: Wisconsin



Dan,

Rather than adapt my function code into what you're doing, just paste the entire function to the bottom of your code module (or into a new one). Then, just call the function from within your loop:

CODE
Do While Not rst.EOF
    strPhoneSQL = strNumbersOnly(rst.Fields("MyField1").Value)
    rst.MoveNext
Loop

By the way... what is strPhoneSQL? Is it a field in your recordset? Or are you deleting some of your code where you're using strPhoneSQL?

Dennis
Go to the top of the page
 
+
accesshawaii
post Jun 23 2011, 02:17 PM
Post #5

UtterAccess VIP
Posts: 4,703
From: From Hawaii - Now in Wisconsin...Am I Nuts?



You're a genius as always, Dennis. That did the trick. The strPhoneSQL field was just one of the many ways I was trying to do it that was not working but it's working now. Thanks again.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th June 2013 - 04:52 AM