简体   繁体   中英

MSAccess VBA: How to display number with preceding 0?

I have the following zip codes in a linked table. My query pulls from this linked table.

01234
00123
12345
12345-6789

Problem: The preceding zeros are truncated. I have tried adding an apostrophe to convert to string: ="'" & [PCode] But that is not a nice solution.

Please help. Thanks.

Format should suit:

p="12345-6789"
?Format(p,"00000")
12345-6789

p="123"
?Format(p,"00000")
00123

However, if you have a number longer than 5 digits that requires preceding zeros, you will need an IIF:

p="123-6789"
?Format(p,"00000")
123-6789
Postal Code: IIf([PCode] Like "####","0" & [PCode],IIf([PCode] Like "#####-",Left([PCode],Len([PCode])-1),IIf([PCode] Like "#########",Format([PCode],"@@@@@-@@@@"),[PCode])))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM