Need to a text field a value made up of 3 letters + 00000000 +01 through 999999 and have the total length to be no more than 20 long

Posted on 2014-09-03
Medium Priority
Last Modified: 2014-09-04
Need to a text field a value made up of 3 letters + 00000000 +01 through 999999 and have the total length to be no more than 20 long
Question by:frank_guess
  • 5
  • 3
  • 3
  • +1

Expert Comment

by:William Blake
ID: 40302059
I am not sure I understand what you need.  Are you saying you want something that will automatically generate and store a string value of any three letters and 00000000 and 01 -> 999999

Is this what you are looking for?  If so that should be a pretty easy function to write.

Author Comment

ID: 40302094
I am looking for a way to to put a string together to place into a text field.
Field name is Vendor_Number
I have an autonumber field called VendorID
and I need the Vendor_Number to look like this - VEN0000001
This value represents the number FIRST auto number with the VEN0000001  - 10 zeros in between the VEN0000000001
Then from that point on the right side would end with the VendorID number but be right justified so the positions and zeros will always have no more thank 10 spaces so the first number is VEN0000000001 and 999 would be VEN0000000999 if the number grows to VEN0000099999 then the number of zeros goes down but the number of spaces and numbers after the VEN are the 10 spaces.

Expert Comment

by:William Blake
ID: 40302143
Ok I understand.  A couple of questions, is this vendor number being placed on a form field? Is this form opened by a few people at once or only one person?

And to be clear it needs to be VEN + 10 digits.  Right? You said one thing above and typed another as an example.
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

LVL 31

Assisted Solution

hnasr earned 800 total points
ID: 40302148
"no more than 20 long"
The maximum string is ABC00000000999999 which is 17 long.

Try this in the immediate window:
The idea is to concatenate the 3 characters + 8 0s + right 6chars (from 5 0s + the number)
? "ABC" & "00000000" & right("00000" & 99,6)

To use it in code: Try the following statements  in immediate window.

? c & z & right(f & n,6)

Author Comment

ID: 40302193
William sorry I made a mistake but yes max string lenght is 20 long.
that includes VEN + 17 Zeros and numbers

Author Comment

ID: 40302201
Sorry hnasr You are correct with my example.

So it looks like this:

A = "VEN"
B = "000000000000000000000"
C = [VendorID] - this starts at 1 and goes to 9999999

The value now looks like "VEN00000000009999999"
The number of zeros decrease as the number of 9's increase.

Assisted Solution

by:William Blake
William Blake earned 400 total points
ID: 40302205
Ok, here is some VBA that might get you started.  It does a DMAX lookup to get the highest Vendor ID in a table (I assume you have some sort of vendors table).  Then strips off the VEN prefix and converts the id to a number and adds one.  It then converts it back to a string and gets a string length which using an if else if statement adds the apppriate number of zeros between VEN and the Counter.  Let me know if this goes in the right direction.  If you need it to appear on a form, that can easily be done too (although I might want this function to fire when the form is submitted to make sure the vendor ID remains unique.

Dim vendorID As String
Dim vendorIDNum As String
Dim idVal As Integer
Dim vendorIDLen As Integer
' get the max vendor id from a table
vendorID = DMax("vendorID", "table1")
' split off the ven prefix
vendorIDNum = Mid(vendorID, 4)
'convert to a number
idVal = CInt(vendorIDNum)
' add one to it
idVal = idVal + 1
'convert back to string
vendorID = CStr(idVal)
'check the length
vendorIDLen = Len(vendorID)

If vendorIDLen = 1 Then
    vendorID = "VEN000000000" & vendorID
ElseIf vendorIDLen = 2 Then
    vendorID = "VEN00000000" & vendorID
ElseIf vendorIDLen = 3 Then
    vendorID = "VEN0000000" & vendorID
ElseIf vendorIDLen = 4 Then
    vendorID = "VEN000000" & vendorID
ElseIf vendorIDLen = 5 Then
    vendorID = "VEN00000" & vendorID
ElseIf vendorIDLen = 6 Then
    vendorID = "VEN0000" & vendorID
ElseIf vendorIDLen = 7 Then
    vendorID = "VEN000" & vendorID
ElseIf vendorIDLen = 8 Then
    vendorID = "VEN00" & vendorID
ElseIf vendorIDLen = 9 Then
    vendorID = "VEN0" & vendorID
ElseIf vendorIDLen = 10 Then
    vendorID = "VEN" & vendorID
End If

Open in new window

I did the above while I thought it was 10 digits between the VEN and counter.  You will have to adjust the If else if to compensate.
LVL 15

Assisted Solution

ChloesDad earned 800 total points
ID: 40302313
You can do it in one line

VendorID = "VEN" + right("0000000000000000" & CStr(idVal),17)

This will add the right most 17 characters of 0000000000000000000000xxx to the initial string
LVL 15

Assisted Solution

ChloesDad earned 800 total points
ID: 40302316
In .net you can do it with the .Tostring also

VendorID = "VEN" & idval.tostring("00000000000000000")
LVL 31

Accepted Solution

hnasr earned 800 total points
ID: 40302323
Try this:
A = "VEN"
B = "00000000000000000"
You may use:
B = string(17,"0")
C= [VendorID]
txt = A  & RIGHT(B & C , 17)

Author Comment

ID: 40302373
Thanks to everyone.  I will review and let you know in the morning.  Got to go and pick up something before the store closes.

Author Closing Comment

ID: 40303596
All solutions offered except for the Dot Net, sorry I have not got into Dot Net yet.  This is in an Access 2010 database system and I have not yet moved it to an Internet or Intranet offering but thank you for your insight.  William Blake your solution works great when I want to use a button to run the code.  CholesDad The one line works great too.  Hnasr your code works great as well.  You all have done a great job for me.
I forget to add  that what I needed was a way to do these in a one line by running a query but I figured that out when I remembered a small utility I built a year ago to develop a modulo and then call it within the query.  Thanks all
LVL 31

Expert Comment

ID: 40303804

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
As a person who answers a lot of questions, I often see code that could be simplified, made easier to read, and perhaps most importantly made easier to maintain if the code was modified to use the Select Case statement. This article explains how to…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question