SOLVED

Strip Leading Zero's

%3CLINGO-SUB%20id%3D%22lingo-sub-3102454%22%20slang%3D%22en-US%22%3EStrip%20Leading%20Zero's%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3102454%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Experts%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20could%20I%20strip%20the%20leading%20zeros%20from%20invoice%20numbers%20so%20that%20I%20can%20simply%20compare%20on%20the%20non%20zeros%3F%26nbsp%3B%20My%20query%20is%20below%20and%20I%20have%20highlighted%20in%20red%20the%20area%20that%20I%20am%20looking%20for%20a%20solution.%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20not%20sure%20if%20this%20is%20with%20Replace%3F%26nbsp%3B%3C%2FP%3E%3CP%3Ethank%20you.%26nbsp%3B%3C%2FP%3E%3CP%3ESELECT%20Stampli.Vendor%2C%20Stampli.%5BInvoice%20%23%5D%2C%20Import_ExcelPC.%5BInvoice%20No%5D%2C%20Stampli.%5BInvoice%20amount%5D%2C%20%22these%20were%20NOT%20IN%20a%20previous%20DD%22%20AS%20ME%2C%20Import_ExcelPC.Vendor%2C%20Import_ExcelPC.%5BInvoice%20amount%5D%3CBR%20%2F%3EFROM%20Stampli%20LEFT%20JOIN%20Import_ExcelPC%20%3CFONT%20color%3D%22%23000000%22%3EON%20(Stampli.%5BInvoice%20amount%5D%20%3D%20Import_ExcelPC.%5BInvoice%20amount%5D)%20%3C%2FFONT%3EAND%20(Stampli.Vendor%20%3D%20Import_ExcelPC.Vendor)%20AND%20%3CSTRONG%3E%3CFONT%20color%3D%22%23DF0000%22%3E(Stampli.%5BInvoice%20%23%5D%20%3D%20Import_ExcelPC.%5BInvoice%20No%5D)%3C%2FFONT%3E%3C%2FSTRONG%3E%3CBR%20%2F%3EWHERE%20(((Import_ExcelPC.%5BInvoice%20No%5D)%20Is%20Null)%20AND%20((Import_ExcelPC.Vendor)%20Is%20Null)%20AND%20((Import_ExcelPC.%5BInvoice%20amount%5D)%20Is%20Null))%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3102454%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eaccess%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3103025%22%20slang%3D%22en-US%22%3ERe%3A%20Strip%20Leading%20Zero's%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3103025%22%20slang%3D%22en-US%22%3Euse%20Val(%5Bfieldname%5D)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3104555%22%20slang%3D%22en-US%22%3ERe%3A%20Strip%20Leading%20Zero's%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3104555%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1228265%22%20target%3D%22_blank%22%3E%40arnel_gp%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20Arnel%2C%20thank%20you%20for%20the%20reply.%20I%20have%20a%20follow%20up%20if%20you%20dont%20mind.%3CBR%20%2F%3EI%20created%20the%20new%20query.%3CBR%20%2F%3EThe%20%5BInvoice%20%23%5D%20is%20a%20short%20text%20field%20(its%20a%20linked%20table%20fyi).%3CBR%20%2F%3EWhen%20I%20apply%20NumInvoice%3A%20Val(%5BStampli%5D.%5BInvoice%20%23%5D%20%26amp%3B%20%22%22)%20the%20result%20is%20not%20as%20expected%20on%20the%20display.%3C%2FP%3E%3CP%3EThe%20original%20%5Binvoice%20%23%5D%20field%20in%20the%20linked%20table%20has%20combinations%20of%20letters%2C%20dashes%2C%20spaces%20and%20leading%20zeros.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20how%20the%20data%20looks.%20You%20can%20see%20the%20original%20invoice%20%23%20and%20the%20modified%20(NumInvoice).%20There%20is%20an%20example%20with%20a%20leading%200%20(the%20last%20record)%20and%20the%20NumInvoice%20does%20strip%20the%20leading%20zero%20but%20the%20other%20invoice%23%20records%20have%20been%20relished%20to%20either%200%20or%20all%20stripped%20after%20a%20non%20number.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Tony2021_0-1643803212445.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F344549i94A80D4F6853738F%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Tony2021_0-1643803212445.png%22%20alt%3D%22Tony2021_0-1643803212445.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3104615%22%20slang%3D%22en-US%22%3ERe%3A%20Strip%20Leading%20Zero's%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3104615%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1029845%22%20target%3D%22_blank%22%3E%40Tony2021%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EArnel%2C%20I%20ran%20into%20this%20code%20online%3A%3CBR%20%2F%3EI%20tested%20and%20it%20seems%20to%20return%20what%20I%20need.%3C%2FP%3E%3CP%3EPublic%20Function%20StrippedChar(theString%20As%20String)%20As%20String%3C%2FP%3E%3CP%3E'%20Remove%20Spaces%3CBR%20%2F%3EtheString%20%3D%20Replace(theString%2C%20%22%20%22%2C%20%22%22)%3CBR%20%2F%3E%3CBR%20%2F%3E'%20Remove%20Foward%20Slash%3CBR%20%2F%3EtheString%20%3D%20Replace(theString%2C%20%22%2F%22%2C%20%22%22)%3CBR%20%2F%3E%3CBR%20%2F%3E'%20Remove%20Dash%3CBR%20%2F%3EtheString%20%3D%20Replace(theString%2C%20%22-%22%2C%20%22%22)%3CBR%20%2F%3E%3CBR%20%2F%3E'%20Remove%20Comma%3CBR%20%2F%3EtheString%20%3D%20Replace(theString%2C%20%22%2C%22%2C%20%22%22)%3CBR%20%2F%3E%3CBR%20%2F%3E'%20Remove%20LEADING%20Zeros%3CBR%20%2F%3EDo%20Until%20Left(theString%2C%201)%20%26lt%3B%26gt%3B%200%3CBR%20%2F%3EtheString%20%3D%20Mid(theString%2C%202)%3CBR%20%2F%3ELoop%3CBR%20%2F%3E%3CBR%20%2F%3E'%20Remove%20TRAILING%20Zeros%3CBR%20%2F%3EDo%20Until%20Right(theString%2C%201)%20%26lt%3B%26gt%3B%200%3CBR%20%2F%3EtheString%20%3D%20Mid(theString%2C%201%2C%20Len(theString)%20-%201)%3CBR%20%2F%3ELoop%3C%2FP%3E%3CP%3EStrippedChar%20%3D%20theString%3C%2FP%3E%3CP%3EEnd%20Function%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ehere%20is%20how%20it%20looks%20with%20the%20StrippedChar%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Tony2021_0-1643804615987.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F344563i60A2612EB1DC4DBD%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Tony2021_0-1643804615987.png%22%20alt%3D%22Tony2021_0-1643804615987.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20you%20happen%20to%20have%20any%20word%20for%20caution%20please%20let%20me%20know.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3Ethank%20you%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3104692%22%20slang%3D%22en-US%22%3ERe%3A%20Strip%20Leading%20Zero's%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3104692%22%20slang%3D%22en-US%22%3Ethat%20function%20will%20do%2C%20the%20only%20thing%20to%20Get%20rid%20is%20removing%20the%20Trailing%20zeros.%3CBR%20%2F%3E%3CBR%20%2F%3Eyou%20see%201000%20is%20different%20with%201%20(trailing%20zeros%20stripped).%3C%2FLINGO-BODY%3E
Super Contributor

