Results 1 to 4 of 4
  1. #1
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199

    How to add leading zeros to a field through a query

    Hello,

    I have data in a table where one of the fields is a text field full of numbers in each row, i.e. 56791, 345672. At this point, with the data already in the table, how can I create a new table/query to force a character limit using leading zeros. For instance every entry has to be 10 digits so query would do this: 0000056791 and 0000345672.

    Thanks,

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    One way is to do this through a Update Query.
    Select all records where the length of your field is less than 10.
    Then, append 10 zeroes to the entry in your field, and take the 10 right-most characters.

    The SQL code of that Query will look something like this:
    Code:
    UPDATE Table1 
    SET MyField = Right("0000000000" & [MyField],10)
    WHERE Len([MyField])<10;

  3. #3
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    txtnumber=format(myfield,"0000000000")

    or if you want to keep it's numeric properties, change the field type to long and simply put 0000000000 in the format property of the table field or form/report control or use the above format function in a query

    Not sure how you would force a character limit using preceding zero's, the usual way would be to set the character limit to 10 in your field size property

  4. #4
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    @joeM, thanks this solution worked great!

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

Similar Threads

  1. Do not add leading zeros
    By phifer2088 in forum Access
    Replies: 5
    Last Post: 03-02-2015, 10:05 AM
  2. Issues with leading zeros in a form field
    By apetriella in forum Forms
    Replies: 2
    Last Post: 02-10-2015, 11:27 AM
  3. Replies: 2
    Last Post: 01-26-2015, 03:53 AM
  4. Replies: 1
    Last Post: 07-11-2012, 07:16 PM
  5. Leading Zeros
    By dirtbiker1824 in forum Access
    Replies: 1
    Last Post: 03-14-2011, 02:16 PM

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