2

We have a field in a query that should be left-padded with zeroes if it is too short, and we accomplish this using the Format() function. However, there are some values that produce bizarre results.

Format("14425112-8","00000000-00")

Returns the value "00019330-78"

For most inputs, the string gets formatted as expected, 8 digits, hyphen, two digits. But in a few rare cases, the value is modified. Is this repeatable for anyone else? Does anyone have an explanation?

Thanks for your help.

2 Answers 2

1

This is an example of access trying to be too helpful. It looks like it is interpreting these values as dates, but since you didn't use any date indicators in the format e.g: (dd,mm,yyyy), it converted 1-1 to a date, and then tried to display it in decimal form:

debug.print Format("1-1","000000-00")

returns 000427-36 which is the decimal value 42736 which, if you convert to a date, becomes 1/1/2017. This is what access interpreted "1-1" as.

it seems that access has reserved the - character as symbolizing a date format, despite what their website says. This function is only useful for formatting actual dates, or numeric values, such as prices. If you are set on using the format function, you will have to change you separator to a decimal point, which apparently is the only character that will get you what you want with the leading and trailing zeros.

Otherwise, you may have to build your own function for this.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Quark. this is valuable insight.
1

You cannot format a string like a number this way. Try this:

PaddedNumber = Right(String(8, "0") & "14425112-8", 10)

2 Comments

Thanks for the info, this is an interesting way to pad left zeroes. But I really am hoping for some understanding of why the format function fails in this regard and not just "You can't do it that way."
The Format function doesn't "fail". It's simply that you cannot format strings as if they were numbers. So either do clean string handling, or convert your strings to numbers and then format these as you wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.