Results 1 to 6 of 6
  1. #1
    WILTSBOY is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2017
    Posts
    7

    After Update on Form to Reduce Number of Digits

    I have a field called Supplier_Number that is being used on an input form.


    I would like to ensure that 9 digits are always input into this field including any lead in zeros. Error message to be shown if 9 digits not input.
    I believe an After Update function can be used with Code.
    Could someone advise me what Code I could use in the Code Builder?
    Thank you.

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    No code needed
    Format the textbox on your form as 000000000
    Leading zeroes will be added automatically after data is added
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    WILTSBOY is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2017
    Posts
    7
    Hi,
    I tried that but if a user searches for the record via a query then the lead in zeros are ignored.

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Quote Originally Posted by WILTSBOY View Post
    Hi,
    I tried that but if a user searches for the record via a query then the lead in zeros are ignored.
    Format the field in the table design using that format
    If say 123 is entered using form or table it will be displayed as 000000123
    It will then work if you enter either value as the filter criterion using a query.
    Note that if the long version is used, Access will remove the leading zeroes in the filter criteria but still display the result
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Join Date
    Apr 2017
    Posts
    1,679
    When you need the supplier_number to be a text field in your table, then you have to write OnLostFocus event for text box
    Code:
    Private Sub Text0_LostFocus()
        Me.supplier_number = Right("000000000" & Me.supplier_number, 9)
    End Sub
    or
    Code:
    Private Sub Text0_LostFocus()
        Me.supplier_number = Format(Me.supplier_number, "000000000")
    End Sub
    But you have to remember, that the field is now a text. E.g. to calculate a numeric value bigger on 1 you have to use the formula
    Code:
    =CInt([supplier_number])+1
    And for text string like supplier_number, but bigger on 1
    Code:
    =Format(CInt([supplier_number])+1,"000000000")
    Btw, with Format function you can have various delimiters for grouping numbers in text field. And when you use only spaces for delimiters, you can convert those strings to numbers - CInt() works without problems (for me, I'm not sure about US settings - maybe with them it works with commas only in such way).
    So when you use format "00 000000 0" and enter 12345, you get the string "00 001234 5", and CInt(([supplier_number]) returns 12345.
    When you use format "000 000-000" and enter 123245, you get the string "000 012-345", and CInt(([supplier_number]) returns an error.

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    That's all very well Arvil but not necessary for this situation.
    The field can be a number field but still be shown with leading zeroes at table and query level
    See attached for a very simple example
    Attached Files Attached Files
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. Replies: 2
    Last Post: 05-04-2016, 06:13 AM
  2. Trying to reduce number of fields
    By dallin in forum Database Design
    Replies: 1
    Last Post: 09-09-2014, 11:32 PM
  3. Replies: 6
    Last Post: 07-23-2012, 11:24 AM
  4. Replies: 5
    Last Post: 12-26-2010, 10:56 PM
  5. format number filed to significant digits
    By stileguru in forum Reports
    Replies: 3
    Last Post: 08-13-2010, 01:26 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