Convert integer to formatted text string..

  • Thread starter Thread starter Jeremy Sculler
  • Start date Start date
J

Jeremy Sculler

If I have a column with integers:

12345
2345
345

... and want to leftpad the first digits with zeros and insert a space in
front of the two last digits like this:

12345 -> 0123 45
2345 -> 0023 45
345 -> 0003 45

how can that be done in access..?

regards

Jeremy
 
Function PadNumber(NumberIn As Long) As String
Dim strNumber As String

strNumber = Format(NumberIn, "000000")
PadNumber = Left(strNumber, 4) & " " & _
Mid(strNumber, 5)

End Function
 
Jeremy said:
If I have a column with integers:

12345
2345
345

.. and want to leftpad the first digits with zeros and insert a space
in front of the two last digits like this:

12345 -> 0123 45
2345 -> 0023 45
345 -> 0003 45

how can that be done in access..?

=Format(Format([FieldName], "000000"), "0000 00")
 
Rick Brandt said:
Jeremy said:
If I have a column with integers:

12345
2345
345

.. and want to leftpad the first digits with zeros and insert a space
in front of the two last digits like this:

12345 -> 0123 45
2345 -> 0023 45
345 -> 0003 45

how can that be done in access..?

=Format(Format([FieldName], "000000"), "0000 00")

I knew there had to be a better way...!
 
Douglas said:
Jamie Collins said:
Rick said:
=Format(Format([FieldName], "000000"), "0000 00")

I knew there had to be a better way...!

Actually, I was fully expecting someone to come up with a better
way than mine :-)

How about

Format([FieldName], "0000 00")

D'oh! <g>

That is what I tested first and I could have sworn that there was somethign
about it that didn't work correctly on the shorter numbers. However, in trying
it now I can't see any problems. (?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top