Hello Experts,

 

How could I strip the leading zeros from invoice numbers so that I can simply compare on the non zeros?  My query is below and I have highlighted in red the area that I am looking for a solution. 

I am not sure if this is with Replace? 

thank you. 

SELECT Stampli.Vendor, Stampli.[Invoice #], Import_ExcelPC.[Invoice No], Stampli.[Invoice amount], "these were NOT IN a previous DD" AS ME, Import_ExcelPC.Vendor, Import_ExcelPC.[Invoice amount]
FROM Stampli LEFT JOIN Import_ExcelPC ON (Stampli.[Invoice amount] = Import_ExcelPC.[Invoice amount]) AND (Stampli.Vendor = Import_ExcelPC.Vendor) AND (Stampli.[Invoice #] = Import_ExcelPC.[Invoice No])
WHERE (((Import_ExcelPC.[Invoice No]) Is Null) AND ((Import_ExcelPC.Vendor) Is Null) AND ((Import_ExcelPC.[Invoice amount]) Is Null));

12 Replies

Create a Query that will convert to Numeric [Invoice #] Or [Invoice No] (whichever is String):

 

select *, Val[theFieldname] & "") As NumInvoice from yourTableName;

 

join this Query to your Other table (using NumInvoice as the joining Column).

@arnel_gp 

Hi Arnel, thank you for the reply. I have a follow up if you dont mind.
I created the new query.
The [Invoice #] is a short text field (its a linked table fyi).
When I apply NumInvoice: Val([Stampli].[Invoice #] & "") the result is not as expected on the display.

The original [invoice #] field in the linked table has combinations of letters, dashes, spaces and leading zeros. 

 

Below is how the data looks. You can see the original invoice # and the modified (NumInvoice). There is an example with a leading 0 (the last record) and the NumInvoice does strip the leading zero but the other invoice# records have been relished to either 0 or all stripped after a non number. 

 

Tony2021_0-1643803212445.png

 

 

 

@Tony2021 

 

Arnel, I ran into this code online:
I tested and it seems to return what I need.

Public Function StrippedChar(theString As String) As String

' Remove Spaces
theString = Replace(theString, " ", "")

' Remove Foward Slash
theString = Replace(theString, "/", "")

' Remove Dash
theString = Replace(theString, "-", "")

' Remove Comma
theString = Replace(theString, ",", "")

' Remove LEADING Zeros
Do Until Left(theString, 1) <> 0
theString = Mid(theString, 2)
Loop

' Remove TRAILING Zeros
Do Until Right(theString, 1) <> 0
theString = Mid(theString, 1, Len(theString) - 1)
Loop

StrippedChar = theString

End Function

 

here is how it looks with the StrippedChar:

Tony2021_0-1643804615987.png

 

I you happen to have any word for caution please let me know.  

thank you

that function will do, the only thing to Get rid is removing the Trailing zeros.

you see 1000 is different with 1 (trailing zeros stripped).
best response confirmed by Tony2021 (Super Contributor)
Solution
you can also use RegExp to extract the number:

Public Function fncAlphaNumber(ByVal varInput As Variant) As Variant
varInput = varInput & ""
fncAlphaNumber = varInput
If Len(varInput) < 1 Then
Exit Function
End If
With CreateObject("vbscript.regexp")
.ignorecase = True
.Global = True
.pattern = "[^a-z0-9]"
varInput = .Replace(varInput, "")
While Left$(varInput, 1) = "0"
varInput = Mid$(varInput, 2)
Wend
End With
fncAlphaNumber = varInput
End Function


NumInvoice: fncAlphaNumber([Invoice #])

Review again the requirement, Arnel! ;)

 

You don't need an external library and regex expressions to strip some leading zeros. An expression with just 3 functions should do the trick:

 

IIf(IsNumeric([Invoice #]),Val([Invoice #]),[Invoice #])

 

BTW @Tony2021 "Invoice #" is a horrible field name. Because of the special character you always will need to surround it with square brackets. You should change it to sth like "InvoiceNo" ASAP.

 

Servus
Karl
Access News
Access DevCon

 

 

yes that is true. In my query though I have 2 additional criteria (not only on the invoice #)...I add Co Name and Amount as an extra test. I wanted to remove the trailing zeros since I am dealing with 2 excel files and it could be a case where someone left off a trailing zero (there up to 10 sometimes) in 1 file but enter in all of them in the other and while I know stripping trailing zeros will lead to what you said above, adding in company name and amount I think would be a necessary part if going that route. thanks for pointing that out though.
thank you Karl. My request could have changed a bit after I grabbed some code (posted above) that was a sort of catch all and not only the leading zeros. I know I should have changed that field name to remove the # sign. its an import excel and would now have to change so many things if I changed it to a proper field name. Ugh. thanks for pointing that out.

@arnel_gp 

Hi Arnel,
I liked your solution since it removed a #error I was getting with the other strippedchar function I was using.

I do have an issue it seems.

 

Can I put "Is Null" on the field that is using the the fncAlphaNumber (StrippedInvPC)..pic below?

The result I am getting is no records, which I know is not correct. 

 

If I remove the Is Null from the StrippedInvPC field below then I get the result I need but that is only because of the Is Null criteria on the other fields.  I need the Is Null test on the Invoice No (strippedInvPC) as that is where the real test is and the other fields with Is Null criteria are an extra level of surety. 

 

Please let me know what you think. 

 

Tony2021_0-1643846256947.png

 

use:

Not Is Null

as your criteria.

@arnel_gp 

 

wow that worked. Why do I need Not Is Null on that particular field?  it seems to go against the logic.  Is my screen shot below correct?  

 

Tony2021_0-1643903618450.png

 

depends on what are you Filtering.
if you need the filter fields without values, use Is Null.
if you want to filter only those with values, use Not Is Null.