How get 4 characters from a string of characters

If I have this string of characters in a text field on a form...  "80522526J142-101"    How can I "pull" the "J142" portion of it out for use in another text field on a form?  It will always be the 4 characters prior to the hyphen.
SteveL13Asked:
Who is Participating?

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

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

Rgonzo1971Commented:
Hi,

pls try

res: Mid("yourString",Instr(1,"yourString", "-")-5,Instr(1,"yourString", "-")-1)

Regards
Russ SuterSenior Software DeveloperCommented:
You could do this with a simple Regex. The following will match the 4 characters immediately preceeding a '-' character.

var testString = '80522526J142-101';

alert(testString.match(/(.{4})(?=-)/));

Open in new window

Rgonzo1971Commented:
Corrected Code

res: Mid("yourString",Instr(1,"yourString", "-")-5,4)
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

SteveL13Author Commented:
This didn't work:  Mid("yourString",Instr(1,"yourString", "-")-5,Instr(1,"yourString", "-")-1)


and

what is Regex?
Russ SuterSenior Software DeveloperCommented:
Sorry, slight edit to above...
var testString = '80522526J142-101';

alert(testString.match(/(.{4})(?=-)/)[0]);

Open in new window

This is just in case your string contains more than 1 '-' character. If you need to respond to multiple matches then the logic gets a little more complex.
SteveL13Author Commented:
Actually I'm trying to do it in a query.  Like:

Query snapshot
But when I try to run the query I get "Invalid procedure call".

???
Rgonzo1971Commented:
pls try
Code: Mid( [PartN],Instr(1, [PartN], "-")-5,4)

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:
Had to modify slightly:

Code: Mid([PartN],InStr(1,[PartN],"-")-4,4)
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.