Solved

MS Access -- Left() and InStr() Problem

Posted on 2006-07-13
6
460 Views
Last Modified: 2008-07-03
Here is my issue:

I am using the following in select statement:

Left(N.FirstName,InStr(1,N.FirstName,' '))

It works fine when N.FirstName is two words but return nothing when it is only one word.

I understand because InStr(1,N.FirstName,' ') is returning '0' then I am tryng to get everything to the left of '0'.  Is there away to put in some sort of if statement to take this into account.

0
Comment
Question by:daugh016
  • 2
  • 2
  • 2
6 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 17100393
U have to make use of IIF

assuming this is done in a query

Left(N.FirstName,   IIF(InStr(1,N.FirstName,' ')>0,InStr(1,N.FirstName,' '),Len(N.FirstName))

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17100399
ok, my mistake. Out of practice I give the answer but dont always give an explanation. Sorry about this, I hadnt realise what I do was considered Post Tagging
So if u need an explanation, let me know
0
 
LVL 4

Expert Comment

by:Carl2002
ID: 17100406
It is returning nothing when N.Firstname is one word because of the " " at the end of the statement. The expression is specifically searching for the first space, if there isn't one it will fall down.

Can you explain a little more what you are trying to achieve?
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:daugh016
ID: 17100439
Carl,

If someones first name is two words, I just want the first one.  

Example if someones name is Mike, I want it to show Mike.
Example if someones name is John Mark, I want it to show John.

Rockiroads,

I couldn't get it to work.  It says missing operator.

0
 
LVL 4

Accepted Solution

by:
Carl2002 earned 50 total points
ID: 17100498
Try this

Expr1: IIf(InStr([field1]," ")=0,[field1],Left([field1],InStr([field1]," ")))
0
 

Author Comment

by:daugh016
ID: 17100541
Thanks Carl.  That worked
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now