Results 1 to 10 of 10
  1. #1
    jhrBanker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    68

    Pad Text Field With Zeros in VBA

    I have several Text fields in a table. These fields contain both numbers and text (for example: 123, A532, BBB, Null, etc.). I need to concatenate the fields into a single string, with each field separated with a dash, and all fields being the same length (for example 6 positions), so that the result of the above fields would be 000123-00A532-000BBB-000000. The Format function works okay when there's only numbers in the field, but not when non-numeric characters are present (or null values).



    Any suggestions?

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Why not just write your own User Defined Function (UDF)?

  3. #3
    jhrBanker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    68
    Is that something you could help me with?

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Here's the Function that goes in a Standard Module:
    Code:
    Public Function PadIt(InString As String, Optional InLength As Integer = 6) As String
    '-- Pad InString with leading zero's
       PadIt = String(InLength - Len(InString), "0") & InString
    End Function

  5. #5
    jhrBanker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    68
    Then to use it I would code as follows?
    Call PadIt(Field1,"0")

    Sorry, but I'm new at this.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    If you want to pad to 6 characters then you would use it like:
    MyField = PadIt([Field1]) & "-" & PadIt([Field2])...
    If other than 6 then:
    MyField = PadIt([Field1],n) & "-" & PadIt([Field2],n)...
    ...where n is the length of the final padded string.

  7. #7
    jhrBanker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    68
    Thanks for your help. I'll give it a try.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Let us know how you make out.

  9. #9
    jhrBanker is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jun 2009
    Posts
    68
    Works like a charm. Thanks for your help. Greatly appreciated.

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Excellent. Enjoy.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Unbound text box truncates leading zeros
    By brharrii in forum Forms
    Replies: 4
    Last Post: 01-10-2016, 08:06 PM
  2. Replies: 4
    Last Post: 10-15-2012, 11:38 AM
  3. Replies: 1
    Last Post: 07-11-2012, 07:16 PM
  4. Replies: 12
    Last Post: 06-04-2012, 10:55 AM
  5. Replies: 2
    Last Post: 05-05-2010, 02:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums