Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting Numeric data only from a text field

Status
Not open for further replies.

CarrieR

Technical User
Nov 9, 2001
60
US
Field name is serial Number, data type is text. Problem is some have numeric value only, some numeric combined with text. How would I extract only numeric values in a query? Text is in the field in various places, 1st, middle of 9 digits, etc.
 
Hi

You'll need to write a function
Code:
function Digits(sVal as string) 
  for i = 1 to len(sVal)
    b = mid(sVal, i, 1)
    select case b
      case "0" to "9"
        Digits = Digits & b
    end select
  next
end function


Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
Can you please post some serial Number values and expected result ?
Here a brute force method.
Copy'n'paste this code in a standard code module:
Code:
Public Function getNum(myField)
  Dim s As String, l As Long, x As String
  If Len(Trim(Nz(myField, ""))) = 0 Then
    getNum = myField
    Exit Function
  End If
  For l = 1 To Len(myField)
    x = Mid(myField, l, 1)
    If (x >= "0" And x <= "9") Then
      s = s & x
    End If
  Next l
  getNum = s
End Function
Then in the query grid:
NumOnly: getNum([serial number field])

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV:

Here are some examples of numbers I don't want: 5397M, 4287M, 64554M000123. The ones I do want, from the same field would be as such: 992041234, 042134587. The actual field name is: Modem Serial #.
 
Code:
function JustNumbers(rng)
  if isnumeric(rng.value) then
    JustNumbers = rng.value
  end if
end function


Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
What about something like this ?
Public Function getNum(myField)
If Len(Trim(Nz(myField, ""))) > 0 _
And IsNumeric(Trim(myField)) Then
getNum = Val(myField)
End If
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Guys

I hate to sound like a real blank wall, but I cannot get this to work. I am not real keen on VB, have tried to place this in a blank module and call this, but I am doing something wrong somewhere.

And thank you all for your help, some day it would be nice if I could do this on my own.
 


How are you using this Function?

Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
I cannot get this to work
Any error message ?
The function must be defined in a standard code module, not a form/report module.
The module must have different name than any function/sub.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Did all as requested, named module different than the function. Called out the function in a query, not getting any results.
 


How are you USING this function????????

Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
JustNumbers: ([ModemSerial#]) in the query grid (for your example)
 
probably should be:

Code:
JustNumbers(ModemSerial#)

Leslie
 
PHv said:
Then in the query grid:
NumOnly: getNum([serial number field])

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 


[tt]
JustNumbers([ModemSerial#])
[/tt]
NO COLON

Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
Skip

Run time error, object required and it locks me out. When I use the function suggested by Skip, I get partial data, for example, when running the query, it will not show the preceding "0" of a series of numbers. It will also, in the field where a number/letter combo used to be, the #error message.
 
Skip

Run time error, object required and it locks me out. When I use the function suggested by PHV, I get partial data, for example, when running the query, it will not show the preceding "0" of a series of numbers. It will also, in the field where a number/letter combo used to be, the #error message.
 
If PHV's functio seems to work, use this modification
Code:
Public Function getNum(myField)
  If Len(Trim(Nz(myField, ""))) > 0 _
  And IsNumeric(Trim(myField)) Then
    getNum = myField
  Else
     getNum = ""
  End If
End Function


Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
Same errors as mentioned previously, strips out preceding zeroes and #Error on lines where number/text was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top