Iif Then Else Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hgwright
    New Member
    • Jul 2008
    • 2

    Iif Then Else Statement

    I have a database where I have fields for First Name and Surname. In the FirstName field we list more than one name. I need to write a statement that will allow me to export this field with only the first name in it. I have found a statement that allows me to remove everything after the first name "(Left([FirstName],InStr(1,[FirstName]," ")-1)", but if they only have one name then this statement gives me an "#Error" message. Does anyone have any suggestions?

    hgwright
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    This will check for the space first, then act accordingly.

    Code:
     If InStr(1, [FirstName], " ") > 0 Then
      ExportFirstName = Left([FirstName], InStr([FirstName], " ") - 1)
    Else
      ExportFirstName =  [FirstName]
    End If
    Welcome to Bytes!

    Linq ;0)>

    Comment

    • hgwright
      New Member
      • Jul 2008
      • 2

      #3
      In SQL you need something like :
      Code:
      IIf(InStr(1,[FirstName]," ")>0,Left([FirstName],InStr(1,[FirstName]," ")-1,[FirstName])

      Comment

      Working...