Another "autonumber" issue with a text field

An expert just helped by with a situation where I am trying to autonumber a text field.

The new situation is I have a text field that looks like this:  SP14-00170.   The next new record should be SP14-00171.

The solution in the previous topic was to create a query with one field that looked like:  

LastTaskCardN: Max(Right([SAItaskCardN],InStr([SAItaskCardN],"-")+1))

And then use this code in the before insert event of the form:

    If Me.NewRecord = True Then
    Dim NewNum As String
        NewNum = DMax("LastTaskCardN", "qryLastSAItaskCardN")
        Me.txtSAItaskCardN = "TC-" & Format(CInt(NewNum) + 1, "0000")
    End If

Open in new window

But now my new situation is different (SP14-00170) instead of AB-1023  in the other topic.

How can I accomplish the same thing which SP14-00170 instead of AB-1023   ??
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.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
It's the same process, except you use a 5 digit format code, and preface the number with "SP14" instead of "AB". Did you try to adapt that logic to your current problem before posting here???

Unless you have other requirements which you haven't spelled out here.
SteveL13Author Commented:
Yes I did.  But I can't get the query to work.  With this I get an error "Data type mismatch in query expression".  (SAILotN is a text field just as SAItaskCardN was).

LastTaskCardN: Max(Right([SAILotN],InStr([SAILotN],"-")+1))
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Try it with -1 instead of +1:

LastTaskCardN: Max(Right([SAILotN],InStr([SAILotN],"-") -1))

If that doesn't work, then break it down into the elemental parts and try each of those until you discover where it fails:

LastTaskCardN: InStr([SAILotN],"-")

And then:

LastTaskCardN: Right([SAILotN],InStr([SAILotN],"-") -1)

And so on ...

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:
The minus 1 gave me the same error.

LastTaskCardN: InStr([SAILotN],"-") gave me a result of 5

Then in experimenting this gave me the f digits I was expecting:

LastTaskCardN: Right([SAILotN],InStr([SAILotN],"-"))

Then this code worked:

    If Me.NewRecord = True Then
    Dim NewNum2 As String
        NewNum2 = DMax("LastSAIlotN", "qryLastSAIlotNumber")
        Me.txtSAIlotN = "SP14-" & Format(CInt(NewNum2) + 1, "00000")
    End If

Open in new window

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.