Results 1 to 5 of 5
  1. #1
    Matthieu is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2009
    Posts
    31

    Question Sql Syntax Query

    So, i'm trying to create a query that will take what the Options Groups in my forms input and change them from 1, 2, 3, or -1 to Yes, No, N/A and 1. I am trying to use an iif statement to do this, this is what I have:


    iif([Data].[Customer]=1, [Data].[Customer]= "Yes", (iif([Data].[Customer]=2,[Data].[Customer]= "No",[Data].[Customer]="N/A"))) AS Customer, etc..
    Instead of changing each cell from 1,2 or 3 to Yes, No, N/A, it's creating new columns and leaving them blank. I can't figure out what i'm doing wrong. I know sql very roughly, having mostly taught myself. Any thoughts would help!
    Notes: Data.Customer is one of the columns in the data- table. I am trying to modify multiple columns. I have also tried something like iif([Data].[Customer]=1, "Yes", (iif([Data].[Customer]=2, "No", "N/A"
    -this did not work either.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Do you have more than one table in your query? If not, leave the table scoping off of your reference.
    IIF(Customer]=1, "Yes", IIF([Customer]=2, "No", ..etc

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You could also make it easier to read and maintain if you created a Global Function in a standard module and used a Select Case structure.

  4. #4
    Matthieu is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2009
    Posts
    31
    Thanks, I got it to work like this:
    (iif([Customer]= 1, "Yes", iif([Customer]=2, "No", "N/A"))) AS Customer1
    and did that for each column. I would prefer to do that Global Function Case statement that you recommended but I attempted it in Visual Basic and my understanding of VB is too limited, I couldn't get it to work. I gave up and just decided to do it in sql like this. Thanks for the help though and if you have recommendations on how to write something like that or a good link, that would also be appreciated. Writing it in sql like this is messy and difficult to work with.

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows XP Access 2002
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The code in a standard module will look something like:
    Code:
    Public Function Foo(InNum As Integer) As String
       Select Case InNum
          Case 1
             Foo = "Yes"
          Case 2
             Foo = "No"
          Case 3
             Foo = "N/A"
          Case -1
             Foo = "1"
          Case Else
             '-- Error or return what ever you want
       End Select
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Looping syntax
    By fadiaccess in forum Access
    Replies: 1
    Last Post: 10-23-2009, 02:57 AM
  2. Replies: 1
    Last Post: 10-07-2009, 07:36 AM
  3. Syntax-reference to pages
    By AmyHill in forum Access
    Replies: 5
    Last Post: 08-27-2009, 10:04 AM
  4. sql syntax error
    By SMAlvarez in forum Access
    Replies: 1
    Last Post: 03-12-2009, 09:43 PM
  5. Syntax Error In Insert Into Statement
    By frankvfox in forum Queries
    Replies: 1
    Last Post: 09-09-2008, 12:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums