Solved

# 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
283 Views
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
0
Question by:frank_guess
• 5
• 3
• 3
• +1

Expert Comment

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
IE:
ABC0000000001
ABC0000000002
ABC0000000003
[...]
ABC00000000999999

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

Author Comment

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.
0

Expert Comment

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.
0

LVL 30

Assisted Solution

hnasr earned 200 total points
"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="ABC"
z="00000000"
f="00000"
n=99
? c & z & right(f & n,6)
0

Author Comment

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

Author Comment

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.
0

Assisted Solution

William Blake earned 100 total points
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)
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
``````

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.
0

LVL 15

Assisted Solution

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
0

LVL 15

Assisted Solution

In .net you can do it with the .Tostring also

VendorID = "VEN" & idval.tostring("00000000000000000")
0

LVL 30

Accepted Solution

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

Author Comment

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

Author Closing Comment

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
0

LVL 30

Expert Comment

Welcome!
0

## Featured Post

Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…