How "autonumber" text field for next record

I have a field on a form that for example looks like this...  AB-1023  it is a text field

When a new record is added I want the new record's field to be AB-1024.  There will always be a hyphen before the numbers.  Kind of like auto-number but since its not a number field it isn't.

How can I accomplish this?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:

Here is one way of doing it. Have an auto number in your table and then take the max add 1 and and append it to AB-

MaxNumber = Select max(autonumber) from your_table_name

Your_Column_Value  = "AB -" & MaxNumber +1

so if your max number is 1023 then Your_Column_Value will be AB- 1024
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You can write a function to get the biggest number after that AD- preface and add one.

Create a query with a single field:

Max(YourFieldName, Instr(YourFieldName, "-") -1)

This would return the numeric value after that hyphen.

Then just query that query for the Max. You could use DMax for that:

Dim NewNum As String
NewNum = DMAX("NameOfYourQueryField", "NameOfYourQuery")

You could run this in the BeforeInsert or AfterInsert event of the form. This is run when adding a new Record, so the process will not overwrite existing records. I would assume your code would look something like this:

Me.YourTextField = "AD-" & CInt(NewNum) + 1
SteveL13Author Commented:
Scott:  I entered this in query designer (with the table in the query designer), but get an error when I tab off the field.  The error says "The expression you entered has a function containing the wrong number of arguments.

NewTaskCardN: Max(SAItaskCardN, Instr(SAItaskCardN, "-") -1)
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
That should have been:

NewTaskCardN: Max(Right(SAItaskCardN, Instr(SAItaskCardN, "-") -1))
SteveL13Author Commented:
No error now.  But  have two test records in the table...  TC-0386 and TC-0387.

The query is returning 87 and I was expecting 0387.

SteveL13Author Commented:
Ok I have the query returning 0387.

But the form is returning AD-388 instead of AD-0388  (I need the zero)
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
0387 isn't a number - why would you expect that??

This code piece:

Me.YourTextField = "AD-" & CInt(NewNum) + 1

If you want to further format that, then use the Format function to do so. See this Help file:

Something like:

Me.YourTextField = "AD-" & Format(CInt(NewNum) + 1, "0000")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
Perfect.  Thank you very much.  This did it...

Me.YourTextField = "AD-" & Format(CInt(NewNum) + 1, "0000")
SteveL13Author Commented:
If you have time please see new topic "Another "autonumber" issue with a text field"
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.