Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Force Text Field To Always Be 4 Characters


    We have a table where the data is alphanumeric and can be between 2 - 4 characters. If the data is < 4 characters we want to append a 0 to the beginning of the string to force it to be 4 characters.

    Example - 1b would become 001b or 11b would become 011b. Can you add an input mask on a query to do such or would I need to run an update query on the field to somehow append 1 zero the any string where Len = 3 and append 2 zero's to any string where Len = 2?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can leave it as entered and display it with the Format() function. I suppose you could also use that function in the update event of the textbox and add the padded zeros there.

    Format(FieldName, "0000")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by pbaldy View Post
    You can leave it as entered and display it with the Format() function. I suppose you could also use that function in the update event of the textbox and add the padded zeros there.

    Format(FieldName, "0000")
    I am actually using it as a control source for a report and want the report to display 4 digits. Would I be better off formatting in the query or report?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Will it always be a 'b' or can there be other alpha characters? Always just the single alpha at the end?

    InputMask property can be a bit aggravating, especially if you don't want to force users to enter the prefix zeros. VBA in the textbox AfterUpdate event can modify the value before it is committed to record.

    If you need to modify existing data, will be a little complicated because of the alpha.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It probably doesn't matter; I'd probably do it on the report.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by June7 View Post
    Will it always be a 'b' or can there be other alpha characters? Always just the single alpha at the end?

    InputMask property can be a bit aggravating, especially if you don't want to force users to enter the prefix zeros. VBA in the textbox AfterUpdate event can modify the value before it is committed to record.

    If you need to modify existing data, will be a little complicated because of the alpha.
    Yes - it would be existing data. We have a button press event on a form that imports an Excel spreadsheet into a table. So the data is imported however it is sent.

    No, "b" will not always be the first example, those were just the first two I saw in the table when making this post so I used for example.

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by pbaldy View Post
    It probably doesn't matter; I'd probably do it on the report.
    Would I do such by setting the Input Mask of the text box that has the field as the control source?

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    A control source of:

    =Format(...)

    with the text above.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    If you used Format([MyField],"0000") in an update query, it might work if the values began with letters, but I think it would alter any value that began with a number to become 0000. The problem with trying to do this in an after update event on a form is that you'd have to leave the record in order to update it, otherwise you'd get a lock violation error - unless maybe the option for no locks was set.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by pbaldy View Post
    A control source of:

    =Format(...)

    with the text above.
    Set the control source to Format([MyField],"0000")

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    ?format(123,"0000")
    0123
    ?format("123","0000")
    0123
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by jo15765 View Post
    Set the control source to Format([MyField],"0000")
    With the = I mentioned, yes.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by pbaldy View Post
    With the = I mentioned, yes.
    Perfect, that got it! Thank you!

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    =Format([MyField],"0000")
    I was trying that because I figured it should work in the report and was surprised at what I got. With 3 records (1a, 2b, 3c) in a short text field (default 255 character limit, no table level formatting) and an additional textbox on the report with the expression as above, I got this for 3 records (left control bound to table, right control with the expression):

    1a 0000
    2b 2b
    3c 3c
    What the heck?? Just in case the 1 was an issue, I changed it to 4a in the table and got the same result - 0000. OK, so what if I was right about the values starting with numbers versus letters? Changed 4a to a4 and got a4. So why does the solution seem to work for the OP??
    Last edited by Micron; 11-04-2017 at 06:20 PM. Reason: added info

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 08-25-2015, 05:19 PM
  2. HTML characters in plain text field
    By etorasso in forum Access
    Replies: 3
    Last Post: 09-25-2014, 11:47 AM
  3. Replies: 7
    Last Post: 06-20-2014, 08:55 PM
  4. Replies: 1
    Last Post: 05-01-2014, 11:33 AM
  5. Counting Characters in a text field
    By velvettiger in forum Queries
    Replies: 1
    Last Post: 03-12-2010, 12:36 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