Results 1 to 7 of 7
  1. #1
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153

    Lightbulb Using Right() and if the results have the first digit as a 0 change to a 1


    We have fuel cards and for PIN numbers I set them as the last four digits of the DL number. But the PIN cannot begin with a zero, so I just changed all the ones that began with 0 to a 1.

    Now how can I get a query to calculate a field that will do the same in my database.

    EXAMPLE::
    DL Number Last 4 Digits via Right([DLN], 4) PIN Number ACTUAL Digits
    J083573698 3698 3698
    K080850789 0789 1789

    The second column I know how to get with the Right() function, but how can I get it to give me the third column.
    I know I'm going to have to do it in VBA but I'm still so timid with VBA.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    How are you generating the PIN now? VBA? Formula?
    And are you using a form?

    Please show the formula or code.

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    A suitable query expression might be:

    iif(mid([DL],7,1)="0","1",mid([DL],7,1)) & mid([DL],8)

    This assumes all the DL Numbers are 10 characters long as in your example; it will be a bit more complicated if that is not the case.

    John

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    breakingme10,

    Sometimes it's a lot simpler to just put 1 fact in 1 field.(KISS method)
    1 field for DL Number and 1 field for PIN.

    If no PINs can start with 0, how did you end up with a DL like K080850789 in the first place?

    What happens to DL Numbers if you have to change PIN for any reason?

  5. #5
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    OK I thought that was a universal abbreviation, apparently maybe its only around Oklahoma that DL stands for "Driver's License" so the DLNumber field is their driver's license number, which cannot change! I originally just used an excel spreadsheet and manually typed them in to send the PIN numbers to the fuel card company. I was just thinking since the data was derived from another field in the database that I should use VBA or a calculated query field to generate the PIN in the database.
    As I said in the first post, I was trying to use the
    Code:
    PIN: Right([DLNumber], 4)
    calculation in a query.
    But if the first digit of those 4 is a 0 I need to substitute a 1 in it's place.

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    But if the first digit of those 4 is a 0 I need to substitute a 1 in it's place.
    Did John_G's answer solve your problem?
    It works for me when I test it out on your data.

  7. #7
    breakingme10 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Oklahoma
    Posts
    153
    YES it worked great! All OK DL's are 1 letter + 9 digits!

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

Similar Threads

  1. Replies: 7
    Last Post: 06-26-2014, 01:41 PM
  2. dates - 2 digit year
    By RCDAwebmaster in forum Queries
    Replies: 5
    Last Post: 05-20-2014, 08:14 AM
  3. Combo box is recognized as digit format
    By Gealeks in forum Forms
    Replies: 8
    Last Post: 11-27-2013, 03:08 PM
  4. How to change year digit in update query?
    By Osman in forum Queries
    Replies: 6
    Last Post: 10-27-2010, 04:35 AM
  5. display single digit
    By appiades in forum Access
    Replies: 2
    Last Post: 07-05-2010, 05:36 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