Split PostCode

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Split PostCode

Post by D Willett »

Hi Guys

I need to split a postcode which I can do by removing the last 3 characters but that sometimes leaves a space. The field is:

ContactPostcode

RawPostCode: Left([ContactPostcode],Len([ContactPostcode]-3))

TrimmedPostCode: Trim(Left([RawPostCode],Len([RawPostCode]-3)))

But these return #Error, can anyone see why?

Kind Regards
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78439
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Split PostCode

Post by HansV »

Move the closing parenthesis after -3 to after [ContactPostcode]:

RawPostCode: Left([ContactPostcode],Len([ContactPostcode])-3)

TrimmedPostCode: Trim(Left([RawPostCode],Len([RawPostCode])-3))
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Split PostCode

Post by D Willett »

Hmm, not quite the result I was after Hans?
ScreenHunter_097.jpg
You do not have the required permissions to view the files attached to this post.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78439
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Split PostCode

Post by HansV »

Sorry, the latter should be

TrimmedPostCode: Trim(Left([ContactPostCode],Len([ContactPostCode])-3))

or

TrimmedPostCode: Trim([RawPostCode])
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78439
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Split PostCode

Post by HansV »

If you want to suppress the error value, use (for example)

RawPostCode: IIf(Nz([ContactPostCode])="",Null,Left([ContactPostcode],Len([ContactPostcode])-3))

TrimmedPostCode: IIf(Nz([ContactPostCode])="",Null,Trim(Left([ContactPostCode],Len([ContactPostCode])-3)))
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Split PostCode

Post by D Willett »

Just the ticket Hans, that's very useful.

Thanks once more.

Cheers
Cheers ...

Dave.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Split PostCode

Post by macropod »

Try:
=LEFT([ContactPostCode]& " ",FIND(" ",[ContactPostCode])-1)
Since one of your codes lacks a space, that one will be output in full.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
HansV
Administrator
Posts: 78439
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Split PostCode

Post by HansV »

This is about Access, Paul...
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Split PostCode

Post by macropod »

Oops! Perhaps something along the lines of:
Split([ContactPostCode]& " "," ")(0)
Paul Edstein
[Fmr MS MVP - Word]

User avatar
HansV
Administrator
Posts: 78439
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Split PostCode

Post by HansV »

You'd have to wrap that in a VBA function, for Split doesn't work when used directly in a query.
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Split PostCode

Post by macropod »

Fair enough - I don't work with Access much.
Paul Edstein
[Fmr MS MVP - Word]