Just built that and tested it ok i think- it just cycles through the field from left to right until the first character that is not the character to remove and then returns everything to the right of it
Main Topics
Browse All TopicsI need to remove leading zeroes from the following types of text strings. I'm looking for the shortest, fastest way of converting this field. I'll be using setvalue to post these to another text field. The initial field's character count can be any number of digits.
Below are sample entries.
01234 converts to 1234
0001234a converts to 1234a
001234-a converts to 1234-a
101234 remains at 101234
2509398 remains at 2509398
123z remains at 123z
000002829839 converts to 2829839
Thank you. Points go to fastest application. I'll be testing with 1000 rows of data.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
mbizup that is a good idea however for the example of 2509398 it would then return 259398
if access had an instring reverse method like excel does it would be the easiest way of doing this but i'm not sure of any inbuilt functions that could handle doing this trim.
If it had an arguement for trim like mysql does then you would just set the character but as it is I think VBA is probably the best approach.
Hi lvbarnes,
I always fall back on Regular Expressions :) Add this UDF to a regular module:
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True)
' This function uses Regular Expressions to parse a string, and replace parts of the string
' matching the specified pattern with another string. The optional argument ReplaceAll controls
' whether all instances of the matched string are replaced (True) or just the first instance (False)
' By default, RegExp is case-sensitive in pattern-matching. To keep this, omit MatchCase or
' set it to True
' If you use this function from Excel, you may substitute range references for all the arguments
Dim RegX As Object
Set RegX = CreateObject("VBScript.Reg
With RegX
.Pattern = PatternStr
.Global = ReplaceAll
.IgnoreCase = Not MatchCase
End With
RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
Set RegX = Nothing
End Function
And run an update query:
UPDATE YourTable
SET SomeField = RegExpReplace([SomeField],
Regards,
Patrick
capricorn1,
> Patrick,
> will the function ignore the zero in between
Yes, it will. The caret signifies the beginning of the text, so only leading zeroes will be affected. Once the first non-
zero character is encountered, it stops looking. (Of course, if the string is *all* zeroes, then they will all go :)
It may not be faster than Raynard's, but once you figure out how to write RegExp pattern strings, it is supremely flexible.
Have a look at: http://vbaexpress.com/kb/g
Regards,
Patrick
This will do your job.
vFound will equal false until the first non-zero character is located.
Once located, vFound is set to TRUE.
From that point on - all characters are included.
function ParceString(byref vstring as string) as string
dim vFound as boolean
'--- get rid of any spaces ------
vstring = trim(vstring)
dim j as integer
for j = 1 to len(vstring)
if mid(vstring,j,1) = "0" and vFound = 0 then
'----- do nothing -------
else
vFound = true
ParceString = ParceString & mid(vstring,j,1)
end if
next j
end function
Scott C
Hello everyone,
This was a wonderful string of posts and I ended up with three very different, but very valid solutions. A time trial over 1000 records was too close to call, so I expanded the test to 50,000 records and tested repeatedly for a valid time.
1st Place: Raynard7 at 01:41
2nd Place: ClarkScott at 01:48
3rd Place: MathewsPatric at 1:56
As you can see they were all very close (but so different in methods!) Thank you all for your answers and the solution to my question.
mbizup, capricorn1...thank you for your solutions, but you can see how the alphanumeric part through me for a loop as well.
grayl...the results of your formula didn't match with the desired results, most likely due to the alphanumeric/val combinations, but I appreciate your effort.
Thank you,
Lawrence Barnes
Lawrence,
Glad to help. I expected that my solution would run slower: using late binding is slower, plus on every
record you have the creation and destruction of the RegExp object. It gets expensive. I do like that
function a lot, though, because you can do some truly magical things with it that can be hard to impossible
using other methods.
Regards,
Patrick
Re: Select Val(myFld) & Mid(myFld,instr(myFld,val(
I just test the expression: Val(myFld) & Mid(myFld,instr(myFld,val(
in the Immediate pane of the VB Editor and could not find any expression for myFld with or without leading zeros and with or without trailing alpha strings which did not evaluate correctly.
<grayl...the results of your formula didn't match with the desired results> - can you give me an example?
Lawrence:
using this table named myTable:
myFld
000002829839
0001234a
001234-a
01234
101234
123z
2509398
running this query:
SELECT myFld, Val(myFld) & Mid(myFld,instr(myFld,val(
FROM myTable;
I got these results:
myFld StrippedFld
000002829839 2829839
0001234a 1234a
001234-a 1234-a
01234 1234
101234 101234
123z 123z
2509398 2509398
this shows the original value and the stripped value side by side. Once you achieve the result you want, it is a simple thing to change the Select query into an Update query - but I never got the chance to say that. I would be interested in the time against your test table.
Business Accounts
Answer for Membership
by: Raynard7Posted on 2006-10-03 at 17:59:03ID: 17655987
Public Function trimLeadingChar(charToRemo ve As String, stringToFix As String) As String
Dim lngLC As Long
For lngLC = 1 To Len(stringToFix)
If Mid(stringToFix, lngLC, 1) <> Left(charToRemove, 1) Then
trimLeadingChar = Right(stringToFix, Len(stringToFix) - lngLC + 1)
Exit Function
End If
Next lngLC
trimLeadingChar = ""
End Function