Avatar of kay soo
kay soo
 asked on

Count Leading Zero of String

Hi, can anyone show me how to count leading zero of a string?
I have searched the KB but not exactly what I am looking for, just want a simple count result of the number of leading Zero of a string.
Strings randomly consist of min 5 characters or max of 15 characters, all numeric, no alphabet involved.

e.g.
04421000
Result = 1

001540045456001
Result = 2

000123450100124
Result: = 3

121001421000041
Result = 0

Thx
Microsoft Access

Avatar of undefined
Last Comment
kay soo

8/22/2022 - Mon
Ryan Chong

try a function like this:

Function getLeadingZeroCount(ByVal v As String) As Integer
    tmp = Trim(v)
    If tmp = "" Then
        getLeadingZeroCount = 0
    Else
        For i = 1 To Len(tmp)
            If Mid(tmp, i, 1) = "0" Then
                getLeadingZeroCount = getLeadingZeroCount + 1
            Else
                Exit Function
            End If
        Next i
    End If
End Function

Open in new window

ASKER CERTIFIED SOLUTION
mbizup

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ryan Chong

@mbizup,

yup, a smart one
kay soo

ASKER
Thx for the solutions fellas, I go for @mbizup cause I used an unbound field to keep the count.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck