Field with mixed text and numbers, need to omit text data (1 Viewer)

ragribben

New member
Local time
Yesterday, 19:22
Joined
Mar 11, 2011
Messages
6
I have a field that has data with text and numbers. For example:
1/123hg
45/984phl
1/456
53/54656
All data has the / in it and I want to omit only the data with the text in it.
I have tried a number of like and not like type statments but have not been able to figure out how to omit this data.

Thank you for any help you can give.

Becky :)
 

DCrake

Remembered
Local time
Today, 01:22
Joined
Jun 8, 2005
Messages
8,632
Does the text always appear at the rear of the string?
Do you want to retain the /?
 

ragribben

New member
Local time
Yesterday, 19:22
Joined
Mar 11, 2011
Messages
6
Yes I want to retain the / and yes it is always at the rear but the qty of text can vary.
 

DCrake

Remembered
Local time
Today, 01:22
Joined
Jun 8, 2005
Messages
8,632
Code:
Public Function StripChars(AnyString as string) As String

'/Read the incoming string from right to left 1 character at a time
'/If it comes accross a number it knows to exit the loop
'/It then returns the numeric part of the string.


For x = Len(AnyString) to 1 step -1
   If IsNumeric(Mid(AnyString,x) Then
      Anystring = Left(Anystring,x)
      Exit For
   End If
Next

StripChars = Anystring

End Function

Place this function in a standard module then in your query

Code:
AliasName:StripChars([FieldName])


Aircode for brevity only, untested.
 

philben

Registered User.
Local time
Today, 02:22
Joined
Jan 30, 2011
Messages
23
A small correction in above code :
Code:
Public Function StripChars(AnyString As String) As String
'/Read the incoming string from right to left 1 character at a time
'/If it comes accross a number it knows to exit the loop
'/It then returns the numeric part of the string.
[B]Dim x As Long[/B]
For x = Len(AnyString) To 1 Step -1
   If IsNumeric(Mid(AnyString, x[COLOR=red][B], 1[/B][/COLOR])) Then
      AnyString = Left(AnyString, x)
      Exit For
   End If
Next
StripChars = AnyString
End Function

Possibly an inline alternative :
Code:
Left([MyField], InStr([MyField], "/") - 1) & "/" & Val(Mid([MyField], InStr([MyField], "/") + 1))

Philippe
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 20:22
Joined
Jan 23, 2006
Messages
15,394
Here's another version, including a test. I was testing while others submitted apparently.

Code:
Sub TestChrs() '45/984phl
Dim s As String
s = "45/984phl"
Debug.Print fKeepNumbers_Slash(s)
End Sub

Function fKeepNumbers_Slash(strIn) As String
'
'keep the slash and any digits
'
Dim I As Integer
Dim strOut As String
   For I = 1 To Len(strIn) Step 1
      If Mid(strIn, I, 1) Like "[0-9/]" Then
         strOut = strOut & Mid(strIn, I, 1)
      End If
   Next I
   fKeepNumbers_Slash = strOut
End Function
 

stopher

AWF VIP
Local time
Today, 01:22
Joined
Feb 1, 2006
Messages
2,395
Here's a solution that doesn't require VBA:

Code:
Left([myStr],InStr([myStr],"/")) & Val(Right([myStr],Len([myStr])-InStr([myStr],"/")))
 

stopher

AWF VIP
Local time
Today, 01:22
Joined
Feb 1, 2006
Messages
2,395
A cheekier solution :D:

Code:
Replace(Val(Replace([myStr],"/",".")),".","/")


Edit: assumes numbers never contain the decimal point
 

stopher

AWF VIP
Local time
Today, 01:22
Joined
Feb 1, 2006
Messages
2,395
Possibly an inline alternative :
Code:
Left([MyField], InStr([MyField], "/") - 1) & "/" & Val(Mid([MyField], InStr([MyField], "/") + 1))

Philippe
Sorry, I didn't spot this before I posted my first solution.
 

philben

Registered User.
Local time
Today, 02:22
Joined
Jan 30, 2011
Messages
23
Hi,

Sorry, I didn't spot this before I posted my first solution
No problem, we've had the same idea :)

According to the fastest VBA function, the winner seems to be DCrake.

Regards,

Philippe
 

Users who are viewing this thread

Top Bottom